Last active
November 1, 2020 10:13
-
-
Save ytnk531/81ca240500a6fe1dd9b2e017216ad7f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GildedRose | |
NAME_SULFURAS = 'Sulfuras, Hand of Ragnaros'.freeze | |
NAME_BRIE = 'Aged Brie'.freeze | |
NAME_PASS = 'Backstage passes to a TAFKAL80ETC concert'.freeze | |
MAX_QUALITY = 50 | |
def initialize(items) | |
@items = items | |
end | |
def update_quality | |
@items.each do |item| | |
next if item.name == NAME_SULFURAS | |
item.quality = calc_quality(item) | |
item.sell_in -= 1 | |
item.quality = calc_quality_of_over_sell_in(item) if item.sell_in.negative? | |
end | |
end | |
def calc_quality_of_over_sell_in(item) | |
return 0 if item.name == NAME_PASS | |
return plus_quality item, 1 if item.name == NAME_BRIE | |
minus_quality(item, 1) | |
end | |
def calc_quality(item) | |
case item.name | |
when NAME_BRIE | |
plus_quality item, 1 | |
when NAME_PASS | |
pass_quality item | |
else | |
return item.quality unless item.quality.positive? | |
minus_quality item, 1 | |
end | |
end | |
def plus_quality(item, quality) | |
[item.quality + quality, MAX_QUALITY].min | |
end | |
def minus_quality(item, quality) | |
[item.quality - quality, 0].max | |
end | |
def pass_quality(pass) | |
if pass.sell_in < 6 | |
plus_quality(pass, 3) | |
elsif pass.sell_in < 11 | |
plus_quality(pass, 2) | |
else | |
plus_quality(pass, 1) | |
end | |
end | |
end | |
class Item | |
attr_accessor :name, :sell_in, :quality | |
def initialize(name, sell_in, quality) | |
@name = name | |
@sell_in = sell_in | |
@quality = quality | |
end | |
def to_s | |
"#{@name}, #{@sell_in}, #{@quality}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment