From 147d45e4fef8941c89390dc54d3efad741ccca29 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sun, 6 Dec 2020 04:07:49 +0300 Subject: [PATCH] DataMan and State: - Save state of given time. Still not tested with recovering saved states. --- src/Campaign.java | 3 +- src/DataManeger.java | 107 +++++++++++++++++++++++++++++++++++++++++++ src/MakkahCity.java | 20 ++++++++ src/Route.java | 4 +- src/Street.java | 21 +-------- src/Vehicle.java | 3 +- 6 files changed, 136 insertions(+), 22 deletions(-) create mode 100644 src/DataManeger.java diff --git a/src/Campaign.java b/src/Campaign.java index 4c9ab31..c48ca1e 100644 --- a/src/Campaign.java +++ b/src/Campaign.java @@ -1,7 +1,8 @@ +import java.io.Serializable; import java.util.ArrayList; import java.util.Date; -public class Campaign { +public class Campaign implements Serializable { private String UID; private int numberOfAriivedBuses; diff --git a/src/DataManeger.java b/src/DataManeger.java new file mode 100644 index 0000000..af66508 --- /dev/null +++ b/src/DataManeger.java @@ -0,0 +1,107 @@ +import java.io.*; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Date; + +public class DataManeger { + + private Path workingDir; + +// public DataManeger(Path path) { +// this.seperator = File.separatorChar; +// this.workingDir = path; +// } + + public DataManeger(State state, Date time){ + Path dir = Paths.get(""); + } + + public DataManeger() { + this(null, null); + } + + public boolean stateAvailable(Date time) { + File f = new File(workingDir.toString()+time.getTime()); + return f.exists(); + } + + public State loadState(Date time){ + State state = null; + if (stateAvailable(time)){ + try { + ObjectInputStream objectInputStream = new ObjectInputStream( + new FileInputStream(String.format("%s%d",workingDir,time.getTime()))); + state = (State)objectInputStream.readObject(); + objectInputStream.close(); + } catch (IOException | ClassNotFoundException e) { + e.printStackTrace(); + } + } + return state; + } + + public boolean saveState(State state, Date time){ + try { + FileOutputStream fs = new FileOutputStream(String.format("%s%d",workingDir,time.getTime())); + BufferedOutputStream bfs = new BufferedOutputStream(fs); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(bfs); + objectOutputStream.writeObject(state); + objectOutputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + return true; + } +} + +class State implements Serializable { + + //A better implementation is to make this more general or make Makkah an instance. + + private ArrayList listOfCampaigns; + private ArrayList listOfVehicles; + private Route[] stdRoutes; + private Street[] stdStreet; + private Date allArrivedToArafatTime; + private Date allArrivedToHotelsTime; + + public State(ArrayList listOfCampaigns, ArrayList listOfVehicles, Route[] stdRoutes, Street[] stdStreet, Date allArrivedToArafatTime, Date allArrivedToHotelsTime) { + //Make clones since values may change if this is running on a thread. + this.listOfCampaigns = (ArrayList) listOfCampaigns.clone(); + this.listOfVehicles = (ArrayList) listOfVehicles.clone(); + this.stdRoutes = stdRoutes.clone(); + this.stdStreet = stdStreet.clone(); + if (allArrivedToArafatTime != null) { + this.allArrivedToArafatTime = (Date) allArrivedToArafatTime.clone(); + } + if (allArrivedToHotelsTime != null) { + this.allArrivedToHotelsTime = (Date) allArrivedToHotelsTime.clone(); + } + } + + public ArrayList getListOfCampaigns() { + return listOfCampaigns; + } + + public ArrayList getListOfVehicles() { + return listOfVehicles; + } + + public Route[] getStdRoutes() { + return stdRoutes; + } + + public Street[] getStdStreet() { + return stdStreet; + } + + public Date getAllArrivedToArafatTime() { + return allArrivedToArafatTime; + } + + public Date getAllArrivedToHotelsTime() { + return allArrivedToHotelsTime; + } +} diff --git a/src/MakkahCity.java b/src/MakkahCity.java index 7e96bab..defaf5f 100644 --- a/src/MakkahCity.java +++ b/src/MakkahCity.java @@ -53,6 +53,7 @@ public class MakkahCity { //Start of Every hour if (firstDayTimeMan.getCurrentCalendar().get(Calendar.MINUTE) == 0){ System.out.println("\n\n" + getStreetsReport()); + saveState(); } else System.out.print("."); @@ -732,4 +733,23 @@ public class MakkahCity { } return buses; } + + private static void saveState(){ + State s = new State(listOfCampaigns, + listOfVehicles, + stdRoutes, + stdStreet, + allArrivedToArafatTime, + allArrivedToHotelsTime); + DataManeger dataManeger = new DataManeger(); + dataManeger.saveState(s, currenttimeManager.getCurrentTime()); + + boolean result = dataManeger.saveState(s, currenttimeManager.getCurrentTime()); + 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); + } } diff --git a/src/Route.java b/src/Route.java index 07067e0..1dfb890 100644 --- a/src/Route.java +++ b/src/Route.java @@ -1,4 +1,6 @@ -public class Route implements Travelable { +import java.io.Serializable; + +public class Route implements Travelable, Serializable { private Street[] streets; private District hotelArea; diff --git a/src/Street.java b/src/Street.java index 3743b79..014984f 100644 --- a/src/Street.java +++ b/src/Street.java @@ -1,33 +1,24 @@ +import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; -public class Street implements Travelable { +public class Street implements Travelable, Serializable { private double length; private int numberOfLanes; private ArrayList vehicles; //Current - private HashMap> vehiclesHistory;//History of vehicles private StreetNames name; public Street(double length, int numberOfLanes, StreetNames name) { vehicles = new ArrayList<>(); - vehiclesHistory = new HashMap<>(); setLength(length); setNumberOfLanes(numberOfLanes); this.name = name; } - public Date[] getHistoryForVehicle(Vehicle vehicle){ - if (vehiclesHistory.containsKey(vehicle)){ - Date[] hist = new Date[vehiclesHistory.get(vehicle).size()]; - return vehiclesHistory.get(vehicle).toArray(hist); - } - return null; - } - private void setLength(double length) { if (length >= 0) this.length = length; else throw new IllegalArgumentException("Can not make a negative length street"); @@ -96,14 +87,6 @@ public class Street implements Travelable { public void addVehicle(Vehicle vehicle) { vehicles.add(vehicle); - addHistoryEntry(vehicle); - } - - private void addHistoryEntry(Vehicle vehicle) { - if (!vehiclesHistory.containsKey(vehicle)) { - vehiclesHistory.put(vehicle, new ArrayList<>()); - } - vehiclesHistory.get(vehicle).add(MakkahCity.getTimeMan().getCurrentTime());//Add time } public double capcityPoint(double min, double max) { diff --git a/src/Vehicle.java b/src/Vehicle.java index be86765..830c202 100644 --- a/src/Vehicle.java +++ b/src/Vehicle.java @@ -1,7 +1,8 @@ +import java.io.Serializable; import java.util.Date; import java.util.HashMap; -public abstract class Vehicle { +public abstract class Vehicle implements Serializable { private double vehicleSize; private Route route;