mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 14:31:28 +00:00
refactored code
This commit is contained in:
parent
5a4e92199b
commit
3cebdea82c
@ -1,13 +1,19 @@
|
||||
//Тепер структура Item тепер має приватні поля, оскільки вони вже не потребують зовнішнього доступу
|
||||
//У методі GildedRose::update_quality() замість вкладених умов if-else застосовано збіги за допомогою match. Це полегшує розуміння логіки для кожного типу товару
|
||||
//Я створила окремі методи update_aged_brie(), update_backstage_passes() та update_normal_item()
|
||||
// для оновлення якості товару відповідно до його типу. Це зробило код більш читабельним та зменшило повторення
|
||||
//Також я додала метод update_sell_in(), який відповідає за оновлення поля sell_in для всіх типів товарів, крім "Sulfuras, Hand of Ragnaros"
|
||||
use std::fmt::{self, Display};
|
||||
|
||||
pub struct Item {
|
||||
pub name: String,
|
||||
pub sell_in: i32,
|
||||
pub quality: i32,
|
||||
name: String,
|
||||
sell_in: i32,
|
||||
quality: i32,
|
||||
}
|
||||
|
||||
impl Item {
|
||||
pub fn new(name: impl Into<String>, sell_in: i32, quality: i32) -> Item {
|
||||
Item {
|
||||
pub fn new(name: impl Into<String>, sell_in: i32, quality: i32) -> Self {
|
||||
Self {
|
||||
name: name.into(),
|
||||
sell_in,
|
||||
quality,
|
||||
@ -22,64 +28,67 @@ impl Display for Item {
|
||||
}
|
||||
|
||||
pub struct GildedRose {
|
||||
pub items: Vec<Item>,
|
||||
items: Vec<Item>,
|
||||
}
|
||||
|
||||
impl GildedRose {
|
||||
pub fn new(items: Vec<Item>) -> GildedRose {
|
||||
GildedRose { items }
|
||||
pub fn new(items: Vec<Item>) -> Self {
|
||||
Self { items }
|
||||
}
|
||||
|
||||
pub fn update_quality(&mut self) {
|
||||
for item in &mut self.items {
|
||||
if item.name != "Aged Brie" && item.name != "Backstage passes to a TAFKAL80ETC concert"
|
||||
{
|
||||
if item.quality > 0 {
|
||||
if item.name != "Sulfuras, Hand of Ragnaros" {
|
||||
item.quality = item.quality - 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if item.quality < 50 {
|
||||
item.quality = item.quality + 1;
|
||||
|
||||
if item.name == "Backstage passes to a TAFKAL80ETC concert" {
|
||||
if item.sell_in < 11 {
|
||||
if item.quality < 50 {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if item.sell_in < 6 {
|
||||
if item.quality < 50 {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
match item.name.as_str() {
|
||||
"Aged Brie" => self.update_aged_brie(item),
|
||||
"Backstage passes to a TAFKAL80ETC concert" => {
|
||||
self.update_backstage_passes(item)
|
||||
}
|
||||
"Sulfuras, Hand of Ragnaros" => {}
|
||||
_ => self.update_normal_item(item),
|
||||
}
|
||||
self.update_sell_in(item);
|
||||
}
|
||||
}
|
||||
|
||||
if item.name != "Sulfuras, Hand of Ragnaros" {
|
||||
item.sell_in = item.sell_in - 1;
|
||||
}
|
||||
fn update_aged_brie(&mut self, item: &mut Item) {
|
||||
if item.quality < 50 {
|
||||
item.quality += 1;
|
||||
}
|
||||
item.sell_in -= 1;
|
||||
if item.sell_in < 0 && item.quality < 50 {
|
||||
item.quality += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if item.sell_in < 0 {
|
||||
if item.name != "Aged Brie" {
|
||||
if item.name != "Backstage passes to a TAFKAL80ETC concert" {
|
||||
if item.quality > 0 {
|
||||
if item.name != "Sulfuras, Hand of Ragnaros" {
|
||||
item.quality = item.quality - 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
item.quality = item.quality - item.quality;
|
||||
}
|
||||
} else {
|
||||
if item.quality < 50 {
|
||||
item.quality = item.quality + 1;
|
||||
}
|
||||
}
|
||||
fn update_backstage_passes(&mut self, item: &mut Item) {
|
||||
if item.quality < 50 {
|
||||
item.quality += 1;
|
||||
if item.sell_in < 11 && item.quality < 50 {
|
||||
item.quality += 1;
|
||||
}
|
||||
if item.sell_in < 6 && item.quality < 50 {
|
||||
item.quality += 1;
|
||||
}
|
||||
}
|
||||
item.sell_in -= 1;
|
||||
if item.sell_in < 0 {
|
||||
item.quality = 0;
|
||||
}
|
||||
}
|
||||
|
||||
fn update_normal_item(&mut self, item: &mut Item) {
|
||||
if item.quality > 0 {
|
||||
item.quality -= 1;
|
||||
}
|
||||
item.sell_in -= 1;
|
||||
if item.sell_in < 0 && item.quality > 0 {
|
||||
item.quality -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn update_sell_in(&mut self, item: &mut Item) {
|
||||
if item.name != "Sulfuras, Hand of Ragnaros" {
|
||||
item.sell_in -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,7 +12,6 @@ fn main() {
|
||||
Item::new("Backstage passes to a TAFKAL80ETC concert", 15, 20),
|
||||
Item::new("Backstage passes to a TAFKAL80ETC concert", 10, 49),
|
||||
Item::new("Backstage passes to a TAFKAL80ETC concert", 5, 49),
|
||||
// this conjured item does not work properly yet
|
||||
Item::new("Conjured Mana Cake", 3, 6),
|
||||
];
|
||||
let mut rose = GildedRose::new(items);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user