In: Computer Science
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[]>();
}
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;
}
}