In: Computer Science
Can someone check to see if anything needs to be changed from this code? The instructions are confusing and I'm not sure if this fulfills everything because my compiler won't run it properly.
DIRECTIONS:
In this project we will develop classes to implement a
Day Planner program. Be sure to develop the code
in a step by step manner, finish phase 1 before moving on to phase
2.
Phase 1
The class Appointment is essentially a record; an object built from this class will represent an Appointment in a Day Planner . It will contain 5 fields: month (3 character String), day (int), hour (int), minute (int), and message (String no longer then 40 characters).
Write 5 get methods and 5 set methods, one for each data field. Make sure the set methods verify the data. (E.G. month is a valid 3 letter code). Simple error messages should be displayed when data is invalid, and the current value should NOT change.
Write 2 constructor methods for the class Appointment . One with NO parameters that assigns default values to each field and one with 5 parameters that assigns the values passed to each field. If you call the set methods in the constructor(s) you will NOT need to repeat the data checks.
Write a toString method for the class Appointment . It should create and return a nicely formatted string with ALL 5 fields. Pay attention to the time portion of the data, be sure to format it like the time should be formatted ( HH : MM ) , a simple if-else statement could add a leading zero, if needed.
Write a method inputAppointment () that will use the class UserInput from a previous project, ask the user to input the information and assign the data fields with the users input. Make sure you call the UserInput methods that CHECK the min/max of the input AND call the set methods to make sure the fields are valid.
Write a main() method, should be easy if you have created the methods above, it creates a Appointment object, calls the inputAppointment () method to input values and uses the method toString() print a nicely formatted Appointment object to the screen. As a test, use the constructor with 5 parameters to create a second object (you decide the values to pass) and print the second object to the screen. The primary purpose of this main() method is to test the methods you have created in the Appointment class.
Phase 2
Create a class Planner , in the data area of the class declare an array of 20 Appointment objects. Make sure the array is private (data abstraction).
In this project we are going to build a simple Day Planner program that allow the user to create various Appointment objects and will insert each into an array. Be sure to insert each Appointment object into the array in the proper position, according to the date and time of the Appointment . This means the earliest Appointment object should be at the start of the array, and the last Appointment object at the end of the array.
Please pre load your array with the following
Appointment objects:
Mar 4,
17:30 Quiz 1
Apr 1, 17:30
Midterm
May 6, 17:30
Quiz 2
Jun 3, 17:30
Final
Notice how the objects are ordered, with the earliest date at the start of the array and the latest at the end of the array.
The program will display the following menu and implement these features:
A)dd Appointment , D)elete Appointment , L)ist Appointment , E)xit
Some methods you must implement in the Planner class for this project:
You may add additional methods to the Planner and Appointment classes as long as you clearly document 'what' and 'why' you added the method at the top of the class. The Appointment class could use one or more constructor methods. DO NOT in any way modify the UserInput class. If it so much as refers to a day or month or anything else in the Planner or Appointment class there will be a major point deduction.
Appointment.java
import java.util.Scanner;
public class Appointment {
public static void main(String[] args) {
}
private String month;
private int day;
private int hour;
private int minute;
private String message;
public String getMonth() {
return month;
}
public void setMonth(String month) {
if(month.length()!=3) {
System.out.println("Month should be 3 letter string");
this.month = "Jan";
}
else {
this.month = month;
}
}
public int getDay() {
return day;
}
public void setDay(int day) {
if(month=="Feb") {
if(day<=28 && day>=1) {
this.day = day;
}
else {
System.out.println("Enter a day (1-28");
this.day =01;
}
}
else if(month=="Apr" || month=="Jun" || month=="Sep" ||
month=="Nov") {
if(day<=30 && day>=1) {
this.day = day;
}
else {
System.out.println("Enter a day 1-30");
this.day =01;
}
}
else {
if(day<=31 && day>=1) {
this.day = day;
}
else {
System.out.println("Enter a day 1-31");
this.day =01;
}
}
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
if(hour<=24 && hour>=0) {
this.hour = hour;
}
else {
System.out.println("Hour must be between 0 and 24.");
this.hour = 00;
}
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
if(minute<60 && minute>=0) {
this.minute= minute;
}
else {
System.out.println("Minute must be between 0 and 59.");
this.minute = 00;
}
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
if(message.length()<=40) {
this.message = message;
}
else {
System.out.println("Msg length max is 40");
this.message=message.substring(0, 40);
}
}
public Appointment() {
month="Jan";
day=01;
hour=00;
minute=00;
message="";
}
public Appointment(String month,int day,int hour,int minute,String
message) {
setMonth(month);
setDay(day);
setHour(hour);
setMinute(minute);
setMessage(message);
}
public String toString() {
String str=month+" ";
str+=Integer.toString(day);
str+=", ";
if(hour<10) {
String padded ="0"+Integer.toString(hour);
str+=padded;
}
else {
str+=Integer.toString(hour);
}
str+=":";
if(minute<10) {
String padded ="0"+Integer.toString(minute);
str+=padded;
}
else {
str+=Integer.toString(minute);
}
str+=" "+message;
return str;
}
public int compareTo(Appointment app) {
String monthArray[]=
{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int ind1 = 0,ind2 = 0;
for(int i=0;i<12;i++) {
if(monthArray[i].equalsIgnoreCase(month)) {
ind1=i;
}
if(monthArray[i].equalsIgnoreCase(app.getMonth())) {
ind2=i;
}
}
if(ind1<ind2) {
return 1;
}
else if(ind1==ind2) {
if(this.day<app.getDay()) {
return 1;
}
else if(this.getDay()>app.getDay()) {
return -1;
}
else {
if(this.getHour()<app.getHour()) {
return 1;
}
else if(this.getHour()>app.getHour()) {
return -1;
}
else {
if(this.getMinute()<app.getMinute()) {
return 1;
}
else if(this.getMinute()>app.getMinute()) {
return -1;
}
else {
return 0;
}
}
}
}
else {
return -1;
}
}
}
Planner.java
public class Planner {
public static void main(String[] args) {
}
private Appointment[] appointments;
private int cnt;
public Planner() {
appointments=new Appointment[20]; cnt=0;
Appointment a1=new Appointment("Mar",4,17,30,"Quiz 1");
appointments[cnt]=a1; cnt++;
a1=new Appointment("Apr",1,17,30,"Midterm");
appointments[cnt]=a1; cnt++;
a1=new Appointment("May",6,17,30,"QUIZ 2");
appointments[cnt]=a1; cnt++;
a1=new Appointment("Jun",3,17,30,"Final");
appointments[cnt]=a1; cnt++;
}
public void printAppointments() {
for(int i=0;i<cnt;i++) {
System.out.println(appointments[i]);
}
}
public void inputAppointment(Appointment app) {
boolean flag=false;
if(cnt<19) {
for(int i=0;i<cnt;i++) {
if(appointments[i].compareTo(app)==-1) {
for(int j=cnt;j>i;j--) {
appointments[j]=appointments[j-1];
}
appointments[i]=app;
cnt++;
flag=true;
break;
}
else if(appointments[i].compareTo(app)==0) {
for(int j=cnt;j>i;j--) {
appointments[j]=appointments[j-1];
}
appointments[i]=app;
cnt++;
flag=true;
break;
}
if(flag==false) {
appointments[cnt]=app;
cnt++;
}
}
}
else {
System.out.println("\nAll appointments are filled.\n");
}
}
public void deleteAppointment() {
if(cnt>=0) {
for(int i=0;i<cnt;i++) {
appointments[i]=appointments[i+1];
}
cnt--;
System.out.println("First appointment deleted.");
}
else {
System.out.println("There are no appointments in the
schedule.");
}
}
}
Appointment.java
import java.util.Scanner;
public class Appointment {
public static void main(String[] args) {
}
private String month;
private int day;
private int hour;
private int minute;
private String message;
public String getMonth() {
return month;
}
public void setMonth(String month) {
if(month.length()!=3) {
System.out.println("Month should be 3 letter string");
this.month = "Jan";
}
else {
this.month = month;
}
}
public int getDay() {
return day;
}
public void setDay(int day) {
if(month=="Feb") {
if(day<=28 && day>=1) {
this.day = day;
}
else {
System.out.println("Enter a day (1-28");
this.day =01;
}
}
else if(month=="Apr" || month=="Jun" || month=="Sep" ||
month=="Nov") {
if(day<=30 && day>=1) {
this.day = day;
}
else {
System.out.println("Enter a day 1-30");
this.day =01;
}
}
else {
if(day<=31 && day>=1) {
this.day = day;
}
else {
System.out.println("Enter a day 1-31");
this.day =01;
}
}
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
if(hour<=24 && hour>=0) {
this.hour = hour;
}
else {
System.out.println("Hour must be between 0 and 24.");
this.hour = 00;
}
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
if(minute<60 && minute>=0) {
this.minute= minute;
}
else {
System.out.println("Minute must be between 0 and 59.");
this.minute = 00;
}
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
if(message.length()<=40) {
this.message = message;
}
else {
System.out.println("Msg length max is 40");
this.message=message.substring(0, 40);
}
}
public Appointment() {
month="Jan";
day=01;
hour=00;
minute=00;
message="";
}
public Appointment(String month,int day,int hour,int minute,String
message) {
setMonth(month);
setDay(day);
setHour(hour);
setMinute(minute);
setMessage(message);
}
public String toString() {
String str=month+" ";
str+=Integer.toString(day);
str+=", ";
if(hour<10) {
String padded ="0"+Integer.toString(hour);
str+=padded;
}
else {
str+=Integer.toString(hour);
}
str+=":";
if(minute<10) {
String padded ="0"+Integer.toString(minute);
str+=padded;
}
else {
str+=Integer.toString(minute);
}
str+=" "+message;
return str;
}
public int compareTo(Appointment app) {
String monthArray[]=
{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int ind1 = 0,ind2 = 0;
for(int i=0;i<12;i++) {
if(monthArray[i].equalsIgnoreCase(month)) {
ind1=i;
}
if(monthArray[i].equalsIgnoreCase(app.getMonth())) {
ind2=i;
}
}
if(ind1<ind2) {
return 1;
}
else if(ind1==ind2) {
if(this.day<app.getDay()) {
return 1;
}
else if(this.getDay()>app.getDay()) {
return -1;
}
else {
if(this.getHour()<app.getHour()) {
return 1;
}
else if(this.getHour()>app.getHour()) {
return -1;
}
else {
if(this.getMinute()<app.getMinute()) {
return 1;
}
else if(this.getMinute()>app.getMinute()) {
return -1;
}
else {
return 0;
}
}
}
}
else {
return -1;
}
}
}
Planner.java
import java.util.*;
public class Planner {
public static void main(String[] args) {
//create the planner object
Planner obj = new Planner();
char ch;
Scanner sc = new Scanner(System.in);
while(true)
{
//prompt the user to enter an option
System.out.print("A)dd Appointment \n D)elete
Appointment \n L)ist Appointment \n E)xit\nEnter your
choice:\t");
ch = sc.nextLine().charAt(0);
//if the choice is A
if(ch == 'A')
{
//prompt the user to enter all the
fields
System.out.print("Enter
Month:\t");
String month = sc.nextLine();
System.out.print("Enter
Day:\t");
int day = sc.nextInt();
System.out.print("Enter
hours:\t");
int hours = sc.nextInt();
System.out.print("Enter
minutes:\t");
int minutes = sc.nextInt();
sc.nextLine();
System.out.print("Enter
Message:\t");
String message =
sc.nextLine();
//create the object for
appointment
Appointment app = new
Appointment(month, day, hours, minutes, message);
//insert the obect into the
array
obj.inputAppointment(app);
}
//if the choice is D
else if(ch == 'D')
{
//delete the first object
obj.deleteAppointment();
}
//if the choice is L
else if(ch == 'L')
{
//list all the objects
obj.printAppointments();
}
//if the choice is E, exit
else if(ch == 'E')
{
break;
}
else
{
System.out.println("Invalid
choice");
}
}
}
private Appointment[] appointments;
private int cnt;
public Planner() {
appointments=new Appointment[20]; cnt=0;
Appointment a1=new Appointment("Mar",4,17,30,"Quiz 1");
appointments[cnt]=a1; cnt++;
a1=new Appointment("Apr",1,17,30,"Midterm");
appointments[cnt]=a1; cnt++;
a1=new Appointment("May",6,17,30,"QUIZ 2");
appointments[cnt]=a1; cnt++;
a1=new Appointment("Jun",3,17,30,"Final");
appointments[cnt]=a1; cnt++;
}
public void printAppointments() {
for(int i=0;i<cnt;i++) {
System.out.println(appointments[i]);
}
}
public void inputAppointment(Appointment app) {
boolean flag=false;
if(cnt<19) {
for(int i=0;i<cnt;i++) {
//if the current object is greater
than or equal to the given object(app)
//inset the app before the current
object
if(appointments[i].compareTo(app)==-1 ||
appointments[i].compareTo(app)==0) {
for(int
j=cnt;j>i;j--) {
appointments[j]=appointments[j-1];
}
appointments[i]=app;
cnt++;
flag=true;
break;
}
}
if(!flag) {
appointments[cnt]=app;
cnt++;
}
}
else {
System.out.println("\nAll appointments are filled.\n");
}
}
public void deleteAppointment() {
if(cnt>=0) {
for(int i=0;i<cnt;i++) {
appointments[i]=appointments[i+1];
}
cnt--;
System.out.println("First appointment deleted.");
}
else {
System.out.println("There are no appointments in the
schedule.");
}
}
}
If you have any doubts please comment and please don't dislike.