From ab3acdec9b3c7147729bba4e0d1566913a88d45c Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 16 Dec 2020 16:26:40 +0300 Subject: [PATCH] DataManeger: - DataManeger is static global var in Makkah. - Save states in data/ dir. - Clear all bin files with constructor. --- src/DataManeger.java | 22 ++++++++++++++++------ src/MakkahCity.java | 7 +------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/DataManeger.java b/src/DataManeger.java index cc38a1d..4934606 100644 --- a/src/DataManeger.java +++ b/src/DataManeger.java @@ -9,20 +9,22 @@ public class DataManeger { private File workingDir; public DataManeger(){ - workingDir = new File("."); + workingDir = new File("./data/"); + workingDir.mkdir(); + clearData(); } public boolean stateAvailable(Date time) { - File f = new File(String.format("0x%016X.bin", time.getTime())); + File f = new File(String.format("./data/%s.bin", time.getTime())); return f.exists(); } - public State loadState(Date time){ + public State loadState(Date time){ State state = null; if (stateAvailable(time)){ try { ObjectInputStream objectInputStream = new ObjectInputStream( - new FileInputStream(String.format("%016X.bin", time.getTime()))); + new FileInputStream(String.format("./data/%s.bin", time.getTime()))); state = (State)objectInputStream.readObject(); objectInputStream.close(); } catch (IOException | ClassNotFoundException e) { @@ -34,7 +36,7 @@ public class DataManeger { public boolean saveState(State state, Date time){ try { - FileOutputStream fs = new FileOutputStream(String.format("%016X.bin", time.getTime())); + FileOutputStream fs = new FileOutputStream(String.format("./data/%s.bin", time.getTime())); BufferedOutputStream bfs = new BufferedOutputStream(fs); ObjectOutputStream objectOutputStream = new ObjectOutputStream(bfs); objectOutputStream.writeObject(state); @@ -57,7 +59,7 @@ public class DataManeger { if (file.getName().contains(".bin")){ String timeInName = file.getName().replaceAll(".bin", "").trim(); if ("".equals(timeInName)) continue; - long time = Long.parseLong(timeInName, 16); + long time = Long.parseLong(timeInName); timesList.add(new HijriDate(time)); } } @@ -65,6 +67,14 @@ public class DataManeger { times = timesList.toArray(times); return times; } + + private void clearData() { + for (File file : savedStateFiles()) { + if (file.getName().contains(".bin")){ + file.delete(); + } + } + } } class State implements Serializable { diff --git a/src/MakkahCity.java b/src/MakkahCity.java index 0bf2340..33b1071 100644 --- a/src/MakkahCity.java +++ b/src/MakkahCity.java @@ -31,6 +31,7 @@ public class MakkahCity { private static final InputListener inputListener = new InputListener(); private static final Thread t = new Thread(inputListener,"InputThread-Makkah"); private static boolean isAllRoutSet; + private static final DataManeger dataManeger = new DataManeger(); //GUI private static boolean exit_flag; private static boolean pause_flag; @@ -1162,7 +1163,6 @@ public class MakkahCity { stdStreet, allArrivedToArafatTime, allArrivedToHotelsTime); - DataManeger dataManeger = new DataManeger(); dataManeger.saveState(s, currenttimeManager.getCurrentTime()); System.out.println(Arrays.toString(dataManeger.savedStatesTimes())); //TODO FOR DEBUG REMOVE @@ -1170,11 +1170,6 @@ public class MakkahCity { if (!result) System.out.println("Could not save state "+currenttimeManager.getCurrentTime().getTime()); } - private static State loadState(Date time){ - DataManeger dataManeger = new DataManeger(); - return dataManeger.loadState(time); - } - private static void updateStreetFrame() { Object[][] streetData = new Object[stdStreet.length][6]; for (int i = 0; i < stdStreet.length; i++) {