Skip to content

Commit

Permalink
Merge pull request #46 from jeffreybakker/fix/extension_4
Browse files Browse the repository at this point in the history
make the simulated time persistent
  • Loading branch information
jeffreybakker authored Aug 4, 2017
2 parents c6b16b6 + 20fdd8d commit 34f8a66
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
time.txt

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

Expand Down
31 changes: 30 additions & 1 deletion src/main/java/honours/ing/banq/time/TimeUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package honours.ing.banq.time;

import java.io.*;
import java.util.Date;

/**
Expand All @@ -11,7 +12,33 @@ public class TimeUtil {

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

private static long addedMillis = 0;
private static long addedMillis = init();

private static long init() {
try {
File time = new File("time.txt");
if (!time.exists()) {
time.createNewFile();
PrintWriter out = new PrintWriter(new FileOutputStream(time));
out.println(0L);
out.close();
}

try (BufferedReader in = new BufferedReader(new FileReader(time))) {
return Long.parseLong(in.readLine());
}
} catch (Exception ignored) { }
return 0;
}

private static void save() {
try {
File time = new File("time.txt");
PrintWriter out = new PrintWriter(new FileOutputStream(time));
out.println(0L);
out.close();
} catch (Exception ignored) { }
}

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

addedMillis += days * DAY;
save();
}

/**
* Resets the simulation of time.
*/
public static void resetTime() {
addedMillis = 0;
save();
}

/**
Expand Down

0 comments on commit 34f8a66

Please sign in to comment.