diff --git a/go/gilded-rose_test.go b/go/gilded-rose_test.go deleted file mode 100644 index 6b03bf25..00000000 --- a/go/gilded-rose_test.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "testing" - -func Test_GildedRose(t *testing.T) { - main() -} diff --git a/go/gilded_rose.go b/go/gilded_rose.go new file mode 100644 index 00000000..89e54a18 --- /dev/null +++ b/go/gilded_rose.go @@ -0,0 +1,90 @@ +package main + +import "fmt" + +type GildedRose struct { + Items []Item +} + +type Item struct { + Name string + SellIn, Quality int +} + +func main() { + rose := GildedRose{ + []Item{ + Item{"+5 Dexterity Vest", 10, 20}, + Item{"Aged Brie", 2, 0}, + Item{"Elixir of the Mongoose", 5, 7}, + Item{"Sulfuras, Hand of Ragnaros", 0, 80}, + Item{"Backstage passes to a TAFKAL80ETC concert", 15, 20}, + Item{"Conjured Mana Cake", 3, 6}, + }, + } + rose.UpdateQuality() + fmt.Print(rose.String()) +} + +func (gr *GildedRose) UpdateQuality() { + + for i := 0; i < len(gr.Items); i++ { + item := &gr.Items[i] + + 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 = item.Quality - 1 + } + } + } else { + if item.Quality < 50 { + item.Quality = item.Quality + 1 + if item.Name == "Backstage passes to a TAFKAL80ETC concert" { + if item.SellIn < 11 { + if item.Quality < 50 { + item.Quality = item.Quality + 1 + } + } + if item.SellIn < 6 { + if item.Quality < 50 { + item.Quality = item.Quality + 1 + } + } + } + } + } + + if item.Name != "Sulfuras, Hand of Ragnaros" { + item.SellIn = item.SellIn - 1 + } + + if item.SellIn < 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 = item.Quality - 1 + } + } + } else { + item.Quality = item.Quality - item.Quality + } + } else { + if item.Quality < 50 { + item.Quality = item.Quality + 1 + } + } + } + } + +} + +func (gr *GildedRose) String() string { + var str string + str += "=== The Gilded Rose ===\n" + for i, item := range gr.Items { + str += fmt.Sprintf("%d) %s (quality %d, sell in %d)\n", i+1, item.Name, item.Quality, item.SellIn) + } + return str +} diff --git a/go/gilded_rose_test.go b/go/gilded_rose_test.go new file mode 100644 index 00000000..ea203f68 --- /dev/null +++ b/go/gilded_rose_test.go @@ -0,0 +1,12 @@ +package main + +import "testing" + +func Test_GildedRose_UpdateQuality_DoesNotChangeItemName(t *testing.T) { + rose := GildedRose{[]Item{Item{"Foo", 0, 0}}} + expectedName := "Fixme" + rose.UpdateQuality() + if rose.Items[0].Name != expectedName { + t.Errorf("Expected Item name to be %#v (got %#v)", expectedName, rose.Items[0].Name) + } +} diff --git a/go/glided-rose.go b/go/glided-rose.go deleted file mode 100644 index 3aac5d37..00000000 --- a/go/glided-rose.go +++ /dev/null @@ -1,75 +0,0 @@ -package main - -import "fmt" - -type Item struct { - name string - sellIn, quality int -} - -var items = []Item{ - Item{"+5 Dexterity Vest", 10, 20}, - Item{"Aged Brie", 2, 0}, - Item{"Elixir of the Mongoose", 5, 7}, - Item{"Sulfuras, Hand of Ragnaros", 0, 80}, - Item{"Backstage passes to a TAFKAL80ETC concert", 15, 20}, - Item{"Conjured Mana Cake", 3, 6}, -} - -func main() { - fmt.Println("OMGHAI!") - // fmt.Print(items) - GlidedRose() -} - -func GlidedRose() { - for i := 0; i < len(items); i++ { - - if items[i].name != "Aged Brie" && items[i].name != "Backstage passes to a TAFKAL80ETC concert" { - if items[i].quality > 0 { - if items[i].name != "Sulfuras, Hand of Ragnaros" { - items[i].quality = items[i].quality - 1 - } - } - } else { - if items[i].quality < 50 { - items[i].quality = items[i].quality + 1 - if items[i].name == "Backstage passes to a TAFKAL80ETC concert" { - if items[i].sellIn < 11 { - if items[i].quality < 50 { - items[i].quality = items[i].quality + 1 - } - } - if items[i].sellIn < 6 { - if items[i].quality < 50 { - items[i].quality = items[i].quality + 1 - } - } - } - } - } - - if items[i].name != "Sulfuras, Hand of Ragnaros" { - items[i].sellIn = items[i].sellIn - 1 - } - - if items[i].sellIn < 0 { - if items[i].name != "Aged Brie" { - if items[i].name != "Backstage passes to a TAFKAL80ETC concert" { - if items[i].quality > 0 { - if items[i].name != "Sulfuras, Hand of Ragnaros" { - items[i].quality = items[i].quality - 1 - } - } - } else { - items[i].quality = items[i].quality - items[i].quality - } - } else { - if items[i].quality < 50 { - items[i].quality = items[i].quality + 1 - } - } - } - } - -} diff --git a/go/readme.md b/go/readme.md index 6b894ed4..29be264e 100644 --- a/go/readme.md +++ b/go/readme.md @@ -1,21 +1,19 @@ -# GO Starter +# Go Starter -- Run : +- Run: ```shell -go run gilded-rose.go +go run gilded_rose.go ``` -- Run tests : +- Run tests: ```shell go test ``` -- Run tests and coverage : +- Run tests and coverage: ```shell -go test -coverprofile=coverage.out - -go tool cover -html=coverage.out -``` \ No newline at end of file +go test -cover +```