Skip to content

Commit 34f8a66

Browse files
Merge pull request #46 from jeffreybakker/fix/extension_4
make the simulated time persistent
2 parents c6b16b6 + 20fdd8d commit 34f8a66

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
time.txt
12

23
# Created by https://www.gitignore.io/api/windows,linux,intellij
34

src/main/java/honours/ing/banq/time/TimeUtil.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package honours.ing.banq.time;
22

3+
import java.io.*;
34
import java.util.Date;
45

56
/**
@@ -11,7 +12,33 @@ public class TimeUtil {
1112

1213
private static final long DAY = 1000 * 60 * 60 * 24; // second * minute * hour * day
1314

14-
private static long addedMillis = 0;
15+
private static long addedMillis = init();
16+
17+
private static long init() {
18+
try {
19+
File time = new File("time.txt");
20+
if (!time.exists()) {
21+
time.createNewFile();
22+
PrintWriter out = new PrintWriter(new FileOutputStream(time));
23+
out.println(0L);
24+
out.close();
25+
}
26+
27+
try (BufferedReader in = new BufferedReader(new FileReader(time))) {
28+
return Long.parseLong(in.readLine());
29+
}
30+
} catch (Exception ignored) { }
31+
return 0;
32+
}
33+
34+
private static void save() {
35+
try {
36+
File time = new File("time.txt");
37+
PrintWriter out = new PrintWriter(new FileOutputStream(time));
38+
out.println(0L);
39+
out.close();
40+
} catch (Exception ignored) { }
41+
}
1542

1643
/**
1744
* Simulates the passing of x days.
@@ -23,13 +50,15 @@ public static void addDays(int days) {
2350
}
2451

2552
addedMillis += days * DAY;
53+
save();
2654
}
2755

2856
/**
2957
* Resets the simulation of time.
3058
*/
3159
public static void resetTime() {
3260
addedMillis = 0;
61+
save();
3362
}
3463

3564
/**

0 commit comments

Comments
 (0)