PDA

Click to See Complete Forum and Search --> : Need advice on Schedule Program


ddmeightball
May 19th, 2006, 02:48 PM
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.

ddmeightball
May 19th, 2006, 03:02 PM
For a further example, here is one.

Here's a pseudocode example:

If startTime <= thisTime <= endTime, there is a conflict.

System_Error
May 19th, 2006, 07:53 PM
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.

System_Error
May 19th, 2006, 07:54 PM
You could also use compare and see if the third date is greater than the second ending time... Probably a much easier route.

ddmeightball
May 22nd, 2006, 04:14 PM
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?

ddmeightball
May 22nd, 2006, 10:43 PM
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.


ts1.setBeginTime(9,15);


Timeslot CLass

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


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.

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.

ddmeightball
May 23rd, 2006, 06:57 AM
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

Calendar cal = new Calendar();

because calendar is abstract.



I could just do something like this:

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:

Calendar cal = new GregorianCalendar();

since I can instantiate a GregorianCalendar class. I thinking I should create a new java class that extends GregorianCalendar.

ComputerJy
May 23rd, 2006, 06:05 PM
I don't think you need beginDate and beginTime in your slot
The calendar object can hold both date and time