refactoring: Michael, Alex and Nemanja did refactoring excerxise

This commit is contained in:
Nemanja Spajic 2022-06-02 00:00:02 +02:00
parent 9653512287
commit a568d2a132
10 changed files with 396 additions and 91 deletions

View File

@ -10,7 +10,7 @@
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<java.version>11</java.version>
<junit.jupiter.version>5.8.2</junit.jupiter.version>
<maven.maven-compiler-plugin.version>3.1</maven.maven-compiler-plugin.version>
<maven.maven-surefire-plugin.version>3.0.0-M4</maven.maven-surefire-plugin.version>
@ -24,6 +24,14 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.21.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -36,6 +44,47 @@
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.22.5</version>
<configuration>
<formats>
<!-- you can define as many formats as you want, each is independent -->
<format>
<!-- define the files to apply to -->
<includes>
<include>*.md</include>
<include>.gitignore</include>
</includes>
<!-- define the steps to apply to those files -->
<trimTrailingWhitespace/>
<endWithNewline/>
<indent>
<tabs>true</tabs>
<spacesPerTab>4</spacesPerTab>
</indent>
</format>
</formats>
<!-- define a language-specific format -->
<java>
<!-- no need to specify files, inferred automatically, but you can if you want -->
<!-- apply a specific flavor of google-java-format and reflow long strings -->
<googleJavaFormat>
<version>1.8</version>
<style>AOSP</style>
</googleJavaFormat>
<!-- make sure every file has the following copyright header.
optionally, Spotless can set copyright years by digging
through git history (see "license" section below) -->
<licenseHeader>
<content>/* (C)$YEAR */</content> <!-- or <file>${project.basedir}/license-header</file> -->
</licenseHeader>
</java>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.maven-surefire-plugin.version}</version>

View File

@ -1,5 +1,8 @@
/* (C)2022 */
package com.gildedrose;
import com.gildedrose.item.Item;
class GildedRose {
Item[] items;
@ -9,54 +12,8 @@ class GildedRose {
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;
}
}
}
}
}
if (!items[i].name.equals("Sulfuras, Hand of Ragnaros")) {
items[i].sellIn = items[i].sellIn - 1;
}
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;
}
}
}
items[i].updateQuality();
items[i].updateSellIn();
}
}
}
}

View File

@ -1,21 +0,0 @@
package com.gildedrose;
public class Item {
public String name;
public int sellIn;
public int quality;
public Item(String name, int sellIn, int quality) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}
@Override
public String toString() {
return this.name + ", " + this.sellIn + ", " + this.quality;
}
}

View File

@ -0,0 +1,22 @@
/* (C)2022 */
package com.gildedrose.item;
public class AgedBrie extends Item {
public static final String AGED_BRIE = "Aged Brie";
public AgedBrie(int sellIn, int quality) {
super(AGED_BRIE, sellIn, quality);
}
@Override
public void updateQuality() {
if (quality < 50) {
if (sellIn <= 0) {
quality += 2;
} else {
quality += 1;
}
}
}
}

View File

@ -0,0 +1,25 @@
/* (C)2022 */
package com.gildedrose.item;
public class BackstageTicket extends Item {
public static final String BACKSTAGE_TICKET = "Backstage passes to a TAFKAL80ETC concert";
public BackstageTicket(int sellIn, int quality) {
super(BACKSTAGE_TICKET, sellIn, quality);
}
@Override
public void updateQuality() {
if (quality < 50) {
if (sellIn <= 0) {
quality = 0;
} else if (sellIn < 6) {
quality += 3;
} else if (sellIn < 11) {
quality += 2;
} else {
quality += 1;
}
}
}
}

View File

@ -0,0 +1,23 @@
/* (C)2022 */
package com.gildedrose.item;
public class Conjured extends Item {
public static final String CONJURED = "Conjured";
public Conjured(int sellIn, int quality) {
super(CONJURED, sellIn, quality);
}
@Override
public void updateQuality() {
if (sellIn <= 0) {
quality -= 4;
} else {
quality -= 2;
}
if (quality < 0) {
quality = 0;
}
}
}

View File

@ -0,0 +1,37 @@
/* (C)2022 */
package com.gildedrose.item;
public class Item {
public String name;
public int sellIn;
public int quality;
public Item(String name, int sellIn, int quality) {
this.name = name;
this.sellIn = sellIn;
this.quality = quality;
}
public void updateQuality() {
if (sellIn <= 0) {
quality -= 2;
} else {
quality -= 1;
}
if (quality < 0) {
quality = 0;
}
}
public void updateSellIn() {
sellIn -= 1;
}
@Override
public String toString() {
return this.name + ", " + this.sellIn + ", " + this.quality;
}
}

View File

@ -0,0 +1,17 @@
/* (C)2022 */
package com.gildedrose.item;
public class Sulfuras extends Item {
public static final String SULFURAS = "Sulfuras, Hand of Ragnaros";
public static final int QUALITY = 80;
public Sulfuras(int sellIn) {
super(SULFURAS, sellIn, QUALITY);
}
@Override
public void updateQuality() {}
@Override
public void updateSellIn() {}
}

View File

@ -1,17 +1,209 @@
/* (C)2022 */
package com.gildedrose;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.gildedrose.item.AgedBrie;
import com.gildedrose.item.BackstageTicket;
import com.gildedrose.item.Conjured;
import com.gildedrose.item.Item;
import com.gildedrose.item.Sulfuras;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class GildedRoseTest {
@Test
void foo() {
Item[] items = new Item[] { new Item("foo", 0, 0) };
GildedRose app = new GildedRose(items);
app.updateQuality();
assertEquals("fixme", app.items[0].name);
public static final String I_AM_NOT_SPECIAL = "I am not special";
private Item sulfuras;
@BeforeEach
void setup() {
sulfuras = new Sulfuras(5);
}
@Test
void updateQuality_nonSpecialItem_thenDecreaseQualityAndSellIn() {
final Item notSpecialItem = new Item(I_AM_NOT_SPECIAL, 2, 5);
final GildedRose sut = initializeApp(notSpecialItem);
updateQuality(sut, 1);
assertThat(notSpecialItem.quality).isEqualTo(4);
assertThat(notSpecialItem.sellIn).isEqualTo(1);
}
@Test
void updateQuality2_nonSpecialItem_thenDecreaseQualityAndSellInBy2() {
final Item notSpecialItem = new Item(I_AM_NOT_SPECIAL, 2, 5);
final GildedRose sut = initializeApp(notSpecialItem);
updateQuality(sut, 2);
assertThat(notSpecialItem.quality).isEqualTo(3);
assertThat(notSpecialItem.sellIn).isZero();
}
@Test
void updateQuality_nonSpecialItemWithZeroSellIn_thenDecreaseQualityBy2() {
final Item notSpecialItem = new Item(I_AM_NOT_SPECIAL, 0, 5);
final GildedRose sut = initializeApp(notSpecialItem);
updateQuality(sut, 1);
assertThat(notSpecialItem.quality).isEqualTo(3);
assertThat(notSpecialItem.sellIn).isEqualTo(-1);
}
@Test
void updateQuality2_nonSpecialItemWith1SellIn_thenDecreaseQualityBy3() {
final Item notSpecialItem = new Item(I_AM_NOT_SPECIAL, 1, 8);
final GildedRose sut = initializeApp(notSpecialItem);
updateQuality(sut, 2);
assertThat(notSpecialItem.quality).isEqualTo(5);
assertThat(notSpecialItem.sellIn).isEqualTo(-1);
}
@Test
void updateQuality_nonSpecialItemWith0Quality_thenDecreaseOnlySellIn() {
final Item notSpecialItem = new Item(I_AM_NOT_SPECIAL, 1, 0);
final GildedRose sut = initializeApp(notSpecialItem);
updateQuality(sut, 1);
assertThat(notSpecialItem.quality).isZero();
assertThat(notSpecialItem.sellIn).isZero();
}
@Test
void updateQuality_agedBrie_thenIncreaseQuality() {
final Item agedBrie = new AgedBrie(3, 5);
final GildedRose sut = initializeApp(agedBrie);
updateQuality(sut, 1);
assertThat(agedBrie.sellIn).isEqualTo(2);
assertThat(agedBrie.quality).isEqualTo(6);
}
@Test
void updateQuality2_agedBrie49_thenIncreaseQualityToMax50() {
final Item agedBrie = new AgedBrie(3, 49);
final GildedRose sut = initializeApp(agedBrie);
updateQuality(sut, 2);
assertThat(agedBrie.sellIn).isEqualTo(1);
assertThat(agedBrie.quality).isEqualTo(50);
}
@Test
void updateQuality_agedBrieWithSellIn0_thenIncreaseQualityBy2() {
final Item agedBrie = new AgedBrie(0, 5);
final GildedRose sut = initializeApp(agedBrie);
updateQuality(sut, 1);
assertThat(agedBrie.sellIn).isEqualTo(-1);
assertThat(agedBrie.quality).isEqualTo(7);
}
@Test
void updateQuality_sulfuras_thenQualityStaysTheSame() {
final GildedRose sut = initializeApp(sulfuras);
updateQuality(sut, 1);
assertThat(sulfuras.quality).isEqualTo(80);
assertThat(sulfuras.sellIn).isEqualTo(5);
}
@Test
void updateQuality2_backstageTicket11DaysLeft_thenIncreaseQualityBy3() {
final Item backStage = new BackstageTicket(11, 10);
final GildedRose sut = initializeApp(backStage);
updateQuality(sut, 2);
assertThat(backStage.sellIn).isEqualTo(9);
assertThat(backStage.quality).isEqualTo(13);
}
@Test
void updateQuality2_backstageTicket6DaysLeft_thenIncreaseQualityBy5() {
final Item backStage = new BackstageTicket(6, 10);
final GildedRose sut = initializeApp(backStage);
updateQuality(sut, 2);
assertThat(backStage.sellIn).isEqualTo(4);
assertThat(backStage.quality).isEqualTo(15);
}
@Test
void updateQuality_backstageTicket1DayLeft_thenIncreaseQualityBy3() {
final Item backStage = new BackstageTicket(1, 10);
final GildedRose sut = initializeApp(backStage);
updateQuality(sut, 1);
assertThat(backStage.sellIn).isZero();
assertThat(backStage.quality).isEqualTo(13);
}
@Test
void updateQuality2_backstageTicket1DayLeft_thenDecreaseQualityTo0() {
final Item backStage = new BackstageTicket(1, 10);
final GildedRose sut = initializeApp(backStage);
updateQuality(sut, 2);
assertThat(backStage.sellIn).isEqualTo(-1);
assertThat(backStage.quality).isZero();
}
@Test
void updateQuality_conjured_thenDecreaseQualityBy2() {
final Item conjured = new Conjured(4, 5);
final GildedRose sut = initializeApp(conjured);
updateQuality(sut, 1);
assertThat(conjured.sellIn).isEqualTo(3);
assertThat(conjured.quality).isEqualTo(3);
}
@Test
void updateQuality2_conjuredWithQuality3_thenDecreaseQualityTo0() {
final Item conjured = new Conjured(4, 3);
final GildedRose sut = initializeApp(conjured);
updateQuality(sut, 2);
assertThat(conjured.sellIn).isEqualTo(2);
assertThat(conjured.quality).isZero();
}
@Test
void updateQuality_conjuredSellIn0_thenDecreaseQualityBy4() {
final Item conjured = new Conjured(0, 5);
final GildedRose sut = initializeApp(conjured);
updateQuality(sut, 1);
assertThat(conjured.sellIn).isEqualTo(-1);
assertThat(conjured.quality).isEqualTo(1);
}
private void updateQuality(final GildedRose sut, final int times) {
for (int i = 0; i < times; i++) {
sut.updateQuality();
}
}
private GildedRose initializeApp(final Item item) {
Item[] items = new Item[] {item};
return new GildedRose(items);
}
}

View File

@ -1,20 +1,25 @@
/* (C)2022 */
package com.gildedrose;
import com.gildedrose.item.Item;
public class TexttestFixture {
public static void main(String[] args) {
System.out.println("OMGHAI!");
Item[] items = new Item[] {
new Item("+5 Dexterity Vest", 10, 20), //
new Item("Aged Brie", 2, 0), //
new Item("Elixir of the Mongoose", 5, 7), //
new Item("Sulfuras, Hand of Ragnaros", 0, 80), //
new Item("Sulfuras, Hand of Ragnaros", -1, 80),
new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
// this conjured item does not work properly yet
new Item("Conjured Mana Cake", 3, 6) };
Item[] items =
new Item[] {
new Item("+5 Dexterity Vest", 10, 20), //
new Item("Aged Brie", 2, 0), //
new Item("Elixir of the Mongoose", 5, 7), //
new Item("Sulfuras, Hand of Ragnaros", 0, 80), //
new Item("Sulfuras, Hand of Ragnaros", -1, 80),
new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20),
new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49),
new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49),
// this conjured item does not work properly yet
new Item("Conjured Mana Cake", 3, 6)
};
GildedRose app = new GildedRose(items);
@ -33,5 +38,4 @@ public class TexttestFixture {
app.updateQuality();
}
}
}