Skip to content

Commit 2706b57

Browse files
committed
fix: load missing total coin count from file and update tests accordingly
1 parent f9dfd8e commit 2706b57

File tree

6 files changed

+25
-23
lines changed

6 files changed

+25
-23
lines changed

src/main/java/com/dinosaur/dinosaurexploder/model/GameData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static int getHighScore() {
6363

6464
// Getter for total coins
6565
public static int getTotalCoins() {
66-
totalCoins = shipUnlockChecker.getTotalCoins().getTotal();
66+
totalCoins = new FileDataProvider().getTotalCoins().getTotal();
6767
System.out.println("Total: " + totalCoins);
6868
return totalCoins;
6969
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.dinosaur.dinosaurexploder.utils;
22

33
import com.dinosaur.dinosaurexploder.model.HighScore;
4+
import com.dinosaur.dinosaurexploder.model.TotalCoins;
45

56
public interface DataProvider {
67
HighScore getHighScore();
8+
9+
TotalCoins getTotalCoins();
710
}

src/main/java/com/dinosaur/dinosaurexploder/utils/FileDataProvider.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.dinosaur.dinosaurexploder.utils;
22

33
import com.dinosaur.dinosaurexploder.model.HighScore;
4+
import com.dinosaur.dinosaurexploder.model.TotalCoins;
45

56
import java.io.FileInputStream;
67
import java.io.IOException;
@@ -16,4 +17,14 @@ public HighScore getHighScore() {
1617
return new HighScore();
1718
}
1819
}
20+
21+
@Override
22+
public TotalCoins getTotalCoins() {
23+
try (FileInputStream file = new FileInputStream("totalCoins.ser");
24+
ObjectInputStream in = new ObjectInputStream(file)) {
25+
return (TotalCoins) in.readObject();
26+
} catch (IOException | ClassNotFoundException e) {
27+
return new TotalCoins();
28+
}
29+
}
1930
}

src/main/java/com/dinosaur/dinosaurexploder/utils/ShipUnlockChecker.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
import com.dinosaur.dinosaurexploder.model.HighScore;
55
import com.dinosaur.dinosaurexploder.model.TotalCoins;
66

7-
import java.io.FileInputStream;
8-
import java.io.IOException;
9-
import java.io.ObjectInputStream;
107
import java.util.Map;
118

129
public class ShipUnlockChecker {
@@ -48,16 +45,9 @@ public int check(int shipNumber) {
4845
return shipNumber;
4946
}
5047

51-
public TotalCoins getTotalCoins() {
52-
try (FileInputStream file = new FileInputStream("totalCoins.ser");
53-
ObjectInputStream in = new ObjectInputStream(file)) {
54-
return (TotalCoins) in.readObject();
55-
} catch (IOException | ClassNotFoundException e) {
56-
return new TotalCoins();
57-
}
58-
}
59-
6048
private void checkScoreAndCoins(int shipNumber) {
49+
totalCoins = dataProvider.getTotalCoins();
50+
6151
int lowerScoreLimit = scoreMap.getOrDefault(shipNumber, 0);
6252
int lowerCoinLimit = coinMap.getOrDefault(shipNumber, 0);
6353

src/main/java/com/dinosaur/dinosaurexploder/utils/WeaponUnlockChecker.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,9 @@ public int check(int weaponNumber) {
3737
return weaponNumber;
3838
}
3939

40-
41-
public TotalCoins getTotalCoins() {
42-
try (FileInputStream file = new FileInputStream("totalCoins.ser");
43-
ObjectInputStream in = new ObjectInputStream(file)) {
44-
return (TotalCoins) in.readObject();
45-
} catch (IOException | ClassNotFoundException e) {
46-
return new TotalCoins();
47-
}
48-
}
49-
5040
private void checkScoreAndCoins(int weaponNumber) {
41+
totalCoins = dataProvider.getTotalCoins();
42+
5143
int lowerScoreLimit = scoreMap.getOrDefault(weaponNumber, 0);
5244
int lowerCoinLimit = coinMap.getOrDefault(weaponNumber, 0);
5345

src/test/java/com/dinosaur/dinosaurexploder/model/SelectionTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class SelectionTest {
1313
public static final int HIGH_SCORE = 100;
1414
public static final int UNLOCKED_WEAPON_HIGHER_LIMIT = 2;
1515
public static final int UNLOCKED_SHIP_HIGHER_LIMIT = 3;
16+
public static final int TOTAL_COINS = 10;
1617

1718
@Test
1819
void cannotSelectLockedShip() {
@@ -43,5 +44,10 @@ class MockDataProvider implements DataProvider {
4344
public HighScore getHighScore() {
4445
return new HighScore(HIGH_SCORE);
4546
}
47+
48+
@Override
49+
public TotalCoins getTotalCoins() {
50+
return new TotalCoins(TOTAL_COINS);
51+
}
4652
}
4753
}

0 commit comments

Comments
 (0)