Merge pull request #21 from HeshamTB/history-bin

History bin
This commit is contained in:
IMXHERO 2020-12-09 01:00:52 +03:00 committed by GitHub
commit 4a5b9cf964
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 184 additions and 27 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ out/
/bin/
*.jpeg
*.zip
*.bin

View File

@ -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;

107
src/DataManeger.java Normal file
View File

@ -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(String.format("0x%016X.bin", 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("0x%016X.bin", 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("0x%016X.bin", 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<Campaign> listOfCampaigns;
private ArrayList<Vehicle> listOfVehicles;
private Route[] stdRoutes;
private Street[] stdStreet;
private Date allArrivedToArafatTime;
private Date allArrivedToHotelsTime;
public State(ArrayList<Campaign> listOfCampaigns, ArrayList<Vehicle> 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<Campaign>) listOfCampaigns.clone();
this.listOfVehicles = (ArrayList<Vehicle>) 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<Campaign> getListOfCampaigns() {
return listOfCampaigns;
}
public ArrayList<Vehicle> getListOfVehicles() {
return listOfVehicles;
}
public Route[] getStdRoutes() {
return stdRoutes;
}
public Street[] getStdStreet() {
return stdStreet;
}
public Date getAllArrivedToArafatTime() {
return allArrivedToArafatTime;
}
public Date getAllArrivedToHotelsTime() {
return allArrivedToHotelsTime;
}
}

View File

@ -221,6 +221,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(".");
@ -359,8 +360,9 @@ public class MakkahCity {
"[3] View Campaigns\n" +
"[4] View Routes\n" +
"[5] Print report\n" +
"[6] Continue\n" +
"[7] Exit");
"[6] Browse History\n" +
"[7] Continue\n" +
"[8] Exit");
String choice = in.next();
//Split into methods?
if (choice.equals("1")){
@ -393,8 +395,9 @@ public class MakkahCity {
}
}
if (choice.equals("5")) System.out.println(getStreetsReport());
if (choice.equals("6")) return;
if (choice.equals("7")) {
if (choice.equals("6")) browseHistory();
if (choice.equals("7")) return;
if (choice.equals("8")) {
inputListener.stop();
t.interrupt();
System.exit(0);
@ -471,6 +474,46 @@ public class MakkahCity {
System.out.println(route.toString());
}
private static void browseHistory() {
Calendar dummyCal = new GregorianCalendar();
dummyCal.setTime(currenttimeManager.getCurrentTime());
//dummyCal.roll(Calendar.HOUR, -1);
dummyCal.set(Calendar.MINUTE, 0);//Go down to last hour.
Scanner in = new Scanner(System.in);
boolean selected = false;
while (!selected){
System.out.println(new HijriDate(dummyCal.getTimeInMillis()));
System.out.print("[1] Forward\n" +
"[2] Backward\n" +
"[3] Select\n" +
"[4] Return\n");
String choice = in.next();
if (choice.equals("1")) dummyCal.roll(Calendar.HOUR, 1);
if (choice.equals("2")) dummyCal.roll(Calendar.HOUR, -1);
if (choice.equals("3")) {
selected = true;
DataManeger dataManeger = new DataManeger();
if (dataManeger.stateAvailable(dummyCal.getTime())) {
State state = dataManeger.loadState(dummyCal.getTime());
System.out.print(dummyCal.getTime() + " (History)\n");
System.out.println("\n"+
"---------------------------\n" +
"[1] View Buses\n" +
"[2] View Streets\n" +
"[3] View Campaigns\n" +
"[4] View Routes\n");
choice = in.next();
if (choice.equals("1")){
System.out.print("0 - "+state.getListOfVehicles().size());
choice = in.next();
showVehicle(state.getListOfVehicles().get(Integer.parseInt(choice)));
}
} else System.out.print("Not saved");
}
}
}
private static void clearDoneCivilVehicles() {
//Clear civil cars from list
for (int i = 0; i < listOfVehicles.size();){
@ -968,6 +1011,25 @@ 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);
}
static void updateStreetFrame() {
Object[][] streetData = new Object[stdStreet.length][6];
for (int i = 0; i < stdStreet.length; i++) {

View File

@ -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;

View File

@ -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<Vehicle> vehicles; //Current
private HashMap<Vehicle, ArrayList<Date>> 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) {

View File

@ -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;