diff --git a/exercises/gilded-rose/.meta/solutions/gilded_rose.rb b/exercises/gilded-rose/.meta/solutions/gilded_rose.rb new file mode 100644 index 0000000000..8c5c187750 --- /dev/null +++ b/exercises/gilded-rose/.meta/solutions/gilded_rose.rb @@ -0,0 +1,95 @@ +class GildedRose + attr_reader :items + def initialize(items) + @items = items + end + + def update_quality + @items.each do |item| + case item.name + when /Aged/ + Aged.new(item).day + when /Sulfuras/ + item + when /Backstage passes/ + Backstage.new(item).day + when /Conjured/ + Conjured.new(item).day + else + Normal.new(item).day + end + 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 + +class Normal + attr_reader :item + def initialize(item) + @item = item + end + + def day + item.quality -= 1 + item.quality -= 1 if item.sell_in <= 0 + item.quality = 0 if item.quality < 0 + item.sell_in -= 1 + end +end + +class Conjured + attr_reader :item + def initialize(item) + @item = item + end + + def day + item.quality -= 2 + item.quality -= 2 if item.sell_in <= 0 + item.quality = 0 if item.quality < 0 + item.sell_in -= 1 + end +end +require 'pry' +class Aged + attr_reader :item + def initialize(item) + @item = item + end + + def day + item.quality += 1 + item.quality += 1 if item.sell_in <= 0 + item.quality = 50 if item.quality > 50 + item.sell_in -= 1 + end +end + +class Backstage + attr_reader :item + def initialize(item) + @item = item + end + + def day + item.quality += 1 + item.quality += 1 if item.sell_in <= 10 + item.quality += 1 if item.sell_in <= 5 + item.quality = 50 if item.quality > 50 + item.quality = 0 if item.sell_in <= 0 + item.sell_in -= 1 + end +end \ No newline at end of file diff --git a/exercises/gilded-rose/README.md b/exercises/gilded-rose/README.md new file mode 100644 index 0000000000..9b57433da9 --- /dev/null +++ b/exercises/gilded-rose/README.md @@ -0,0 +1,80 @@ +# Gilded Rose + +Hi and welcome to team Gilded Rose. As you know, we are a small inn +with a prime location in a prominent city run by a friendly innkeeper +named Allison. We also buy and sell only the finest +goods. Unfortunately, our goods are constantly degrading in quality as +they approach their sell by date. We have a system in place that +updates our inventory for us. It was developed by a no-nonsense type +named Leeroy, who has moved on to new adventures. Your task is to add +the new feature to our system so that we can begin selling a new +category of items. First an introduction to our system: + +- All items have a SellIn value which denotes the number of days we + have to sell the item +- All items have a Quality value which denotes how valuable the item + is +- At the end of each day our system lowers both values for every item + +Pretty simple, right? Well this is where it gets interesting: + + - Once the sell by date has passed, Quality degrades twice as fast + - The Quality of an item is never negative + - "Aged Brie" actually increases in Quality the older it gets + - The Quality of an item is never more than 50 + - "Sulfuras", being a legendary item, never has to be sold or + decreases in Quality + - "Backstage passes", like aged brie, increases in Quality as it's + SellIn value approaches; Quality increases by 2 when there are 10 + days or less and by 3 when there are 5 days or less but Quality + drops to 0 after the concert + +We have recently signed a supplier of conjured items. This requires an update to our system: + +- "Conjured" items degrade in Quality twice as fast as normal items + +Feel free to make any changes to the UpdateQuality method and add any +new code as long as everything still works correctly. However, do not +alter the Item class or Items property as those belong to the goblin +in the corner who will insta-rage and one-shot you as he doesn't +believe in shared code ownership (you can make the UpdateQuality +method and Items property static if you like, we'll cover for +you). Your work needs to be completed by Friday, February 18, 2011 +08:00:00 AM PST. + +Just for clarification, an item can never have its Quality increase +above 50, however "Sulfuras" is a legendary item and as such its +Quality is 80 and it never alters. + +* * * * + +For installation and learning resources, refer to the +[exercism help page](http://exercism.io/languages/ruby). + +For running the tests provided, you will need the Minitest gem. Open a +terminal window and run the following command to install minitest: + + gem install minitest + +If you would like color output, you can `require 'minitest/pride'` in +the test file, or note the alternative instruction, below, for running +the test file. + +In order to run the test, you can run the test file from the exercise +directory. For example, if the test suite is called +`hello_world_test.rb`, you can run the following command: + + ruby hello_world_test.rb + +To include color from the command line: + + ruby -r minitest/pride hello_world_test.rb + + +## Source + +[the original kata article](http://iamnotmyself.com/2011/02/13/refactor-this-the-gilded-rose-kata/) +[Jim Weirich's implementation in Ruby](https://github.com/jimweirich/gilded_rose_kata) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. \ No newline at end of file diff --git a/exercises/gilded-rose/gilded_rose.rb b/exercises/gilded-rose/gilded_rose.rb new file mode 100644 index 0000000000..4eca9d65f7 --- /dev/null +++ b/exercises/gilded-rose/gilded_rose.rb @@ -0,0 +1,62 @@ +def update_quality(items) + items.each do |item| + 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 -= 1 + end + end + else + if item.quality < 50 + item.quality += 1 + if item.name == 'Backstage passes to a TAFKAL80ETC concert' + if item.sell_in < 11 + if item.quality < 50 + item.quality += 1 + end + end + if item.sell_in < 6 + if item.quality < 50 + item.quality += 1 + end + end + end + end + end + if item.name != 'Sulfuras, Hand of Ragnaros' + item.sell_in -= 1 + end + 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 -= 1 + end + end + else + item.quality = item.quality - item.quality + end + else + if item.quality < 50 + item.quality += 1 + end + end + end + end +end + +# DO NOT CHANGE THINGS BELOW ----------------------------------------- + +Item = Struct.new(:name, :sell_in, :quality) + +# We use the setup in the spec rather than the following for testing. +# +# Items = [ +# Item.new("+5 Dexterity Vest", 10, 20), +# Item.new("Aged Brie", 2, 0), +# Item.new("Elixir of the Mongoose", 5, 7), +# Item.new("Sulfuras, Hand of Ragnaros", 0, 80), +# Item.new("Backstage passes to a TAFKAL80ETC concert", 15, 20), +# Item.new("Conjured Mana Cake", 3, 6), +# ] \ No newline at end of file diff --git a/exercises/gilded-rose/gilded_rose_test.rb b/exercises/gilded-rose/gilded_rose_test.rb new file mode 100644 index 0000000000..e69de29bb2