Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored code #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Java/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
109 changes: 62 additions & 47 deletions Java/src/main/java/com/gildedrose/GildedRose.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,71 @@ public GildedRose(Item[] items) {
this.items = items;
}

public void updateQuality() {
for (int i = 0; i < items.length; i++) {
if (!items[i].name.equals("Aged Brie")
&& !items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
if (items[i].quality > 0) {
if (!items[i].name.equals("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.equals("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;
}
}
}
}
}
public void updateInventory() {
for (Item item : items) {
itemUpgrade(item);
}
}

private static void itemUpgrade(Item item) {
updateQuality(item);
updateExpiration(item);
if (isExpired(item)) {
processExpired(item);
}
}

if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
items[i].sellIn = items[i].sellIn - 1;
private static void updateQuality(Item item) {
if (item.name.equals("Aged Brie")) {
increaseQuality(item);
} else if (item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
increaseQuality(item);

if (item.sellIn < 11) {
increaseQuality(item);
}

if (items[i].sellIn < 0) {
if (!items[i].name.equals("Aged Brie")) {
if (!items[i].name.equals("Backstage passes to a TAFKAL80ETC concert")) {
if (items[i].quality > 0) {
if (!items[i].name.equals("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;
}
}
if (item.sellIn < 6) {
increaseQuality(item);
}

} else if (item.name.equals("Sulfuras, Hand of Ragnaros")) {
return;
} else decreaseQuality(item);
}

private static void updateExpiration(Item item) {
if (item.name.equals("Sulfuras, Hand of Ragnaros")) {
return;
}
item.sellIn--;
}

private static boolean isExpired(Item item) {
return item.sellIn < 0;
}

private static void processExpired(Item item) {
if (item.name.equals("Aged Brie")) {
increaseQuality(item);
} else if (item.name.equals("Backstage passes to a TAFKAL80ETC concert")) {
item.quality = 0;
} else if (item.name.equals("Sulfuras, Hand of Ragnaros")) {
return;
} else {
decreaseQuality(item);
}
}

private static void increaseQuality(Item item) {
if (item.quality < 50) {
item.quality += 1;
}
}

private static void decreaseQuality(Item item) {
if (item.quality > 0) {
item.quality -= 1;
}
}
}
}
7 changes: 3 additions & 4 deletions Java/src/test/java/com/gildedrose/GildedRoseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
class GildedRoseTest {

@Test
void foo() {
void testUpdateQuality() {
Item[] items = new Item[] { new Item("foo", 0, 0) };
GildedRose app = new GildedRose(items);
app.updateQuality();
assertEquals("fixme", app.items[0].name);
app.updateInventory();
assertEquals("foo", app.items[0].name);
}

}
3 changes: 1 addition & 2 deletions Java/src/test/java/com/gildedrose/TexttestFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public static void main(String[] args) {
System.out.println(item);
}
System.out.println();
app.updateQuality();
app.updateInventory();
}
}

}