Need advice on Schedule Program
I am writing a program that will allow a user to enter in appointments on a schedule. I am having problems with figuring out how to work out the Calendar objects.
My problem is this. Each appointment needs a start time and ending time and a duration. I am having problems trying to figure out how to check to see if an appointment is already taken.
For example, if someone signs up for an appointment at 1:30pm to 2:30pm. This appointment will have a 60 minute duration. Then someone enters an appointment that starts at 1:45pm.
The two starting times are different as we can plainly see so I cant check to see if appointment times are equal, because they arent. I want to be able to check to see if a starting time of an appointment doesnt conflict with another appointments timeslot. Something like if an appointment start time is somewhere in between another appointments start/end time, dont add it.
I was hoping to get some suggestions or even some code examples of what everyone has done to try to solve this problem. Any help is appreciated.
Re: Need advice on Schedule Program
For a further example, here is one.
Here's a pseudocode example:
If startTime <= thisTime <= endTime, there is a conflict.
Re: Need advice on Schedule Program
Subtract the two dates (the ending time from the starting time) That would be a long value. Get the milliseconds from that. Covert the second controversial time to milliseconds. Compare those. If the second variable is larger than the first then it is a legal uninterrupted appointment. Hope that makes since.
Re: Need advice on Schedule Program
You could also use compare and see if the third date is greater than the second ending time... Probably a much easier route.
Re: Need advice on Schedule Program
Quote:
Originally Posted by System_Error
You could also use compare and see if the third date is greater than the second ending time... Probably a much easier route.
System_Error, can you show me an example of using compare(). Are you meaning to compare the date objects or comparing the time attribute of one time object to another?
Re: Need advice on Schedule Program
Allright, here is what I have so far in my timeslot class. I am getting an Null Pointer excpetion on line 16 of my Test.java class when I try to set the begintime of a timeslot object. My code is in the post, it is also available in a zip file.
Code:
ts1.setBeginTime(9,15);
Timeslot CLass
Code:
import java.util.*;
public class Timeslot {
private Calendar day;
private Calendar beginDate;
private Calendar endDate;
private Calendar beginTime;
private Calendar endTime;
private double duration = 1;
public Timeslot()
{
}
public Calendar getBeginDate()
{
return beginDate;
}
public void setBeginDate(Calendar bDate)
{
this.beginDate = bDate;
}
public void setBeginDate(int year,int month,int date)
{
this.beginDate.set(year, month, date);
}
public Calendar getEndDate()
{
return endDate;
}
public void setEndDate(Calendar eDate)
{
this.endDate = eDate;
}
public void setEndDate(int year,int month,int date)
{
this.endDate.set(year, month, date);
}
public Calendar getBeginTime()
{
return beginTime;
}
public void setBeginTime(Calendar bTime)
{
this.beginTime = bTime;
}
public void setBeginTime(int hour, int minute)
{
this.beginTime.set(1,1,1,hour, minute);
}
public Calendar getEndTime()
{
return endTime;
}
public void setEndTime(Calendar eTime)
{
this.endTime = eTime;
}
public void setEndTime(int hour, int minute)
{
this.endTime.set(1,1,1,hour, minute);
}
public Calendar getDay()
{
return day;
}
public void setDay(Calendar dow)
{
this.day = dow;
}
public boolean compare(Timeslot tsCompare)
{
if((this.getBeginTime().getTimeInMillis() >= tsCompare.getBeginTime().getTimeInMillis() || this.getEndTime().getTimeInMillis()<= tsCompare.getBeginTime().getTimeInMillis()))
{
return true;
}
else
{
return false;
}
}
}
Test Class
Code:
public class Test {
Timeslot ts1 = new Timeslot();
Timeslot ts2 = new Timeslot();
public static void main(String[] args) {
new Test();
}
public Test() {
ts1.setBeginTime(9,15);
ts1.setEndTime(9,30);
ts2.setBeginTime(15,15);
ts2.setEndTime(15,30);
if(ts1.compare(ts2))
{
System.out.println("Compare returned True");
}
else if(!ts1.compare(ts2))
{
System.out.println("Compare returned False");
}
}
}
Here is the error message in its entirety if you want to see it.
Quote:
Exception in thread "main" java.lang.NullPointerException
at Timeslot.setBeginTime(Timeslot.java:53)
at Test.<init>(Test.java:16)
at Test.main(Test.java:7)
Press any key to continue...
Thanks for helping.
Re: Need advice on Schedule Program
I figured out the null pont exception. beginTime is declared as a Calendar object, but it is never set to an instance of the Calendar class. In the setBeginTime method, I am are trying to call the set method on the object, but it hasn't been instantiated yet
Thanks for pointing that out. But my question is how can I instantiate a class that is abstract? I cant do
Code:
Calendar cal = new Calendar();
because calendar is abstract.
I could just do something like this:
Code:
private Calendar beginDate = Calendar.getInstance();
then set it to something else later in the program.
It would probably be better if I do something like:
Code:
Calendar cal = new GregorianCalendar();
since I can instantiate a GregorianCalendar class. I thinking I should create a new java class that extends GregorianCalendar.
Re: Need advice on Schedule Program
I don't think you need beginDate and beginTime in your slot
The calendar object can hold both date and time