File tree Expand file tree Collapse file tree 6 files changed +25
-23
lines changed
main/java/com/dinosaur/dinosaurexploder
test/java/com/dinosaur/dinosaurexploder/model Expand file tree Collapse file tree 6 files changed +25
-23
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 11package com .dinosaur .dinosaurexploder .utils ;
22
33import com .dinosaur .dinosaurexploder .model .HighScore ;
4+ import com .dinosaur .dinosaurexploder .model .TotalCoins ;
45
56public interface DataProvider {
67 HighScore getHighScore ();
8+
9+ TotalCoins getTotalCoins ();
710}
Original file line number Diff line number Diff line change 11package com .dinosaur .dinosaurexploder .utils ;
22
33import com .dinosaur .dinosaurexploder .model .HighScore ;
4+ import com .dinosaur .dinosaurexploder .model .TotalCoins ;
45
56import java .io .FileInputStream ;
67import 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}
Original file line number Diff line number Diff line change 44import com .dinosaur .dinosaurexploder .model .HighScore ;
55import com .dinosaur .dinosaurexploder .model .TotalCoins ;
66
7- import java .io .FileInputStream ;
8- import java .io .IOException ;
9- import java .io .ObjectInputStream ;
107import java .util .Map ;
118
129public 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments