refactor: name never changes once an item is created

This commit is contained in:
Kadir Sirimsi 2025-02-10 12:09:41 +01:00
parent 4698468083
commit 6564125ba3
No known key found for this signature in database
GPG Key ID: A21C0144C2D2A134
3 changed files with 22 additions and 13 deletions

View File

@ -9,10 +9,10 @@ class GildedRose {
public void updateQuality() {
for (Item item : items) {
if (!item.name.equals("Aged Brie")
&& !item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
if (!item.getName().equals("Aged Brie")
&& !item.getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
if (item.quality > 0) {
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
if (!item.getName().equals("Sulfuras, Hand of Ragnaros")) {
item.quality = item.quality - 1;
}
}
@ -20,7 +20,7 @@ class GildedRose {
if (item.quality < 50) {
item.quality = item.quality + 1;
if (item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
if (item.getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
if (item.sellIn < 11) {
if (item.quality < 50) {
item.quality = item.quality + 1;
@ -36,15 +36,15 @@ class GildedRose {
}
}
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
if (!item.getName().equals("Sulfuras, Hand of Ragnaros")) {
item.sellIn = item.sellIn - 1;
}
if (item.sellIn < 0) {
if (!item.name.equals("Aged Brie")) {
if (!item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
if (!item.getName().equals("Aged Brie")) {
if (!item.getName().equals("Backstage passes to a TAFKAL80ETC concert")) {
if (item.quality > 0) {
if (!item.name.equals("Sulfuras, Hand of Ragnaros")) {
if (!item.getName().equals("Sulfuras, Hand of Ragnaros")) {
item.quality = item.quality - 1;
}
}

View File

@ -2,7 +2,7 @@ package com.gildedrose;
public class Item {
public String name;
private final String name;
public int sellIn;
@ -14,6 +14,10 @@ public class Item {
this.quality = quality;
}
public String getName() {
return name;
}
@Override
public String toString() {
return this.name + ", " + this.sellIn + ", " + this.quality;

View File

@ -7,11 +7,16 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
class GildedRoseTest {
@Test
void foo() {
Item[] items = new Item[] { new Item("foo", 0, 0) };
GildedRose app = new GildedRose(items);
void givenListOfItems_whenUpdateQuality_thenNameRemainsUnchanged() {
final var name = "foo";
var items = new Item[] { new Item(name, 0, 0) };
var app = new GildedRose(items);
app.updateQuality();
assertEquals("foo", app.items[0].name);
assertEquals(name, app.items[0].getName());
}
}