Question

In: Computer Science

Calendar Program Write a program to design an appointment calendar. An appointment includes a description, date,...

Calendar Program

Write a program to design an appointment calendar. An appointment includes a description, date, starting time, and ending time. Supply a user interface to add appointments, remove canceled appointments, and print out a list of appointments for a particular day.   Appointments should not be duplicated for a given time and date.

Include a class AppointmentMenu to print the menu to the screen and accept user input. AppointmentMenu creates an AppointmentCalendar and then calls the appropriate methods to add, cancel, show, or quit.

Include a class AppointmentCalendar that is not coupled with the Scanner or PrintStream classes. (In other words, all println statements should be in AppointmentMenu.) AppointmentCalendar will include an arrayList of Appointments, a method to add Appointments, a method to cancel Appointments, and return the Appointments for a selected day.

Include a class Appointments which holds the input of description, date, and time. It should have a method to format the output for printing by the AppointmentMenu, a method to check to see if two appointments are equal so an appointment is not duplicated for a given date and time, and a method to check for a certain date when show and cancel are called.   

Be sure to include any other methods including any accessor methods that you need to complete this program using the design process described in Chapter 12.

Your main class should be called AppointmentSystem. Use the code below:

/**
 A system to manage appointments.
*/
public class AppointmentSystem { 
   public static void main(String[] args) {
      AppointmentMenu menu = new AppointmentMenu();
      menu.run();
   }
}


Here is a sample program run. (bold items are sample user inputs).

A)dd  C)ancel  S)how  Q)uit
A
Appointment (Description Date From To)
Dentist 
01/10/2007 
17:30 
18:30

A)dd  C)ancel  S)how  Q)uit
A
Appointment (Description Date From To)
Sweets Cafe 
01/10/2007
19:00 
20:00

A)dd  C)ancel  S)how  Q)uit
A
Appointment (Description Date From To)
CS1 class 
02/10/2007
08:30 
10:00

A)dd  C)ancel  S)how  Q)uit
S
Show appointments for which date?
01/10/2007
Appointments:
Dentist 01/10/2007 17:30 18:30
Sweets Cafe 01/10/2007 19:00 20:00

A)dd  C)ancel  S)how  Q)uit
C
Cancel appointments on which date?
01/10/2007
1) Dentist 01/10/2007 17:30 18:30
2) Sweets Cafe 01/10/2007 19:00 20:00
Which would you like to cancel? 
1

A)dd  C)ancel  S)how  Q)uit
S
Show appointments for which date?
01/10/2007
Appointments:
Sweets Cafe 01/10/2007 19:00 20:00

A)dd  C)ancel  S)how  Q)uit
A
Appointment (Description Date From To)
Sweets Cafe 
01/10/2007
19:00 
20:00
This appointment already exists.

A)dd  C)ancel  S)how  Q)uit
Q

How do I write this program in JAVA not C++ ? Thanks

Also DON'T

import java.io.IOException;

import java.sql.SQLException;

I can only import the scanner class, and ArrayLists and Arrays

What I've done so far is

/**
A system to manage appointments.
*/
public class AppointmentSystem {

public static void main(String[] args) {

AppointmentMenu menu = new AppointmentMenu();

menu.run();
}
}

import java.util.Scanner;
public class AppointmentMenu extends AppointmentCalendar{

public void run(){
Scanner in = new Scanner(System.in);

System.out.println("A)dd C)ancel S)how Q)uit");

String choice = in.nextLine(); {


if (choice.equalsIgnoreCase("A")){
super.addApt();
System.out.println("Appointment(Description Date From To)");
}


if (choice.equalsIgnoreCase("C")){
super.cancelApt();
System.out.println("Cancel appointments on which date?");
}


if (choice.equalsIgnoreCase("S")){
super.showApt();
System.out.println("Show appointments for which date?");
}


if (choice.equalsIgnoreCase("Q"))
return;


}




}

}

import java.util.ArrayList;

public class AppointmentCalendar extends Appointments{
private ArrayList<String[]> appointments = new ArrayList<String[]>();

public void addApt(){

}

public void cancelApt(){

}

public void showApt(){

}




}

import java.util.ArrayList;
import java.util.Scanner;

public class Appointments {

private ArrayList<String[]> description = new ArrayList<String[]>();
private ArrayList<String[]> dates = new ArrayList<String[]>();
private ArrayList<String[]> times = new ArrayList<String[]>();




}

Solutions

Expert Solution

AppointmentSystem.java


/**
* A system to manage appointments.
*/
public class AppointmentSystem
{
   public static void main(String[] args)
   {
       AppointmentMenu menu = new AppointmentMenu();
       menu.run();
   }
}

AppointmentMenu.java


import java.util.List;
import java.util.Scanner;


public class AppointmentMenu
{
   private static final String[] MENU_OPTIONS = { "Add", "Cancel", "Show", "Quit" };
   private static final int ADD_INDEX = 0;
   private static final int CANCEL_INDEX = 1;
   private static final int SHOW_INDEX = 2;
   private static final int QUIT_INDEX = 3;
  
   private AppointmentCalendar calendar;
   private UIHandler uih;
  
   /**
   * Initialize the objects used in the program.
   */
   private void init()
   {
       calendar = new AppointmentCalendar();
       uih = new UIHandler(new Scanner(System.in), System.out);
   }
  

   public void run()
   {
       init();
      
       // Put the program in a main loop so that the program keeps running until the user quits.
       boolean shouldContinue = true;
       while (shouldContinue)
       {
           // Get user input
           int result = uih.getChoice(MENU_OPTIONS);
          
           // Split cases for user input.
           if (result == ADD_INDEX) {
               addAction();
           }
           else if (result == CANCEL_INDEX) {
               cancelAction();
           }
           else if (result == SHOW_INDEX) {
               showAction();
           }
           else if (result == QUIT_INDEX) {
               // User requested to quit, change shouldContinue to false.
               shouldContinue = false;
           }
          
           // Add spacing
           uih.display("");
       }
   }
  
   // All the related prompts for adding an appointment.
   private static final String ADD_PROMPT = "Appointment (Description Date From To)";
   private static final String APPOINTMENT_ALREADY_EXISTS = "This appointment already exists.";
  
   /**
   * Handles interaction with the user when the user requests to add a appointment.
   */
   private void addAction()
   {
       // User inputs 4 lines:description, date, start, end.
       String[] input = uih.getInput(ADD_PROMPT, 4);
      
       // The addAppointment method in AppointmentCalendar does not add and returns false if the appointment already exists. We can
       // use that.
       if (!calendar.addAppointment(new Appointments(input[0],input[1], input[2], input[3])))
       {
           uih.display(APPOINTMENT_ALREADY_EXISTS);
       }
      
       // Method returns, control goes back to run.
   }
  
   // All the related prompts for canceling an appointment.
   private static final String CANCEL_PROMPT = "Cancel appointments on which date?";
   private static final String CANCEL_INDEX_REQUEST = "Which would you like to cancel?";
   private static final String INDEX_OUT_OF_BOUNDS_MSG = "The index entered is not in the list. No appointment is cancelled.";
  
  
   private void cancelAction()
   {
       // Get the date of the appointment from user
       String date = uih.getInput(CANCEL_PROMPT);
      
       // Get all the appointments on that date.
       List<Appointments> allApps = calendar.findAppointmentOnDate(date);
       // Display the appointments.
       for (int i = 0; i < allApps.size(); i++)
       {
           uih.display(Integer.toString(i + 1) + ") " + allApps.get(i).toString());
       }
      
       // Ask which appointment to cancel.
       String input = uih.getInput(CANCEL_INDEX_REQUEST);
       int index = Integer.parseInt(input) - 1;
      
       // Prevent index out of bounds error.
       if (index < 0 || index >= allApps.size()) {
           uih.display(INDEX_OUT_OF_BOUNDS_MSG);
           return;
       }
      
       // Cancel the appointment.
       calendar.cancelAppointment(allApps.get(index));
   }
  
   // All the related prompts for showing appointments.
   private static final String SHOW_PROMPT = "Show appointments for which date?";
   private static final String SHOW_HEAD = "Appointments:";
  
   /**
   * Handles interaction with the user when the user requests to show appointments.
   */
   private void showAction()
   {
       // Get the date of the appointment from user
       String date = uih.getInput(SHOW_PROMPT);
      
       // Get all the appointments on that date.
       List<Appointments> allApps = calendar.findAppointmentOnDate(date);
       // Display the appointments.
       uih.display(SHOW_HEAD);
       for (int i = 0; i < allApps.size(); i++)
       {
           uih.display(allApps.get(i).toString());
       }
   }
}


AppointmentCalendar.java

import java.util.ArrayList;
import java.util.List;

public class AppointmentCalendar
{
   private List<Appointments> appointmentList;
  
   public AppointmentCalendar()
   {
       appointmentList = new ArrayList<Appointments>();
   }
  

   private boolean hasAppointment(Appointments app)
   {
       for (Appointments appointment : appointmentList)
       {
           if (appointment.equals(app))
           {
               // Appointment already exists.
               return true;
           }
       }
      
       return false;
   }
  
  
   public boolean addAppointment(Appointments app)
   {
       if (hasAppointment(app)) {
           // Already has this appointment
           return false;
       }
      
       // Doesn't have this appointment
       appointmentList.add(app);
       return true;
   }
  
   /**
   * Cancels an appointment from the calendar.
   *
   * @param app The appointment to cancel.
   */
   public void cancelAppointment(Appointments app)
   {
       // Search through the list and find the
       for (int i = 0; i < appointmentList.size(); i ++)
       {
           if (app.equals(appointmentList.get(i))) {
               // This is the appointment to cancel.
               appointmentList.remove(i);
               return;
           }
       }
   }
  
  
   public List<Appointments> findAppointmentOnDate(String date)
   {
       // The list to store the appointments.
       List<Appointments> appsOnDate = new ArrayList<Appointments>();
      
       // Go through the list and find all appointments with hte same date.
       for (Appointments app : appointmentList)
       {
           if (app.isOnDate(date)) {
               appsOnDate.add(app);
           }
       }
      
       return appsOnDate;
   }
}

Appointments.java

public class Appointments
{
   private String description;
   private String date;
   private String startTime;
   private String endTime;
  
   public Appointments(String desc, String appointmentDate, String start, String end)
   {
       description = desc;
       date = appointmentDate;
       startTime = start;
       endTime = end;
   }
  

   public boolean isOnDate(String onDate)
   {
       return date.equals(onDate);
   }
  
   // If all four instance fields are equal, the appointments are equal.
   public boolean equals(Object obj)
   {
       if (obj instanceof Appointments)
       {
           // The object is of this class and we can check the instance fields.
           Appointments app = (Appointments) obj;
           return (description.equals(app.description) && date.equals(app.date) && startTime.equals(app.startTime) && endTime
                   .equals(app.endTime));
       }
      
       // The object is not even of this class. The two are not equal.
       return false;
   }
  
   @Override
   public String toString()
   {
       return (description + " " + date + " " + startTime + " " + endTime);
   }
  
}


UIHandler.java

import java.io.PrintStream;
import java.util.Scanner;


public class UIHandler
{
   private static final String UNKNOWN_RESPONSE = "Unknown command: ";
  
   private Scanner in;
   private PrintStream out;
  
  
   public UIHandler(Scanner input, PrintStream output)
   {
       in = input;
       out = output;
   }
  
   /**
   * Display something to the user.
   *
   * @param content The content to display
   */
   public void display(String content)
   {
       // I can also redirect this to the getInput method.
       getInput(content, 0);
   }
  
  
   public String getInput(String prompt)
   {
       // Redirect to the more general method
       return getInput(prompt, 1)[0];
   }
  
  
   public String[] getInput(String prompt, int linesExpected)
   {
       // Prompt the user.
       out.println(prompt);
      
       // Get the user input.
       String[] userInput = new String[linesExpected];
       for (int i = 0; i < linesExpected; i++) {
           userInput[i] = in.nextLine();
       }
      
       return userInput;
   }
  
  
   public int getChoice(String[] options)
   {
       // Have an array that stores the keys for faster access.
       int length = options.length;
       String[] keyArray = new String[length];
      
       // Print the options, and then start a new line for user input.
       for (int i = 0; i < length; i++)
       {
           // Form the options menu that is for display.
           String opt = options[i];
           String disp = opt.substring(0, 1) + ")" + opt.substring(1);
           out.print(disp + " ");
           // Also save the key for later use.
           keyArray[i] = opt.substring(0, 1);
       }
      
       // Accept user input.
       String input = getInput(""); // Use empty prompt
      
       // Find the matching key.
       for (int i = 0; i < keyArray.length; i++)
       {
           if (keyArray[i].equals(input)) {
               // User input matches this key, return the index.
               return i;
           }
       }
      
       // If the function haven't returned yet, the input does not match any known command.
       out.println(UNKNOWN_RESPONSE + input);
       return -1;
   }
}



Related Solutions

Write a C# program that prints a calendar for a given year. Call this program calendar....
Write a C# program that prints a calendar for a given year. Call this program calendar. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:            0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...
I got this homework, to make an appointment program. it says like this: Write a superclass...
I got this homework, to make an appointment program. it says like this: Write a superclass Appointment and subclasses OneTime, Daily, and Monthly. An Appointment has a description (for example “see the dentist”) and a date. Write a method occursOn(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. You are provided with a Junit test AppointmentTest .java. Run...
By the INITIAL POST date in the course Calendar you should write a post that answers...
By the INITIAL POST date in the course Calendar you should write a post that answers these questions, using information from the textbook and other reputable sources. By the FINAL POST date in the course Calendar you must reply to AT LEAST two other posts on the board, using the "yes and..." format. This means that in each of your responses to others, be sure to include some additional thoughts, comments, or information that further the discussion. IN ALL posts...
For this exercise, consider the situation of developing the “New Appointment” page for a calendar app....
For this exercise, consider the situation of developing the “New Appointment” page for a calendar app. In this scenario you are tasked with just the HTML — web designers will style the page with CSS, and the back-end developers will write the logic that connects it to the database. You will be graded on your ability to use HTML correctly, not on your artistic or creative ability. Feel free to make any creative modifications to the page, as long as...
PYTHON PROGRAM: Write a program that determines the day of the week for any given calendar...
PYTHON PROGRAM: Write a program that determines the day of the week for any given calendar date after January 1, 1900, which was a Monday. This program will need to account for leap years, which occur in every year that is divisible by 4, except for years that are divisible by 100 but are not divisible by 400. For example, 1900 was not a leap year, but 2000 was a leap year.
Why are appointment systems used? What decisions are necessary to design an appointment system? Describe some...
Why are appointment systems used? What decisions are necessary to design an appointment system? Describe some practical examples of the single-resource sequencing problem.
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates...
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates an exception class called ManyCharactersException, designed to be thrown when a string is discovered that has too many characters in it. Consider a program that takes in the last names of users. The driver class of the program reads the names (strings) from the user until the user enters "DONE". If a name is entered that has more than 20 characters, throw the exception....
EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates  ...
EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to be thrown when a   string is discovered that has too many characters in it. Consider a   program that   takes   in the last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,   throw   the   exception.  ...
DESIGN A FLOWCHART IN FLOWGORITHM AND WRITE THE PSEUDOCODE Number Analysis Program Design a program that...
DESIGN A FLOWCHART IN FLOWGORITHM AND WRITE THE PSEUDOCODE Number Analysis Program Design a program that asks the user to enter a series of 20 numbers. The program should store the numbers in an array and then display the following data: The lowest number in the array. The highest number in the array. The total of the numbers in the array. The average of the numbers in the array. PLEASE AND THANK YOU
write a program on c++ that outputs a calendar for a given month in a given...
write a program on c++ that outputs a calendar for a given month in a given year, given the day of the week on which the 1st of the month was. The information in numeric format (months are: 1=Januay, 2=February 3=March, 4= April, 5= May, 6= June, 7= July... etc days are 1=Sunday, 2=Monday, 3= Tuesday, 4= Wednesday, 5= Thursday, 6= Friday and 7= Saturday ). The program output that month’s calendar, followed by a sentence indicating on which day...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT