mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2025-12-12 04:12:13 +00:00
Merge pull request #47 from lermannen/elixir
Proposed fix for #34: Implement a working version of the original code for the kata
This commit is contained in:
commit
fafdb067bb
@ -1,48 +1,83 @@
|
|||||||
defmodule GildedRose do
|
defmodule GildedRose do
|
||||||
# Example
|
# Example
|
||||||
# update_quality([%Item{name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 9, quality: 1}])
|
# update_quality([%Item{name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 9, quality: 1}])
|
||||||
# => [%Item{name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 9, quality: 3}]
|
# => [%Item{name: "Backstage passes to a TAFKAL80ETC concert", sell_in: 8, quality: 3}]
|
||||||
|
|
||||||
def update_quality(items) do
|
def update_quality(items) do
|
||||||
Enum.map(items, &update_item/1)
|
Enum.map(items, &update_item/1)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_item(item) do
|
def update_item(item) do
|
||||||
cond do
|
item = cond do
|
||||||
item.quality == 0 ->
|
item.name != "Aged Brie" && item.name != "Backstage passes to a TAFKAL80ETC concert" ->
|
||||||
item
|
if item.quality > 0 do
|
||||||
item.sell_in < 0 && item.name == "Backstage passes to a TAFKAL80ETC concert" ->
|
if item.name != "Sulfuras, Hand of Ragnaros" do
|
||||||
%{item | quality: 0}
|
|
||||||
item.name == "Aged Brie" || item.name == "Backstage passes to a TAFKAL80ETC concert" ->
|
|
||||||
if item.name == "Backstage passes to a TAFKAL80ETC concert" && item.sell_in > 5 && item.sell_in <= 10 do
|
|
||||||
%{item | quality: item.quality + 2}
|
|
||||||
else
|
|
||||||
if item.name == "Backstage passes to a TAFKAL80ETC concert" && item.sell_in >= 0 && item.sell_in <= 5 do
|
|
||||||
%{item | quality: item.quality + 3}
|
|
||||||
else
|
|
||||||
if item.quality < 50 do
|
|
||||||
%{item | quality: item.quality + 1}
|
|
||||||
else
|
|
||||||
item
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
item.sell_in < 0 ->
|
|
||||||
if item.name == "Backstage passes to a TAFKAL80ETC concert" do
|
|
||||||
%{item | quality: 0}
|
|
||||||
else
|
|
||||||
if item.name == "+5 Dexterity Vest" || item.name == "Elixir of the Mongoose" do
|
|
||||||
%{item | quality: item.quality - 2}
|
|
||||||
else
|
|
||||||
item
|
|
||||||
end
|
|
||||||
end
|
|
||||||
item.name == "+5 Dexterity Vest" || item.name == "Elixir of the Mongoose" ->
|
|
||||||
%{item | quality: item.quality - 1}
|
%{item | quality: item.quality - 1}
|
||||||
|
else
|
||||||
|
item
|
||||||
|
end
|
||||||
|
else
|
||||||
|
item
|
||||||
|
end
|
||||||
|
true ->
|
||||||
|
cond do
|
||||||
|
item.quality < 50 ->
|
||||||
|
item = %{item | quality: item.quality + 1}
|
||||||
|
cond do
|
||||||
|
item.name == "Backstage passes to a TAFKAL80ETC concert" ->
|
||||||
|
item = cond do
|
||||||
|
item.sell_in < 11 ->
|
||||||
|
cond do
|
||||||
|
item.quality < 50 ->
|
||||||
|
%{item | quality: item.quality + 1}
|
||||||
|
true -> item
|
||||||
|
end
|
||||||
|
true -> item
|
||||||
|
end
|
||||||
|
cond do
|
||||||
|
item.sell_in < 6 ->
|
||||||
|
cond do
|
||||||
|
item.quality < 50 ->
|
||||||
|
%{item | quality: item.quality + 1}
|
||||||
|
true -> item
|
||||||
|
end
|
||||||
|
true -> item
|
||||||
|
end
|
||||||
|
true -> item
|
||||||
|
end
|
||||||
|
true -> item
|
||||||
|
end
|
||||||
|
end
|
||||||
|
item = cond do
|
||||||
|
item.name != "Sulfuras, Hand of Ragnaros" ->
|
||||||
|
%{item | sell_in: item.sell_in - 1}
|
||||||
|
true -> item
|
||||||
|
end
|
||||||
|
cond do
|
||||||
|
item.sell_in < 0 ->
|
||||||
|
cond do
|
||||||
|
item.name != "Aged Brie" ->
|
||||||
|
cond do
|
||||||
|
item.name != "Backstage passes to a TAFKAL80ETC concert" ->
|
||||||
|
cond do
|
||||||
|
item.quality > 0 ->
|
||||||
|
cond do
|
||||||
item.name != "Sulfuras, Hand of Ragnaros" ->
|
item.name != "Sulfuras, Hand of Ragnaros" ->
|
||||||
%{item | quality: item.quality - 1}
|
%{item | quality: item.quality - 1}
|
||||||
|
true -> item
|
||||||
|
end
|
||||||
|
true -> item
|
||||||
|
end
|
||||||
|
true -> %{item | quality: item.quality - item.quality}
|
||||||
|
end
|
||||||
true ->
|
true ->
|
||||||
item
|
cond do
|
||||||
|
item.quality < 50 ->
|
||||||
|
%{item | quality: item.quality + 1}
|
||||||
|
true -> item
|
||||||
|
end
|
||||||
|
end
|
||||||
|
true -> item
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user