mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-04 09:11:39 +00:00
Refactor code structure and implement Conjured items support
Refactoring improvements:
- Extract complex nested if-else logic into well-named private methods
- Separate concerns: each item type has its own update method
- Use constants for item names and quality limits
- Add comprehensive docstrings for all methods
- Improve code readability and maintainability
Conjured items implementation:
- Conjured items degrade in quality twice as fast as normal items
- After sell-by date, Conjured items degrade 4x faster (2x normal rate * 2x after sell-by)
- Quality never goes below 0
- Uses prefix matching for flexibility ('Conjured' prefix)
All tests pass, including the new Conjured items tests.
This commit is contained in:
parent
116dc702dd
commit
c851057d59
@ -1,42 +1,114 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
|
||||||
class GildedRose(object):
|
class GildedRose(object):
|
||||||
|
"""Manages the inventory of items at the Gilded Rose inn."""
|
||||||
|
|
||||||
|
# Item type constants
|
||||||
|
AGED_BRIE = "Aged Brie"
|
||||||
|
BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert"
|
||||||
|
SULFURAS = "Sulfuras, Hand of Ragnaros"
|
||||||
|
CONJURED_PREFIX = "Conjured"
|
||||||
|
|
||||||
|
# Quality constraints
|
||||||
|
MAX_QUALITY = 50
|
||||||
|
MIN_QUALITY = 0
|
||||||
|
SULFURAS_QUALITY = 80
|
||||||
|
|
||||||
def __init__(self, items):
|
def __init__(self, items):
|
||||||
self.items = items
|
self.items = items
|
||||||
|
|
||||||
def update_quality(self):
|
def update_quality(self):
|
||||||
|
"""Updates the quality and sell_in values for all items."""
|
||||||
for item in self.items:
|
for item in self.items:
|
||||||
if item.name != "Aged Brie" and item.name != "Backstage passes to a TAFKAL80ETC concert":
|
self._update_item(item)
|
||||||
if item.quality > 0:
|
|
||||||
if item.name != "Sulfuras, Hand of Ragnaros":
|
def _update_item(self, item):
|
||||||
item.quality = item.quality - 1
|
"""Updates a single item's quality and sell_in values."""
|
||||||
else:
|
if self._is_sulfuras(item):
|
||||||
if item.quality < 50:
|
# Sulfuras never changes
|
||||||
item.quality = item.quality + 1
|
return
|
||||||
if item.name == "Backstage passes to a TAFKAL80ETC concert":
|
|
||||||
if item.sell_in < 11:
|
# Update sell_in date
|
||||||
if item.quality < 50:
|
item.sell_in -= 1
|
||||||
item.quality = item.quality + 1
|
|
||||||
if item.sell_in < 6:
|
# Update quality based on item type
|
||||||
if item.quality < 50:
|
if self._is_aged_brie(item):
|
||||||
item.quality = item.quality + 1
|
self._update_aged_brie(item)
|
||||||
if item.name != "Sulfuras, Hand of Ragnaros":
|
elif self._is_backstage_passes(item):
|
||||||
item.sell_in = item.sell_in - 1
|
self._update_backstage_passes(item)
|
||||||
if item.sell_in < 0:
|
elif self._is_conjured(item):
|
||||||
if item.name != "Aged Brie":
|
self._update_conjured_item(item)
|
||||||
if item.name != "Backstage passes to a TAFKAL80ETC concert":
|
else:
|
||||||
if item.quality > 0:
|
self._update_normal_item(item)
|
||||||
if item.name != "Sulfuras, Hand of Ragnaros":
|
|
||||||
item.quality = item.quality - 1
|
def _is_sulfuras(self, item):
|
||||||
else:
|
"""Check if item is Sulfuras."""
|
||||||
item.quality = item.quality - item.quality
|
return item.name == self.SULFURAS
|
||||||
else:
|
|
||||||
if item.quality < 50:
|
def _is_aged_brie(self, item):
|
||||||
item.quality = item.quality + 1
|
"""Check if item is Aged Brie."""
|
||||||
|
return item.name == self.AGED_BRIE
|
||||||
|
|
||||||
|
def _is_backstage_passes(self, item):
|
||||||
|
"""Check if item is Backstage passes."""
|
||||||
|
return item.name == self.BACKSTAGE_PASSES
|
||||||
|
|
||||||
|
def _is_conjured(self, item):
|
||||||
|
"""Check if item is a Conjured item."""
|
||||||
|
return item.name.startswith(self.CONJURED_PREFIX)
|
||||||
|
|
||||||
|
def _update_normal_item(self, item):
|
||||||
|
"""Update quality for normal items."""
|
||||||
|
degradation_rate = self._get_degradation_rate(item)
|
||||||
|
self._decrease_quality(item, degradation_rate)
|
||||||
|
|
||||||
|
def _update_conjured_item(self, item):
|
||||||
|
"""Update quality for Conjured items (degrade twice as fast)."""
|
||||||
|
degradation_rate = self._get_degradation_rate(item) * 2
|
||||||
|
self._decrease_quality(item, degradation_rate)
|
||||||
|
|
||||||
|
def _update_aged_brie(self, item):
|
||||||
|
"""Update quality for Aged Brie (increases with age)."""
|
||||||
|
self._increase_quality(item)
|
||||||
|
if self._is_past_sell_by_date(item):
|
||||||
|
self._increase_quality(item)
|
||||||
|
|
||||||
|
def _update_backstage_passes(self, item):
|
||||||
|
"""Update quality for Backstage passes."""
|
||||||
|
if self._is_past_sell_by_date(item):
|
||||||
|
# Quality drops to 0 after the concert
|
||||||
|
item.quality = 0
|
||||||
|
else:
|
||||||
|
self._increase_quality(item)
|
||||||
|
if item.sell_in < 10:
|
||||||
|
self._increase_quality(item)
|
||||||
|
if item.sell_in < 5:
|
||||||
|
self._increase_quality(item)
|
||||||
|
|
||||||
|
def _get_degradation_rate(self, item):
|
||||||
|
"""Get the degradation rate based on sell_in date."""
|
||||||
|
if self._is_past_sell_by_date(item):
|
||||||
|
return 2 # Degrades twice as fast after sell by date
|
||||||
|
return 1 # Normal degradation rate
|
||||||
|
|
||||||
|
def _is_past_sell_by_date(self, item):
|
||||||
|
"""Check if item is past its sell by date."""
|
||||||
|
return item.sell_in < 0
|
||||||
|
|
||||||
|
def _increase_quality(self, item):
|
||||||
|
"""Increase item quality by 1, respecting maximum quality cap."""
|
||||||
|
if item.quality < self.MAX_QUALITY:
|
||||||
|
item.quality += 1
|
||||||
|
|
||||||
|
def _decrease_quality(self, item, amount):
|
||||||
|
"""Decrease item quality by specified amount, respecting minimum quality."""
|
||||||
|
item.quality = max(self.MIN_QUALITY, item.quality - amount)
|
||||||
|
|
||||||
|
|
||||||
class Item:
|
class Item:
|
||||||
|
"""Represents an item in the inventory."""
|
||||||
|
|
||||||
def __init__(self, name, sell_in, quality):
|
def __init__(self, name, sell_in, quality):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.sell_in = sell_in
|
self.sell_in = sell_in
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user