From 9cf847ad46b465289f79a9261b9be7b162b5fea3 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 16 Dec 2020 16:51:45 +0300 Subject: [PATCH] DataManeger working save load --- src/DataManeger.java | 31 ++++++++++++++++++++++++++++--- src/MakkahCity.java | 4 ++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/DataManeger.java b/src/DataManeger.java index 4934606..a8bdd53 100644 --- a/src/DataManeger.java +++ b/src/DataManeger.java @@ -1,8 +1,7 @@ import java.io.*; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.Date; +import java.util.List; public class DataManeger { @@ -68,6 +67,24 @@ public class DataManeger { return times; } + public List getStates(){ + List list = new ArrayList<>(); + for (File file : workingDir.listFiles()) { + if (file.getName().contains(".bin")){ + try { + FileInputStream fileInputStream = new FileInputStream(file); + BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); + ObjectInputStream objectInputStream = new ObjectInputStream(bufferedInputStream); + State state = (State) objectInputStream.readObject(); + list.add(state); + } catch (IOException | ClassNotFoundException e) { + e.printStackTrace(); + } + } + } + return list; + } + private void clearData() { for (File file : savedStateFiles()) { if (file.getName().contains(".bin")){ @@ -87,8 +104,15 @@ class State implements Serializable { private Street[] stdStreet; private Date allArrivedToArafatTime; private Date allArrivedToHotelsTime; + private Date stateTime; - public State(ArrayList listOfCampaigns, ArrayList listOfVehicles, Route[] stdRoutes, Street[] stdStreet, Date allArrivedToArafatTime, Date allArrivedToHotelsTime) { + public State(ArrayList listOfCampaigns, + ArrayList listOfVehicles, + Route[] stdRoutes, + Street[] stdStreet, + Date allArrivedToArafatTime, + Date allArrivedToHotelsTime, + Date stateTime) { //Make clones since values may change if this is running on a thread. this.listOfCampaigns = (ArrayList) listOfCampaigns.clone(); this.listOfVehicles = (ArrayList) listOfVehicles.clone(); @@ -100,6 +124,7 @@ class State implements Serializable { if (allArrivedToHotelsTime != null) { this.allArrivedToHotelsTime = (Date) allArrivedToHotelsTime.clone(); } + this.stateTime = stateTime; } public ArrayList getListOfCampaigns() { diff --git a/src/MakkahCity.java b/src/MakkahCity.java index 33b1071..d93dcb9 100644 --- a/src/MakkahCity.java +++ b/src/MakkahCity.java @@ -1162,9 +1162,9 @@ public class MakkahCity { stdRoutes, stdStreet, allArrivedToArafatTime, - allArrivedToHotelsTime); + allArrivedToHotelsTime, + currenttimeManager.getCurrentTime()); dataManeger.saveState(s, currenttimeManager.getCurrentTime()); - System.out.println(Arrays.toString(dataManeger.savedStatesTimes())); //TODO FOR DEBUG REMOVE boolean result = dataManeger.saveState(s, currenttimeManager.getCurrentTime()); if (!result) System.out.println("Could not save state "+currenttimeManager.getCurrentTime().getTime());