mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-16 06:51:27 +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};
|
use std::fmt::{self, Display};
|
||||||
|
|
||||||
pub struct Item {
|
pub struct Item {
|
||||||
pub name: String,
|
name: String,
|
||||||
pub sell_in: i32,
|
sell_in: i32,
|
||||||
pub quality: i32,
|
quality: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Item {
|
impl Item {
|
||||||
pub fn new(name: impl Into<String>, sell_in: i32, quality: i32) -> Item {
|
pub fn new(name: impl Into<String>, sell_in: i32, quality: i32) -> Self {
|
||||||
Item {
|
Self {
|
||||||
name: name.into(),
|
name: name.into(),
|
||||||
sell_in,
|
sell_in,
|
||||||
quality,
|
quality,
|
||||||
@ -22,64 +28,67 @@ impl Display for Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct GildedRose {
|
pub struct GildedRose {
|
||||||
pub items: Vec<Item>,
|
items: Vec<Item>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GildedRose {
|
impl GildedRose {
|
||||||
pub fn new(items: Vec<Item>) -> GildedRose {
|
pub fn new(items: Vec<Item>) -> Self {
|
||||||
GildedRose { items }
|
Self { items }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_quality(&mut self) {
|
pub fn update_quality(&mut self) {
|
||||||
for item in &mut self.items {
|
for item in &mut self.items {
|
||||||
if item.name != "Aged Brie" && item.name != "Backstage passes to a TAFKAL80ETC concert"
|
match item.name.as_str() {
|
||||||
{
|
"Aged Brie" => self.update_aged_brie(item),
|
||||||
if item.quality > 0 {
|
"Backstage passes to a TAFKAL80ETC concert" => {
|
||||||
if item.name != "Sulfuras, Hand of Ragnaros" {
|
self.update_backstage_passes(item)
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
"Sulfuras, Hand of Ragnaros" => {}
|
||||||
|
_ => self.update_normal_item(item),
|
||||||
}
|
}
|
||||||
|
self.update_sell_in(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if item.name != "Sulfuras, Hand of Ragnaros" {
|
fn update_aged_brie(&mut self, item: &mut Item) {
|
||||||
item.sell_in = item.sell_in - 1;
|
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 {
|
fn update_backstage_passes(&mut self, item: &mut Item) {
|
||||||
if item.name != "Aged Brie" {
|
if item.quality < 50 {
|
||||||
if item.name != "Backstage passes to a TAFKAL80ETC concert" {
|
item.quality += 1;
|
||||||
if item.quality > 0 {
|
if item.sell_in < 11 && item.quality < 50 {
|
||||||
if item.name != "Sulfuras, Hand of Ragnaros" {
|
item.quality += 1;
|
||||||
item.quality = item.quality - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
item.quality = item.quality - item.quality;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if item.quality < 50 {
|
|
||||||
item.quality = 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", 15, 20),
|
||||||
Item::new("Backstage passes to a TAFKAL80ETC concert", 10, 49),
|
Item::new("Backstage passes to a TAFKAL80ETC concert", 10, 49),
|
||||||
Item::new("Backstage passes to a TAFKAL80ETC concert", 5, 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),
|
Item::new("Conjured Mana Cake", 3, 6),
|
||||||
];
|
];
|
||||||
let mut rose = GildedRose::new(items);
|
let mut rose = GildedRose::new(items);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user