Skip to content

Commit c8d6a70

Browse files
aacciolyO-Link06
authored andcommitted
Refactor backstage pass quality and sellIn update logic
1 parent 4ba7e25 commit c8d6a70

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

Java/src/main/java/com/gildedrose/GildedRose.java

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,25 @@ private void updateItem(Item item) {
4343
item.quality += qualityDelta;
4444
}
4545
return;
46-
}
47-
48-
if (!isBackStagePass(item.name)) {
49-
if (item.quality > 0) {
50-
item.quality = item.quality - 1;
51-
}
52-
} else {
53-
if (item.quality < 50) {
54-
item.quality = item.quality + 1;
55-
56-
if (item.sellIn < 11) {
57-
if (item.quality < 50) {
58-
item.quality = item.quality + 1;
59-
}
60-
if (item.sellIn < 6) {
61-
if (item.quality < 50) {
62-
item.quality = item.quality + 1;
63-
}
64-
}
46+
} else if (isBackStagePass(item.name)) {
47+
item.sellIn--;
48+
if (item.sellIn < 0) {
49+
item.quality = 0;
50+
} else {
51+
if (item.quality < 50) {
52+
var qualityDelta = item.sellIn < 5 ? 3 : item.sellIn < 10 ? 2 : 1;
53+
item.quality += qualityDelta;
6554
}
6655
}
56+
return;
6757
}
6858

6959
// all items except Sulfuras -> decrease sellIn
7060
item.sellIn = item.sellIn - 1;
7161

72-
if (item.sellIn < 0) {
73-
// normal items -> quality decreases twice as fast after expiration date
74-
if (!isBackStagePass(item.name)) {
75-
if (item.quality > 0) {
76-
item.quality = item.quality - 1;
77-
}
78-
} else {
79-
item.quality = 0;
80-
}
62+
if (item.quality > 0) {
63+
var qualityDelta = item.sellIn < 0 ? 2 : 1;
64+
item.quality = item.quality - qualityDelta;
8165
}
8266
}
8367
}

Java/src/test/java/com/gildedrose/GildedRoseTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,32 @@ class BackstagePass {
101101
void isBackstagePass() {
102102
assertThat(GildedRose.isBackStagePass(backStagePass.name)).isTrue();
103103
}
104+
105+
@DisplayName("quality increases by 1 when sellIn is greater than 10")
106+
@Test
107+
void qualityIncreasesBy1WhenSellInIsGreaterThan10() {
108+
GildedRose app = createGildedRose(backStagePass);
109+
app.updateQuality();
110+
assertThat(backStagePass.quality).isEqualTo(11);
111+
}
112+
113+
@DisplayName("quality increases by 2 when sellIn is between 10 and 6")
114+
@Test
115+
void qualityIncreasesBy2WhenSellInIsBetween10And6() {
116+
backStagePass.sellIn = 10;
117+
GildedRose app = createGildedRose(backStagePass);
118+
app.updateQuality();
119+
assertThat(backStagePass.quality).isEqualTo(12);
120+
}
121+
122+
@DisplayName("quality increases by 3 when sellIn is between 5 and 0")
123+
@Test
124+
void qualityIncreasesBy3WhenSellInIsBetween5And0() {
125+
backStagePass.sellIn = 5;
126+
GildedRose app = createGildedRose(backStagePass);
127+
app.updateQuality();
128+
assertThat(backStagePass.quality).isEqualTo(13);
129+
}
104130
}
105131

106132
public GildedRose createGildedRose(Item item) {

0 commit comments

Comments
 (0)