By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,213 Members | 2,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,213 IT Pros & Developers. It's quick & easy.

Appointment Book

P: 13
I am just starting this "appointment book" java program and would appreciate some basis direction. I do not expect anyone to write the code for me but i would like opinions on how to best handle the situation. Specifically should i use <arraylist> or a hashtable or is these a better way? Thank you all in advance. here is the assignment... sorry if it is too long.

In this assignment you will demonstrate your understanding of designing with classes; using inheritance, polymorphism and abstract classes; and using the generic forms of utility classes provided by the Java Collections Framework (Java 1.5). The goal is to create simple calendar application that stores two different types of appointments: personal appointments and meetings. The detailed requirements for the application are below.
o AppointmentBook
o Event
o Appointment
o Meeting
o T_AppointmentBook
The T_AppointmentBook is the test harness and should have a main() method. The other four classes should not have a main() method. More requirements are listed below.

Class AppointmentBook
The AppointmentBook class should provide the ability to add and retrieve for events by name. Specifically, the class must have the following, and only the following, public operations:
o public AppointmentBook()

The constructor should just initialize the object and ensure it is ready to accept requests to add and retrieve events as well as determine conflicts.
o public boolean add(Event newEvent)

This operation will add the new event to the appointment book. The operation will return true if the addition was successful. If the event is already in the appointment book, the addition will not occur and false will be returned.
o public ArrayList<Event> getEventsForDate(GregorianCalendar date)

Returns the collection of events for a given date.
o public ArrayList<Event> findConflicts(Event event)

This operation searches the known events to see if any conflict exists (are scheduled at the same time) with the provided event. The operation will return an empty ArrayList if no matches are found.
The underlying implementation of AppointmentBook must utilize the generic versions of the Java Collections Framework. At a minimum the use of HashMap is required. (That is, you must make use of the collection HashMap<> but may find it useful to use other collections. ArrayList<> must be used as well since one of the operations returns an ArrayList<> to the caller.)

Class Event
The Event class must provide storage for the address and phone number of a contact. It must provide the following operations:
o public void setStartDateAndTime(GregorianCalendar newDateAndTime)
public GregorianCalendar getStartDateAndTime()

These operations will update and retrieve the starting point of the event.
o public void setStopDateAndTime(GregorianCalendar newDateAndTime)
public GregorianCalendar getStopDateAndTime()

These operations will update and retrieve the stopping point of the event.
o public void setEventTitle(String newTitle)
public String getEventTitle()

These operations will update and retrieve the title of the event.
Users of the class should not be able to create instances of the class as events need to have more accompanying information.

Classes Appointment and Meeting
The Appointment class represents a personal appointment whereas the Meeting class represents an appointment involving other people that the user would like to formally identify. We need to be able to add Appointments and Meetings to the AppointmentBook. Appointments and Meetings must have the same functionality as the Event class.
In addition...
o The Appointment class must be able to keep track of a user-defined category (such as "medical" or "personal").
o The Meeting class must be able to keep track of a collection of people who will be in attendance at the meeting. The names of people (simply use a String) can be added to the meeting as well as retrieved from the class as a list.

Class T_AppointmentBook
This class must reasonably exercise the various functionality of each of the AppointmentBook, Event, Appointment and Meeting classes.

Notes on GregorianCalendar
This class makes use of the class GregorianCalendar available in the package java.util. You can find out all you need to know about the GregorianCalendar from the Java API Documentation. As a quick start for the things you will find of interest for this assignment:
First step is to import the class:
import java.util.GregorianDate;

To create a new "date":
GregorianDate sixPMOnJuly52006 = new GregorianCalendar(2006, // year
GregorianCalendar.JULY, // month
5, // day
18, // hour
0); // minute

To retrieve the day of the month (1 to 31) or the year:
int day = somePointInTime.get(GregorianCalendar.DAY_OF_MONTH );
int year = somePointInTime.get(GregorianCalendar.YEAR);
You can also request the day in other contexts: DAY_OF_YEAR, DAY_OF_WEEK and DAY_OF_WEEK_IN_MONTH but they probably won't apply here.

To retrieve the month and compare it to a specific month:
int month = somePointInTime.get(GregorianCalendar.MONTH);

switch (month) {

case GregorianCalendar.MARCH:
case GregorianCalendar.APRIL:
case GregorianCalendar.MAY:
season = "spring";
break;

case GregorianCalendar.JUNE:
case GregorianCalendar.JULY:
case GregorianCalendar.AUGUST:
season = "summer";
break;

case GregorianCalendar.SEPTEMBER:
case GregorianCalendar.OCTOBER:
case GregorianCalendar.NOVEMBER:
season = "autumn";
break;

case GregorianCalendar.DECEMBER:
case GregorianCalendar.JANUARY:
case GregorianCalendar.FEBRUARY:
season = "winter";
break;
}
Note that the comparison is against the constants and no assumption about what the actual numerical value of the month is. This makes the code robust. If at some point Java decides to represent JANUARY with 1 rather than 0, this code will still work. If we hard coded in the integers, it might break.

To retrieve time values for hours (0 to 23) and minutes (0 to 59):
int hour = somePointInTime.get(GregorianCalendar.HOUR_OF_DAY) ;
int minute = somePointInTime.get(GregorianCalendar.MINUTE);
As with determining the day, you can also request the hour in a 12-hour format using HOUR rather that HOUR_OF_DAY. You can then request AM_PM in a fashion similar to retrieving the month. You will get an integer value that maps to one of the constants of GregorianCalendar.AM or GregorianCalendar.PM.
Nov 6 '06 #1
Share this Question
Share on Google+
13 Replies


10K+
P: 13,264
I am just starting this "appointment book" java program and would appreciate some basis direction. I do not expect anyone to write the code for me but i would like opinions on how to best handle the situation. Specifically should i use <arraylist> or a hashtable or is these a better way? Thank you all in advance. here is the assignment... sorry if it is too long.

In this assignment you will demonstrate your understanding of designing with classes; using inheritance, polymorphism and abstract classes; and using the generic forms of utility classes provided by the Java Collections Framework (Java 1.5). The goal is to create simple calendar application that stores two different types of appointments: personal appointments and meetings. The detailed requirements for the application are below.
o AppointmentBook
o Event
o Appointment
o Meeting
o T_AppointmentBook
The T_AppointmentBook is the test harness and should have a main() method. The other four classes should not have a main() method. More requirements are listed below.

Class AppointmentBook
The AppointmentBook class should provide the ability to add and retrieve for events by name. Specifically, the class must have the following, and only the following, public operations:
o public AppointmentBook()

The constructor should just initialize the object and ensure it is ready to accept requests to add and retrieve events as well as determine conflicts.
o public boolean add(Event newEvent)

This operation will add the new event to the appointment book. The operation will return true if the addition was successful. If the event is already in the appointment book, the addition will not occur and false will be returned.
o public ArrayList<Event> getEventsForDate(GregorianCalendar date)

Returns the collection of events for a given date.
o public ArrayList<Event> findConflicts(Event event)

This operation searches the known events to see if any conflict exists (are scheduled at the same time) with the provided event. The operation will return an empty ArrayList if no matches are found.
The underlying implementation of AppointmentBook must utilize the generic versions of the Java Collections Framework. At a minimum the use of HashMap is required. (That is, you must make use of the collection HashMap<> but may find it useful to use other collections. ArrayList<> must be used as well since one of the operations returns an ArrayList<> to the caller.)

Class Event
The Event class must provide storage for the address and phone number of a contact. It must provide the following operations:
o public void setStartDateAndTime(GregorianCalendar newDateAndTime)
public GregorianCalendar getStartDateAndTime()

These operations will update and retrieve the starting point of the event.
o public void setStopDateAndTime(GregorianCalendar newDateAndTime)
public GregorianCalendar getStopDateAndTime()

These operations will update and retrieve the stopping point of the event.
o public void setEventTitle(String newTitle)
public String getEventTitle()

These operations will update and retrieve the title of the event.
Users of the class should not be able to create instances of the class as events need to have more accompanying information.

Classes Appointment and Meeting
The Appointment class represents a personal appointment whereas the Meeting class represents an appointment involving other people that the user would like to formally identify. We need to be able to add Appointments and Meetings to the AppointmentBook. Appointments and Meetings must have the same functionality as the Event class.
In addition...
o The Appointment class must be able to keep track of a user-defined category (such as "medical" or "personal").
o The Meeting class must be able to keep track of a collection of people who will be in attendance at the meeting. The names of people (simply use a String) can be added to the meeting as well as retrieved from the class as a list.

Class T_AppointmentBook
This class must reasonably exercise the various functionality of each of the AppointmentBook, Event, Appointment and Meeting classes.

Notes on GregorianCalendar
This class makes use of the class GregorianCalendar available in the package java.util. You can find out all you need to know about the GregorianCalendar from the Java API Documentation. As a quick start for the things you will find of interest for this assignment:
First step is to import the class:
import java.util.GregorianDate;

To create a new "date":
GregorianDate sixPMOnJuly52006 = new GregorianCalendar(2006, // year
GregorianCalendar.JULY, // month
5, // day
18, // hour
0); // minute

To retrieve the day of the month (1 to 31) or the year:
int day = somePointInTime.get(GregorianCalendar.DAY_OF_MONTH );
int year = somePointInTime.get(GregorianCalendar.YEAR);
You can also request the day in other contexts: DAY_OF_YEAR, DAY_OF_WEEK and DAY_OF_WEEK_IN_MONTH but they probably won't apply here.

To retrieve the month and compare it to a specific month:
int month = somePointInTime.get(GregorianCalendar.MONTH);

switch (month) {

case GregorianCalendar.MARCH:
case GregorianCalendar.APRIL:
case GregorianCalendar.MAY:
season = "spring";
break;

case GregorianCalendar.JUNE:
case GregorianCalendar.JULY:
case GregorianCalendar.AUGUST:
season = "summer";
break;

case GregorianCalendar.SEPTEMBER:
case GregorianCalendar.OCTOBER:
case GregorianCalendar.NOVEMBER:
season = "autumn";
break;

case GregorianCalendar.DECEMBER:
case GregorianCalendar.JANUARY:
case GregorianCalendar.FEBRUARY:
season = "winter";
break;
}
Note that the comparison is against the constants and no assumption about what the actual numerical value of the month is. This makes the code robust. If at some point Java decides to represent JANUARY with 1 rather than 0, this code will still work. If we hard coded in the integers, it might break.

To retrieve time values for hours (0 to 23) and minutes (0 to 59):
int hour = somePointInTime.get(GregorianCalendar.HOUR_OF_DAY) ;
int minute = somePointInTime.get(GregorianCalendar.MINUTE);
As with determining the day, you can also request the hour in a 12-hour format using HOUR rather that HOUR_OF_DAY. You can then request AM_PM in a fashion similar to retrieving the month. You will get an integer value that maps to one of the constants of GregorianCalendar.AM or GregorianCalendar.PM.
This exercise will do well to introduce leaners to the core features of java.

You do not need a hashtable. The specs said use an ArrayList<Events>. You should start by noting the relationships between your objects. What in your opinion is the relationship between Event, Meeting and Appointment?
Nov 7 '06 #2

P: 13
It looks like AppointmentBook should be the parent, i think it should probably an abstract class. Then Event should extend AppBook, also abstract. Then Appointment and Meeting should both extend Event.


This exercise will do well to introduce leaners to the core features of java.

You do not need a hashtable. The specs said use an ArrayList<Events>. You should start by noting the relationships between your objects. What in your opinion is the relationship between Event, Meeting and Appointment?
Nov 7 '06 #3

10K+
P: 13,264
It looks like AppointmentBook should be the parent, i think it should probably an abstract class. Then Event should extend AppBook, also abstract. Then Appointment and Meeting should both extend Event.
You've got them a bit mixed up.

1)Correct on Event should be abstract("Users of the class should not be able to create instances of the class as events need to have more accompanying information.").

2) Event does not extend AppBook. The AppBook class does not extend any class and no class extends it. This is simply where data is stored. Like a mini-database. It strores Event objects inside its ArrayList<Event>.

3)Classes Appointment and Meeting are Events and so they each extend Event. There are the concrete(Instantiable ) classes for the abstract class Event.

4)Note that inside AppBook, we do not need separate ArrayLists for Appointment and for Meeting but just one for Event because both these objects are Events and so can be stored in ArrayList<Event>

Make sure you fully understand all this because it forms the basis of object-oriented design. Once you have a correct design the actual code writing will be a trivial exercise.
Nov 7 '06 #4

P: 13
AppBook being standalone makes sense as a storage area that the other classes can use, without repeating the code, to store info. Event seems to be more of a placeholder that provides the common functions for its children. Does # 4) mean that both Appts and Mtg will all be stored in a single arraylist? or that each will need to create a new instance of arraylist and be stored seperately?

What does this mean?
(" The T_AppointmentBook is the test harness and should have a main() method. The other four classes should not have a main() method. ")
Is this because the other classes are not actually executed, only the methods within them?


You've got them a bit mixed up.

1)Correct on Event should be abstract("Users of the class should not be able to create instances of the class as events need to have more accompanying information.").

2) Event does not extend AppBook. The AppBook class does not extend any class and no class extends it. This is simply where data is stored. Like a mini-database. It strores Event objects inside its ArrayList<Event>.

3)Classes Appointment and Meeting are Events and so they each extend Event. There are the concrete(Instantiable ) classes for the abstract class Event.

4)Note that inside AppBook, we do not need separate ArrayLists for Appointment and for Meeting but just one for Event because both these objects are Events and so can be stored in ArrayList<Event>

Make sure you fully understand all this because it forms the basis of object-oriented design. Once you have a correct design the actual code writing will be a trivial exercise.
Nov 7 '06 #5

10K+
P: 13,264
AppBook being standalone makes sense as a storage area that the other classes can use, without repeating the code, to store info. Event seems to be more of a placeholder that provides the common functions for its children. Does # 4) mean that both Appts and Mtg will all be stored in a single arraylist? or that each will need to create a new instance of arraylist and be stored seperately?

What does this mean?
(" The T_AppointmentBook is the test harness and should have a main() method. The other four classes should not have a main() method. ")
Is this because the other classes are not actually executed, only the methods within them?
Very slowly but surely you are begginning to get it.
Only one ArrayList that stores events will be created. This will store both appointments and meetings. This will work because these two are events (extend from Event).
T_AppointmentBook is the class you are going to use for testing the program.
All java programs start execution at the main method and for this program, our main will be in the T_AppointmentBook class. This will create instances of objects and call their various actions to show that the system works.
Nov 7 '06 #6

P: 13
You keep teachin and i'll keep learning... i really do appreciate your time and attention. alright so i think i might be ready to start coding. Where do you recomment i start? What/How do i need to be thinking about this? i'm thinkin to start with the AppBk class and build the 1st constructor using the GregCal info that was provided to create a storage area for the future events, but i need to import the util and create an arraylist before i can do that. then move on to the add method. Should i then start a test harness to test eash step or wait until the first class is done?
Nov 9 '06 #7

P: 13
will i need to apply the import statement on every class?
Nov 9 '06 #8

10K+
P: 13,264
will i need to apply the import statement on every class?
The more you think about the problem the clearer the solution becomes. You want to start by creating the AppBook Class but inside that class you need to define an ArrayList of Events and manipulate them. So you need the Events class first.

In general you consider coupling(You should look up this and make sure you understand the difference between loose coupling tight coupling).

Start with the classes that do not need to use other classes first. In practice, this may not always be possible so you choose those that depend on the least nimber of classes.

I would suggest the Event class first(remember this is abstract), followed by the two implementations of Event ie Appointment and Meeting.

Whether or not you want to test each class first depends on experience and style. Remeber some classe cannot be tested before others are complete. In the concept of Extreme Programming, tests are written first. In this case I would do the test class last because the correctness of most of your classes depend on the design of others.

Be sure of listing all the methods you need for each class first and check to see that your methods satisfy the requirements of the question.
Nov 10 '06 #9

P: 13
Went back and forth b/t a tutor and my teacher all night an i think i am pretty close. We wrote the event and children first and worked back from there, just as u suggested. Still cannot figure out how to find the conflicts and have not started a test harness yet. Had to use a hashmap b/c teacher said to do it that way. Want the prog to pull a list of events for a given date then compare time to find conflicts, we can assume that all events begin and end on the same day.
Let me show what i got and tell me what u think.

/////////////////////////////////////////////////////AppointmentBook
import java.util.GregorianCalendar;
import java.util.ArrayList;
import java.util.HashMap;

public class AppointmentBook {

HashMap <GregorianCalendar, ArrayList<Event>> calendar;


public AppointmentBook(String NewEvent){

//initialize variables
}

public boolean add(Event newEvent){
boolean success;
GregorianCalendar eventDate;
ArrayList<Event> currentList;
ArrayList<GregorianCalendar> listOfDates;

eventDate = newEvent.getStartDateAndTime();

currentList =calendar.get(eventDate);

if (currentList.contains(newEvent)){
success = false;
}
else{
currentList.add(newEvent);
calendar.remove(eventDate);
calendar.put(eventDate,currentList);
success = true;

}


return success;
}

public ArrayList<Event> getEventsForDate(GregorianCalendar date){
ArrayList<Event> dateList;

dateList = calendar.get(date);

return dateList;

}

public ArrayList<Event> findConflicts(Event event){
ArrayList<Event> dateList;


return dateList;

}

}

/////////////////////////////////////////////////////////////Event
import java.util.GregorianCalendar;

public abstract class Event{

GregorianCalendar startDate;
GregorianCalendar stopDate;
String eventTitle;

public void setStartDateAndTime(GregorianCalendar newDateAndTime){
startDate = newDateAndTime;

}

public GregorianCalendar getStartDateAndTime(){
return startDate;

}

public void setStopDateAndTime(GregorianCalendar newDateAndTime){
stopDate = newDateAndTime;
}

public GregorianCalendar getStopDateAndTime(){
return stopDate;

}

public void setEventTitle(String newTitle){
eventTitle = newTitle;
}

public String getEventTitle(){
return eventTitle;


}
}
////////////////////////////////////////////////////////Appointment
public class Appointment extends Event {


public String typeOfEvent;

public void SetType(String newType)
{
typeOfEvent = newType;
}

public String GetType(){
return typeOfEvent;
}

}

/////////////////////////////////////////////////////////////////////////Meeting
import java.util.ArrayList;

public class Meeting extends Event {

ArrayList<String> people = new ArrayList<String>();

public void AddPerson(String newPerson)
{
people.add(newPerson);
}

public ArrayList GetPeople(){
return people;
}



}
Nov 10 '06 #10

10K+
P: 13,264
You are doing a fine job so far. Not too far away from finishing too.

Expand|Select|Wrap|Line Numbers
  1.  
  2. /////////////////////////////////////////////////////AppointmentBook
  3. import java.util.GregorianCalendar;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.Set;
  7. import java.util.Iterator;
  8.  
  9. //Constructors define how you want the objects to be created 
  10.  
  11. class AppointmentBook {
  12.  
  13. HashMap <GregorianCalendar, ArrayList<Event>> calendar;
  14.  
  15. //No args constructor
  16. public AppointmentBook(){
  17.  calendar = new HashMap <GregorianCalendar, ArrayList<Event>>();
  18.  
  19.  
  20. }
  21.  
  22. //May need to add a copy constructor as well
  23.  
  24.  
  25. //Correct
  26. public boolean add(Event newEvent){
  27. boolean success;
  28. GregorianCalendar eventDate = newEvent.getStartDateAndTime();
  29. ArrayList<Event> currentList = calendar.get(eventDate);
  30. if(currentList != null) {
  31.  if (currentList.contains(newEvent)){//This was always going to give nullpointer on first call
  32.   success = false;
  33.  }
  34.  else{
  35.   currentList.add(newEvent);
  36.   calendar.remove(eventDate);
  37.   calendar.put(eventDate,currentList);
  38.   success = true;
  39.  
  40.  }
  41. }
  42. else {
  43.  currentList = new ArrayList<Event>();
  44.  currentList.add(newEvent);
  45.  calendar.remove(eventDate);
  46.  calendar.put(eventDate,currentList);
  47.  success = true;
  48. }
  49.  
  50. return success;
  51. }
  52.  
  53. //Correct
  54. public ArrayList<Event> getEventsForDate(GregorianCalendar date){
  55. ArrayList<Event> dateList = calendar.get(date);
  56. //If there are no entries, return an empty ArrayList
  57. if(dateList == null) {
  58.  dateList = new ArrayList<Event>();
  59. }
  60. return dateList;
  61.  
  62. }
  63.  
  64. public ArrayList<Event> findConflicts(Event event){
  65. GregorianCalendar conflictingDate = event.getStartDateAndTime();
  66. ArrayList<Event> dateList = getEventsForDate(conflictingDate);
  67. //If there are no entries, return an empty ArrayList. However see the specs for get at java.sun
  68. if(dateList == null) {
  69.  dateList = new ArrayList<Event>();
  70. }
  71. return dateList;
  72.  
  73. }
  74. public String toString() {
  75. String s = "";
  76. Set set = calendar.keySet();
  77. Iterator keys = set.iterator();
  78. while(keys.hasNext()) {
  79.  GregorianCalendar gc = (GregorianCalendar)keys.next();
  80.  s = s + gc.getTime() + "   :"+calendar.get(gc) + "\n";
  81. }
  82. return s;
  83. }
  84.  
  85. }
  86.  
  87. /////////////////////////////////////////////////////////////Event
  88.  
  89.  
  90. abstract class Event{
  91.  
  92. GregorianCalendar startDate;
  93. GregorianCalendar stopDate;
  94. String eventTitle;
  95.  
  96. //Default Constructor
  97. public Event() {
  98.  startDate = null;
  99.  stopDate = null;
  100.  eventTitle = "";
  101. }
  102.  
  103. //Constructor
  104. public Event(GregorianCalendar startDate, GregorianCalendar stopDate, String eventTitle) {
  105.  this.startDate = startDate;
  106.  this.stopDate = stopDate;
  107.  this.eventTitle = eventTitle;
  108. }
  109.  
  110. public void setStartDateAndTime(GregorianCalendar newDateAndTime){
  111. startDate = newDateAndTime;
  112.  
  113. }
  114.  
  115. public GregorianCalendar getStartDateAndTime(){
  116. return startDate;
  117.  
  118. }
  119.  
  120. public void setStopDateAndTime(GregorianCalendar newDateAndTime){
  121. stopDate = newDateAndTime;
  122. }
  123.  
  124. public GregorianCalendar getStopDateAndTime(){
  125. return stopDate;
  126.  
  127. }
  128.  
  129. public void setEventTitle(String newTitle){
  130. eventTitle = newTitle;
  131. }
  132.  
  133. public String getEventTitle(){
  134. return eventTitle;
  135.  
  136.  
  137. }
  138. }
  139. ////////////////////////////////////////////////////////Appointment
  140. //Specs say category not type of Event.
  141. //The type of this event is Appointment
  142. class Appointment extends Event {
  143.  public String category;
  144.  public Appointment(GregorianCalendar startDate, GregorianCalendar stopDate, String eventTitle, String category) {
  145.   super(startDate, stopDate, eventTitle);
  146.   this.category = category;
  147.  }
  148.  
  149.  
  150.  
  151.  
  152. public void setCategory(String newCategory)
  153. {
  154. category = newCategory;
  155. }
  156.  
  157. public String getCategory(){
  158. return category;
  159. }
  160. public String toString() {
  161.  return "AppointMent:" + getEventTitle() + " :"+category;
  162.  
  163. }
  164.  
  165. }
  166.  
  167. /////////////////////////////////////////////////////////////////////////Meeting
  168.  
  169. class Meeting extends Event {
  170.  
  171. ArrayList<String> people;
  172.  
  173. public Meeting() {
  174.  super();
  175.  people = new ArrayList<String>();
  176. }
  177. public Meeting(GregorianCalendar startDate, GregorianCalendar stopDate, String eventTitle, ArrayList<String> people) {
  178.  super();
  179.  this.people = people;
  180. }
  181. public void AddPerson(String newPerson)
  182. {
  183. people.add(newPerson);
  184. }
  185.  
  186. public ArrayList GetPeople(){
  187.  return people;
  188. }
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195. public class T_AppointmentBook {
  196.  public static void main(String[] args) {
  197.   AppointmentBook appBook = new AppointmentBook();
  198.   //The AppointMents are easy to test
  199.   for(int i = 0; i < 10; i++) {
  200.    appBook.add(new Appointment(new GregorianCalendar(2006, i*3, i), new GregorianCalendar(2006, i*3, i), "App01", "Personal"));
  201.   }
  202.   System.out.println(appBook.toString());
  203.  
  204.  }
  205. }
  206.  
  207.  
  208.  
Nov 11 '06 #11

10K+
P: 13,264
And to test the conflict check

Expand|Select|Wrap|Line Numbers
  1.  
  2. /////////////////////////////////////////////////////AppointmentBook
  3. import java.util.GregorianCalendar;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.Set;
  7. import java.util.Iterator;
  8. //Constructors define how you want the objects to be created
  9. class AppointmentBook { 
  10. HashMap <GregorianCalendar, ArrayList<Event>> calendar;
  11. //No args constructor
  12. public AppointmentBook(){
  13.  calendar = new HashMap <GregorianCalendar, ArrayList<Event>>();
  14.  
  15. }
  16. //May need to add a copy constructor as well
  17.  
  18. //Correct
  19. public boolean add(Event newEvent){
  20. boolean success;
  21. GregorianCalendar eventDate = newEvent.getStartDateAndTime();
  22. ArrayList<Event> currentList = calendar.get(eventDate);
  23. if(currentList != null) {
  24.  if (currentList.contains(newEvent)){//This was always going to give nullpointer on first call
  25.   success = false;
  26.  }
  27.  else{
  28.   currentList.add(newEvent);
  29.   calendar.remove(eventDate);
  30.   calendar.put(eventDate,currentList);
  31.   success = true;
  32.  }
  33. }
  34. else {
  35.  currentList = new ArrayList<Event>();
  36.  currentList.add(newEvent);
  37.  calendar.remove(eventDate);
  38.  calendar.put(eventDate,currentList);
  39.  success = true;
  40. }
  41. return success;
  42. }
  43. //Correct
  44. public ArrayList<Event> getEventsForDate(GregorianCalendar date){
  45. ArrayList<Event> dateList = calendar.get(date);
  46. //If there are no entries, return an empty ArrayList
  47. if(dateList == null) {
  48.  dateList = new ArrayList<Event>();
  49. }
  50. return dateList;
  51. }
  52. public ArrayList<Event> findConflicts(Event event){
  53. GregorianCalendar conflictingDate = event.getStartDateAndTime();
  54. ArrayList<Event> dateList = getEventsForDate(conflictingDate);
  55. //If there are no entries, return an empty ArrayList. However see the specs for get at java.sun
  56. if(dateList == null) {
  57.  dateList = new ArrayList<Event>();
  58. }
  59. return dateList;
  60. }
  61. public String toString() {
  62. String s = "";
  63. Set set = calendar.keySet();
  64. Iterator keys = set.iterator();
  65. while(keys.hasNext()) {
  66.  GregorianCalendar gc = (GregorianCalendar)keys.next();
  67.  s = s + gc.getTime() + "   :"+calendar.get(gc) + "\n";
  68. }
  69. return s;
  70. }
  71. }
  72. /////////////////////////////////////////////////////////////Event
  73.  
  74. abstract class Event{
  75. GregorianCalendar startDate;
  76. GregorianCalendar stopDate;
  77. String eventTitle;
  78. //Default Constructor
  79. public Event() {
  80.  startDate = null;
  81.  stopDate = null;
  82.  eventTitle = "";
  83. }
  84. //Constructor
  85. public Event(GregorianCalendar startDate, GregorianCalendar stopDate, String eventTitle) {
  86.  this.startDate = startDate;
  87.  this.stopDate = stopDate;
  88.  this.eventTitle = eventTitle;
  89. }
  90. public void setStartDateAndTime(GregorianCalendar newDateAndTime){
  91. startDate = newDateAndTime;
  92. }
  93. public GregorianCalendar getStartDateAndTime(){
  94. return startDate;
  95. }
  96. public void setStopDateAndTime(GregorianCalendar newDateAndTime){
  97. stopDate = newDateAndTime;
  98. }
  99. public GregorianCalendar getStopDateAndTime(){
  100. return stopDate;
  101. }
  102. public void setEventTitle(String newTitle){
  103. eventTitle = newTitle;
  104. }
  105. public String getEventTitle(){
  106. return eventTitle;
  107.  
  108. }
  109. }
  110. ////////////////////////////////////////////////////////Appointment
  111. //Specs say category not type of Event.
  112. //The type of this event is Appointment
  113. class Appointment extends Event {
  114.  public String category;
  115.  public Appointment(GregorianCalendar startDate, GregorianCalendar stopDate, String eventTitle, String category) {
  116.   super(startDate, stopDate, eventTitle);
  117.   this.category = category;
  118.  }
  119.  
  120.  
  121. public void setCategory(String newCategory)
  122. {
  123. category = newCategory;
  124. }
  125. public String getCategory(){
  126. return category;
  127. }
  128. public String toString() {
  129.  return "AppointMent:" + getEventTitle() + " :"+category;
  130. }
  131. }
  132. /////////////////////////////////////////////////////////////////////////Meeting
  133. class Meeting extends Event {
  134. ArrayList<String> people;
  135. public Meeting() {
  136.  super();
  137.  people = new ArrayList<String>();
  138. }
  139. public Meeting(GregorianCalendar startDate, GregorianCalendar stopDate, String eventTitle, ArrayList<String> people) {
  140.  super();
  141.  this.people = people;
  142. }
  143. public void AddPerson(String newPerson)
  144. {
  145. people.add(newPerson);
  146. }
  147. public ArrayList GetPeople(){
  148.  return people;
  149. }
  150. }
  151.  
  152.  
  153. public class T_AppointmentBook {
  154.  public static void main(String[] args) {
  155.   AppointmentBook appBook = new AppointmentBook();
  156.   //The AppointMents are easy to test
  157.   for(int i = 0; i < 10; i++) {
  158.    appBook.add(new Appointment(new GregorianCalendar(2006, i*3, i), new GregorianCalendar(2006, i*3, i), "App"+i, "Personal"));
  159.   }
  160.   Event a = new Appointment(new GregorianCalendar(2006, 4*3, 4), new GregorianCalendar(2006, 4*3, 4), "App"+12, "Personal");
  161.   System.out.println(appBook.toString());
  162.   ArrayList<Event> conflicts = appBook.findConflicts(a);
  163.   System.out.println(conflicts);
  164.  }
  165. }
  166.  
  167.  
Nov 11 '06 #12

P: 13
You make it look so easy. thank you. after taking a while to figure out how and why the prog works, i almost understand. i muttled through the rest and it works fine. thanx again.
Next project is using java to pull info from a xml document, can / will u still help me or do i need to post somewhwere other than the java page?
Nov 15 '06 #13

10K+
P: 13,264
You make it look so easy. thank you. after taking a while to figure out how and why the prog works, i almost understand. i muttled through the rest and it works fine. thanx again.
Next project is using java to pull info from a xml document, can / will u still help me or do i need to post somewhwere other than the java page?
Sure you can post in this forum as well. You'd have to post a new thread for that. Even if I may not be able to help, there are still some guys here who can help you out
Nov 15 '06 #14

Post your reply

Sign in to post your reply or Sign up for a free account.