Hi:
I am trying to develop code for my Calendar program that will allow me to print the appointments of a specified date
(ex:30/01/03).
Presently my program will add appointements,identify duplicate
appointments and has a method that will remove appointment but I have
not bee able to get the program to print appointments for individual
dates as selected by the user.
Can someone please help me?
Thanks
import java.util.Date;
import java.util.Vector;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
/**
Describes a calendar for a set of appointments.
@version 1.0
*/
public class CalendarTest
{ public static void main(
String[] args)
{ Calendar makeCalendar = new Calendar();
try
{ InputStreamReader reader
= new InputStreamReader(System.in);
BufferedReader console
= new BufferedReader(reader);
boolean done = false;
while (!done)
{
System.out.println();
System.out.println();
System.out.println("Please enter an appointment startdate"
+ " and time(dd/MM/yy hh:mm a)."
+ "\n" + "Or press enter to exit the program.");
String sdateTime = console.readLine();
if ( sdateTime == null || sdateTime.equals(""))
done = true;
else
{
String sTime = sdateTime;
System.out.println("Please enter an appointment enddate and time:");
String edateTime = console.readLine();
String endTime = edateTime;
System.out.println("Please enter an appointment description:");
String aDescript = console.readLine();
String description = aDescript;
System.out.println("Enter yes to add appointment or no to remove"
+ "or p to print calendar");
String action = console.readLine();
if (action.equals("yes"))
{
makeCalendar.addAppointment(new Appointment(sTime, endTime, description));
makeCalendar.print();
makeCalendar.overlapTest();
}
else if (action.equals("no"))
{ System.out.println(sTime);
Appointment toFind = new Appointment(sTime, endTime, description);
makeCalendar.removeAppointment(toFind);
//makeCalendar.print();
}
else if (action.equals("p"))
{
makeCalendar.print();
}
}
}
}
catch (IOException e)
{ System.out.println(e);
System.exit(1);
}
}
}
/**
Describes a calendar for a set of appointments.
@author John J. Farrell
@version 1.0
*/
class Calendar
{
/**
Constructs a calendar.
*/
public Calendar()
{
appointments = new Vector();
}
/**
Adds an appointment to this Calendar.
@param anApp The appointment to add.
*/
public void addAppointment(Appointment anApp)
{
appointments.add(anApp);
}
/**
Removes an appointment from this Calendar.
@param toFind The appointment to be removed.
@return true if the appointment is found.False
if not found.
*/
public boolean removeAppointment(Appointment toFind)
{
return appointments.remove(toFind);
}
/**
Tests for overlapping appointment dates and times.
*/
public void overlapTest()
{
for (int x = 0; x < appointments.size(); x++)
{
Appointment check = (Appointment)appointments.get(x);
for (int y = appointments.size() - 1; y > x; y--)
{
Appointment nextAppointment = (Appointment) appointments.get(y);
if (check.isOverlap(nextAppointment))
{ System.out.println("Duplicate appointments: ");
check.print();
nextAppointment.print();//??
}
}
}
}
/**
Prints the Calendar.
*/
public void print()
{ System.out.println(" C A L E N D A R");
System.out.println();
System.out.println("Starttime EndTime "
+ " Appointment");
//for (int i = 0; i < appointments.size(); i++)
//{ Appointment nextAppointment = (Appointment) appointments.get(i);
// nextAppointment.print();
//}
for (int x = 0; x < appointments.size(); x++)
{
Appointment check = (Appointment)appointments.get(x);
for (int y = appointments.size() - 1; y > x; y--)
{
Appointment nextAppointment = (Appointment) appointments.get(y);
if (check.sameDay(nextAppointment))
{ System.out.println("Duplicate appointments: ");
check.print();
nextAppointment.print();//??
}
}
}
}
private Vector appointments;
private String name;
private Appointment theAppointment;
}
/**
Describes an appointment.
*/
class Appointment
{
public Appointment(String aStarttime,String aEndtime, String anEvent)
{ starttime = aStarttime;
endtime = aEndtime;
event = anEvent;
SimpleDateFormat df = new SimpleDateFormat(PARSE_FORMAT);
SimpleDateFormat df2 = new SimpleDateFormat(PARSE_DATE);
try
{
start = df.parse(starttime);
end = df.parse(endtime);
startDay =df2.parse(starttime);
}
catch (ParseException e)
{ System.out.print("Please enter the date/time in the following format:"
+ "\n" + "dd/MM/yy hh:mm a" +"\n" );
System.exit(1);
}
}
/**
Method to obtain start date.
@return The start date.
*/
public Date getStart()
{
return start;
}
/**
Method to obtain end date.
@return The end date.
*/
public Date getEnd()
{
return end;
}
public Date getDay()
{
return startDay;
}
/**
Method to
test for overlapping appointment times.
@return true if the appointments overlap.False if
the appointment times do not overlap.
*/
public boolean isOverlap(Appointment nextApp)
{
long thisStart = start.getTime();
long thisEnd = end.getTime();
long anotherStart = nextApp.getStart().getTime();
long anotherEnd = nextApp.getEnd().getTime();
if ((thisStart > anotherEnd) || (thisEnd < anotherStart))
return false;
else
return true;
}
/**
Method to test whether one object equals another.
@param otherObject The other object.
@return true if equal, false if not
*/
public boolean equals(Object otherObject)
{
if (otherObject instanceof Appointment)
{ Appointment other = (Appointment)otherObject;
return (starttime.equals(other.starttime)
&& endtime.equals(other.endtime) && event.equals(other.event));
}
else return false;
}
public boolean sameDay(Appointment nextApp)
{
return (startDay.equals(nextApp.getDay())) ;
}
/**
Prints the Starttime, Endtime and a description of the
appointment.
*/
public void print()
{ System.out.println();
System.out.println(starttime + " " + endtime
+ " " + event);
System.out.println();
}
private String starttime;
private String endtime;
private String event;
private Date start;
private Date end;
private Date startDay;
private static final String PARSE_FORMAT = "dd/MM/yy hh:mm a";
private static final String PARSE_DATE = "dd/MM/yy";
}