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

View File

@ -8,6 +8,11 @@ public class MakkahCity {
private static final Route[] stdRoutes = new Route[9];
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) {
//Gen Camp
@ -28,9 +33,9 @@ public class MakkahCity {
//TODO: Set Schedule for Campaigns
while(!PDate.isEnded()) {
PDate.step(Calendar.MINUTE, 1);
System.out.println(PDate.getCurrentTime());
while(!timeManager.isEnded()) {
timeManager.step(Calendar.MINUTE, 1);
System.out.println(timeManager.getCurrentTime());
//TODO: add civil cars in loop iterations. (noise)
//TODO: Move busses and vehicles.
@ -160,4 +165,8 @@ public class MakkahCity {
}
return cars;
}
public static PDate getTimeManager() {
return timeManager;
}
}

View File

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