Make PDate refrence type rather then static members

This commit is contained in:
HeshamTB 2020-11-11 12:04:50 +03:00
parent be05e7f848
commit 9fb653397f
3 changed files with 45 additions and 21 deletions

View File

@ -54,7 +54,7 @@ public class Campaign {
} }
public void setTimeToLeaveToDest(Date timeToLeaveToDest) throws OutOfSimulationTimeException { public void setTimeToLeaveToDest(Date timeToLeaveToDest) throws OutOfSimulationTimeException {
if(PDate.isValidTime(timeToLeaveToDest)) if(PDate.isWithInTimeline(timeToLeaveToDest, MakkahCity.getTimeManager()))
this.timeToLeaveToDest = timeToLeaveToDest; this.timeToLeaveToDest = timeToLeaveToDest;
else throw new OutOfSimulationTimeException(); else throw new OutOfSimulationTimeException();
} }
@ -64,7 +64,7 @@ public class Campaign {
} }
public void setTimeToLeaveToHousing(Date timeToLeaveToHousing) throws OutOfSimulationTimeException { public void setTimeToLeaveToHousing(Date timeToLeaveToHousing) throws OutOfSimulationTimeException {
if(PDate.isValidTime(timeToLeaveToHousing)) if(PDate.isWithInTimeline(timeToLeaveToHousing, MakkahCity.getTimeManager()))
this.timeToLeaveToHousing = timeToLeaveToHousing; this.timeToLeaveToHousing = timeToLeaveToHousing;
else throw new OutOfSimulationTimeException(); else throw new OutOfSimulationTimeException();
} }

View File

@ -8,6 +8,11 @@ public class MakkahCity {
private static final Route[] stdRoutes = new Route[9]; private static final Route[] stdRoutes = new Route[9];
private static final Street[] stdStreet = new Street[8]; private static final Street[] stdStreet = new Street[8];
private static final PDate timeManager = new PDate(
new GregorianCalendar(2020, Calendar.JANUARY, 1, 8, 0, 0),
new GregorianCalendar(2020, Calendar.JANUARY, 2, 8, 0, 0)
);
public static void main(String[] args) { public static void main(String[] args) {
//Gen Camp //Gen Camp
@ -28,9 +33,9 @@ public class MakkahCity {
//TODO: Set Schedule for Campaigns //TODO: Set Schedule for Campaigns
while(!PDate.isEnded()) { while(!timeManager.isEnded()) {
PDate.step(Calendar.MINUTE, 1); timeManager.step(Calendar.MINUTE, 1);
System.out.println(PDate.getCurrentTime()); System.out.println(timeManager.getCurrentTime());
//TODO: add civil cars in loop iterations. (noise) //TODO: add civil cars in loop iterations. (noise)
//TODO: Move busses and vehicles. //TODO: Move busses and vehicles.
@ -160,4 +165,8 @@ public class MakkahCity {
} }
return cars; return cars;
} }
public static PDate getTimeManager() {
return timeManager;
}
} }

View File

@ -7,36 +7,42 @@ import java.util.GregorianCalendar;
*/ */
public class PDate extends Calendar { public class PDate extends Calendar {
public static final Calendar startCalendar = new GregorianCalendar(2020,Calendar.JANUARY,15,13,0,0); public final Calendar startCalendar;
public static final Calendar endCalendar= new GregorianCalendar(2020,Calendar.JANUARY,16,20,0,0); public final Calendar endCalendar;
private static final Calendar currentCalendar = (GregorianCalendar)startCalendar.clone(); private final Calendar currentCalendar;
private static boolean ended; private boolean ended;
public static Calendar getStartCalendar() { public PDate(GregorianCalendar start, GregorianCalendar end) {
this.startCalendar = start;
this.endCalendar = end;
this.currentCalendar = (GregorianCalendar)start.clone();
}
public Calendar getStartCalendar() {
return startCalendar; return startCalendar;
} }
public static Calendar getEndCalendar() { public Calendar getEndCalendar() {
return endCalendar; return endCalendar;
} }
public static Calendar getCurrentCalendar() { public Calendar getCurrentCalendar() {
return currentCalendar; return currentCalendar;
} }
public static Date getStartTime() { public Date getStartTime() {
return startCalendar.getTime(); return startCalendar.getTime();
} }
public static Date getEndTime(){ public Date getEndTime(){
return endCalendar.getTime(); return endCalendar.getTime();
} }
public static Date getCurrentTime() { public Date getCurrentTime() {
return currentCalendar.getTime(); return currentCalendar.getTime();
} }
public static void step(int key, int value){ public void step(int key, int value){
ended = false; ended = false;
Calendar dummy = (GregorianCalendar)currentCalendar.clone(); Calendar dummy = (GregorianCalendar)currentCalendar.clone();
dummy.add(key, value); dummy.add(key, value);
@ -46,19 +52,28 @@ public class PDate extends Calendar {
else ended = true; else ended = true;
} }
public boolean isEnded(){
return ended;
}
/** /**
Check if time/date provided falls within the simulation timeline Check if time/date provided falls within the simulation timeline
*/ */
public static boolean isValidTime(Date timeToLeaveToDest) { public boolean isWithInTimeline(Date time) {
if (timeToLeaveToDest.after(PDate.endCalendar.getTime()) || if (time.after(this.endCalendar.getTime()) ||
timeToLeaveToDest.before(PDate.startCalendar.getTime())) time.before(this.startCalendar.getTime()))
return false; return false;
else else
return true; return true;
} }
public static boolean isEnded(){ /**
return ended; Check if time/date provided falls within the simulation timeline
with reference to a PDate instance.
*/
public static boolean isWithInTimeline(Date time, PDate timeManeger) {
return time.after(timeManeger.getStartTime()) &&
time.before(timeManeger.getEndTime());
} }
@Override @Override