Skip to content

Commit 92df498

Browse files
committed
Refactor StockMarketEntry color logic by extracting repetitive code into getColorValue method
1 parent f9d2ab6 commit 92df498

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

src/main/java/de/rettichlp/ucutils/common/models/StockMarketEntry.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static java.awt.Color.BLUE;
1515
import static java.lang.Double.parseDouble;
1616
import static java.lang.Integer.parseInt;
17+
import static java.util.regex.Pattern.compile;
1718
import static net.minecraft.ChatFormatting.DARK_GREEN;
1819
import static net.minecraft.ChatFormatting.DARK_RED;
1920
import static net.minecraft.ChatFormatting.GOLD;
@@ -25,19 +26,34 @@
2526

2627
public record StockMarketEntry(Company company, double price, double changeMoney, double changePercentage, int ownership, double buyPrice) {
2728

28-
private static final Pattern PRICE_PATTERN = Pattern.compile("Kurs: (?<price>\\d+\\.\\d+)\\$");
29-
private static final Pattern CHANGE_PATTERN = Pattern.compile("Änderung: (?<money>-?\\d+\\.\\d+)\\$ \\((?<percentage>-?\\d+\\.\\d+)%\\) [▼▲]");
30-
private static final Pattern OWNERSHIP_PATTERN = Pattern.compile("Besitz: (?<ownership>\\d+)/\\d+");
31-
private static final Pattern BUY_PRICE_PATTERN = Pattern.compile("EK-Preis: (?<price>-?\\d+\\.\\d+)\\$");
29+
private static final Pattern PRICE_PATTERN = compile("Kurs: (?<price>\\d+\\.\\d+)\\$");
30+
private static final Pattern CHANGE_PATTERN = compile("Änderung: (?<money>-?\\d+\\.\\d+)\\$ \\((?<percentage>-?\\d+\\.\\d+)%\\) [▼▲]");
31+
private static final Pattern OWNERSHIP_PATTERN = compile("Besitz: (?<ownership>\\d+)/\\d+");
32+
private static final Pattern BUY_PRICE_PATTERN = compile("EK-Preis: (?<price>-?\\d+\\.\\d+)\\$");
3233

3334
public @Nullable Color getColor() {
34-
double diff = this.price - this.buyPrice;
35-
35+
// check if price is below or equal to min value
3636
if (this.price <= this.company.getMinValue()) {
3737
return BLUE;
3838
}
3939

40+
// check if user has bought any stock
41+
if (this.ownership == 0) {
42+
return null;
43+
}
44+
45+
// get color value
46+
Integer colorValue = getColorValue();
47+
if (colorValue == null) {
48+
return null;
49+
}
50+
51+
return new Color(colorValue);
52+
}
53+
54+
private @Nullable Integer getColorValue() {
4055
Integer colorValue;
56+
double diff = this.price - this.buyPrice;
4157

4258
if (diff >= 75) {
4359
colorValue = DARK_GREEN.getColor();
@@ -53,11 +69,7 @@ public record StockMarketEntry(Company company, double price, double changeMoney
5369
colorValue = DARK_RED.getColor();
5470
}
5571

56-
if (colorValue == null) {
57-
return null;
58-
}
59-
60-
return new Color(colorValue);
72+
return colorValue;
6173
}
6274

6375
public static @Nullable StockMarketEntry fromItemStack(@NotNull ItemStack itemStack) {
@@ -116,10 +128,6 @@ public record StockMarketEntry(Company company, double price, double changeMoney
116128
}
117129
}
118130

119-
if (price == 0) {
120-
return null;
121-
}
122-
123131
return new StockMarketEntry(company, price, changeMoney, changePercentage, ownership, buyPrice);
124132
}
125133
}

0 commit comments

Comments
 (0)