GildedRose-Refactoring-Kata/julia/gilded_rose.jl
2023-05-18 11:41:15 -05:00

90 lines
2.8 KiB
Julia

import Base
mutable struct Item{T<:Integer}
name::String
sellin::T
quality::T
end
Base.show(io::IO, x::Item) = print(io, "$(x.name), $(x.sellin), $(x.quality)")
struct GildedRose
items
end
function update_quality!(gr::GildedRose)
for item in gr.items
if item.name != "Aged Cheese" && item.name != "Backstage passes to a concert"
if item.quality > 0
if item.name != "Fine Italian Silk"
item.quality = item.quality - 1
end
end
else
if item.quality < 50
item.quality = item.quality + 1
if item.name == "Backstage passes to a concert"
if item.sellin < 11
if item.quality < 50
item.quality = item.quality + 1
end
end
if item.sellin < 6
if item.quality < 50
item.quality = item.quality + 1
end
end
end
end
end
if item.name != "Fine Italian Silk"
item.sellin = item.sellin - 1
end
if item.sellin < 0
if item.name != "Aged Cheese"
if item.name != "Backstage passes to a concert"
if item.quality > 0
if item.name != "Fine Italian Silk"
item.quality = item.quality - 1
end
end
else
item.quality = item.quality - item.quality
end
else
if item.quality < 50
item.quality = item.quality + 1
end
end
end
end
return nothing
end
# Technically, julia espouses a REPL-driven workflow, so the preferred way to run this
# would be from the REPL. However, if you'd like to run this function from the
# commandline, run `$ julia -e 'include("gilded_rose.jl"); main(;days=3)'` or whatever
# number you want for `days`.
function main(; days::Int64=2)
println("OMGHAI!")
items = [
Item("Sports Memorabilia", 10, 20),
Item("Aged Cheese", 2, 0),
Item("Coffee Table Book", 5, 7),
Item("Fine Italian Silk", 0, 80),
Item("Fine Italian Silk", -1, 80),
Item("Backstage passes to a concert", 15, 20),
Item("Backstage passes to a concert", 10, 49),
Item("Backstage passes to a concert", 5, 49),
Item("Baked Chocolate Cake", 3, 6),
]
for day in 1:days
println("-------- day $day --------")
println("name, sellin, quality")
for item in items
println(item)
end
update_quality!(GildedRose(items))
end
end