DataManeger working save load

This commit is contained in:
HeshamTB 2020-12-16 16:51:45 +03:00
parent ab3acdec9b
commit 9cf847ad46
2 changed files with 30 additions and 5 deletions

View File

@ -1,8 +1,7 @@
import java.io.*; import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
public class DataManeger { public class DataManeger {
@ -68,6 +67,24 @@ public class DataManeger {
return times; return times;
} }
public List<State> getStates(){
List<State> 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() { private void clearData() {
for (File file : savedStateFiles()) { for (File file : savedStateFiles()) {
if (file.getName().contains(".bin")){ if (file.getName().contains(".bin")){
@ -87,8 +104,15 @@ class State implements Serializable {
private Street[] stdStreet; private Street[] stdStreet;
private Date allArrivedToArafatTime; private Date allArrivedToArafatTime;
private Date allArrivedToHotelsTime; private Date allArrivedToHotelsTime;
private Date stateTime;
public State(ArrayList<Campaign> listOfCampaigns, ArrayList<Vehicle> listOfVehicles, Route[] stdRoutes, Street[] stdStreet, Date allArrivedToArafatTime, Date allArrivedToHotelsTime) { public State(ArrayList<Campaign> listOfCampaigns,
ArrayList<Vehicle> listOfVehicles,
Route[] stdRoutes,
Street[] stdStreet,
Date allArrivedToArafatTime,
Date allArrivedToHotelsTime,
Date stateTime) {
//Make clones since values may change if this is running on a thread. //Make clones since values may change if this is running on a thread.
this.listOfCampaigns = (ArrayList<Campaign>) listOfCampaigns.clone(); this.listOfCampaigns = (ArrayList<Campaign>) listOfCampaigns.clone();
this.listOfVehicles = (ArrayList<Vehicle>) listOfVehicles.clone(); this.listOfVehicles = (ArrayList<Vehicle>) listOfVehicles.clone();
@ -100,6 +124,7 @@ class State implements Serializable {
if (allArrivedToHotelsTime != null) { if (allArrivedToHotelsTime != null) {
this.allArrivedToHotelsTime = (Date) allArrivedToHotelsTime.clone(); this.allArrivedToHotelsTime = (Date) allArrivedToHotelsTime.clone();
} }
this.stateTime = stateTime;
} }
public ArrayList<Campaign> getListOfCampaigns() { public ArrayList<Campaign> getListOfCampaigns() {

View File

@ -1162,9 +1162,9 @@ public class MakkahCity {
stdRoutes, stdRoutes,
stdStreet, stdStreet,
allArrivedToArafatTime, allArrivedToArafatTime,
allArrivedToHotelsTime); allArrivedToHotelsTime,
currenttimeManager.getCurrentTime());
dataManeger.saveState(s, 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()); boolean result = dataManeger.saveState(s, currenttimeManager.getCurrentTime());
if (!result) System.out.println("Could not save state "+currenttimeManager.getCurrentTime().getTime()); if (!result) System.out.println("Could not save state "+currenttimeManager.getCurrentTime().getTime());