Merge pull request #1 from DoggyDoggyDoggy/refactor

Refactor
This commit is contained in:
Denys 2025-05-11 14:52:11 +12:00 committed by GitHub
commit d78d1ea7a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 70 additions and 56 deletions

View File

@ -1,58 +1,68 @@
package com.gildedrose
import com.gildedrose.data.model.Item
class GildedRose(var items: List<Item>) {
fun updateQuality() {
for (i in items.indices) {
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
}
}
if (
items[i].name != "Aged Brie" &&
items[i].name != "Backstage passes to a TAFKAL80ETC concert" &&
items[i].name != "Sulfuras, Hand of Ragnaros"
) {
downGradeQuality(i)
} else {
if (items[i].quality < 50) {
items[i].quality = items[i].quality + 1
upgradeQuality(i)
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].name == "Backstage passes to a TAFKAL80ETC concert") {
when {
items[i].sellIn < 6 -> {
upgradeQuality(i)
upgradeQuality(i)
}
if (items[i].sellIn < 6) {
if (items[i].quality < 50) {
items[i].quality = items[i].quality + 1
}
items[i].sellIn < 11 -> {
upgradeQuality(i)
}
}
}
}
if (items[i].name != "Sulfuras, Hand of Ragnaros") {
items[i].sellIn = items[i].sellIn - 1
}
sellItem(i)
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
when (items[i].name) {
"Aged Brie" -> {
upgradeQuality(i)
}
} else {
if (items[i].quality < 50) {
items[i].quality = items[i].quality + 1
"Backstage passes to a TAFKAL80ETC concert" -> {
items[i].quality -= items[i].quality
}
else -> {
if (items[i].name != "Sulfuras, Hand of Ragnaros") {
downGradeQuality(i)
}
}
}
}
}
}
private fun sellItem(i: Int) {
if (items[i].name != "Sulfuras, Hand of Ragnaros") items[i].sellIn -= 1
}
private fun downGradeQuality(i: Int) {
if (items[i].quality > 0) items[i].quality -= 1
}
private fun upgradeQuality(i: Int) {
if (items[i].quality < 50) items[i].quality += 1
}
}

View File

@ -1,8 +0,0 @@
package com.gildedrose
//Check git works
open class Item(var name: String, var sellIn: Int, var quality: Int) {
override fun toString(): String {
return this.name + ", " + this.sellIn + ", " + this.quality
}
}

View File

@ -1,24 +1,14 @@
package com.gildedrose
import com.gildedrose.data.constants.ItemRepository
//asd
fun main(args: Array<String>) {
println("OMGHAI!")
val items = listOf(
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("Sulfuras, Hand of Ragnaros", -1, 80),
Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
// this conjured item does not work properly yet
Item("Conjured Mana Cake", 3, 6)
)
val app = GildedRose(items)
val app = GildedRose(ItemRepository.items)
var days = 2
if (args.size > 0) {
@ -28,8 +18,8 @@ fun main(args: Array<String>) {
for (i in 0..days - 1) {
println("-------- day $i --------")
println("name, sellIn, quality")
for (item in items) {
println(item)
for (item in app.items) {
println("${item.name}, ${item.sellIn}, ${item.quality}")
}
println()
app.updateQuality()

View File

@ -0,0 +1,18 @@
package com.gildedrose.data.constants
import com.gildedrose.data.model.Item
object ItemRepository {
val items = listOf(
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("Sulfuras, Hand of Ragnaros", -1, 80),
Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
// this conjured item does not work properly yet
Item("Conjured Mana Cake", 3, 6)
)
}

View File

@ -0,0 +1,3 @@
package com.gildedrose.data.model
data class Item(var name: String, var sellIn: Int, var quality: Int)

View File

@ -1,5 +1,6 @@
package com.gildedrose
import com.gildedrose.data.model.Item
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test