In: Computer Science
Programming Project #1 – Day Planner
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.
Working code implemented in Java and appropriate comments provided for better understanding:
Here I am attaching code for these files:
Source code for Appointment.java:
/*
* This Appointment.java file uses different methods to make
appointments. It uses get and set methods for
* each aspect of the appointment and then pulls from the
UserInput.java file to get user input and make
* sure all the inputs are valid for the appointment.
*/
public class Appointment {
private String month, message;
private int day, hour, minute;
public Appointment(){
day = 1;
hour = 1;
minute = 1;
month = "Jan";
message = "Default";
}
public Appointment (int day, int hour, int minute, String month,
String message){
setDay(day);
setHour(hour);
setMinute(minute);
setMonth(3, 3, month);
setMessage(0, 40, message);
}
public String getMonth(){
return month;
}
public void setMonth(int min, int max, String s){
if (s.length() >= min && s.length() <= max)
month = s;
}
public String getMessage(){
return message;
}
public void setMessage(int min, int max, String s) {
if (s.length() >= min && s.length() <= max)
message = s;
}
public int getDay(){
return day;
}
public void setDay(int x){
if (x >= 1 && x <= 31)
day = x;
else
System.out.println("Invalid day
value.");
}
public int getHour(){
return hour;
}
public void setHour(int x){
if (x >= 1 && x <= 24)
hour = x;
else
System.out.println("Invalid hour value.");
}
public int getMinute(){
return minute;
}
public void setMinute(int x){
if (x >= 0 && x <= 60)
minute = x;
else
System.out.println("Invalid minute value.");
}
public String toString(){
String date;
if (hour < 10 && minute < 10)
date = "Your appointment for " + message + " is on " + month + " "
+ day + " at " + " 0" + hour + ":" + "0" + minute;
else if (minute < 10)
date = "Your appointment for " + message + " is on " + month + " "
+ day + " at " + hour + ":" + "0" + minute;
else if (hour > 10 && minute < 10)
date = "Your appointment for " + message + " is on " + month + " "
+ day + " at " + hour + ":" + "0" + minute;
else if (hour < 10 && minute > 10)
date = "Your appointment for " + message + " is on " + month + " "
+ day + " at " + "0"+ hour + ":" + minute;
else
date = "Your appointment for " + message + " is on " + month + " "
+ day + " at " + hour + ":" + minute;
return date;
}
public void inputAppointment(){
System.out.println("Please enter a month for your
appointment.");
setMonth(3, 3, UserInput.getString(3, 3));
System.out.println("Please enter a day for your
appointment.");
setDay(UserInput.getInt(1, 31));
System.out.println("Please enter a hour for your
appointment.");
setHour(UserInput.getInt(0, 24));
System.out.println("Please enter a minute for your
appointment.");
setMinute(UserInput.getInt(0, 59));
System.out.println("Please enter a message for your
appointment.");
setMessage(0, 40, UserInput.getString(0, 40));
}
public static void main(String[] args) {
Appointment a1 = new Appointment();
a1.inputAppointment();
System.out.println(a1.toString());
Appointment a2 = new Appointment(1, 1, 1, "Jan", "Hello
World!");
System.out.println(a2);
}
}
Source code for Planner.java:
/*
* This Planner.java file takes the Appointment.java file and puts
all new months into an array that is sorted
* by the order in which the appointments are. In this .java file
you can list all the appointments you have,
* insert new appointments, delete appointments, and add new
appointments. This file also compares the different
* appointments to determine where to insert new appointemnts.
*/
public class Planner {
private Appointment[] planner = new Appointment[20];
private String[] months = new String[12];
int numAppointments;
public Planner () {
Appointment a = new Appointment(4, 17, 30, "Mar", "Quiz1");
Appointment a2 = new Appointment(1, 17, 30, "Apr",
"Midterm");
Appointment a3 = new Appointment(6, 17, 30, "May", "Quiz 2");
Appointment a4 = new Appointment(3, 17, 30, "Jun", "Final");
planner[0] = a;
planner[1] = a2;
planner[2] = a3;
planner[3] = a4;
months[0] = "Jan";
months[1] = "Feb";
months[2] = "Mar";
months[3] = "Apr";
months[4] = "May";
months[5] = "Jun";
months[6] = "Jul";
months[7] = "Aug";
months[8] = "Sep";
months[9] = "Oct";
months[10] = "Nov";
months[11] = "Dec";
numAppointments = 4;
}
public void run() {
boolean exitMenu = false;
do {
System.out.println("A)dd Appointment, D)elete Appointment, L)ist
Appointment, E)xit");
char value = UserInput.getChar();
value = Character.toUpperCase(value);
switch (value) {
case 'A':
addAppointment();
break;
case 'D':
deleteAppointment();
break;
case 'L':
listAppointment();
break;
case 'E':
exitMenu = true;
break;
default:
}
} while (!exitMenu);
}
public boolean compareAppointment(Appointment A1, Appointment A2)
{
String a1Month = A1.getMonth();
String a2Month = A2.getMonth();
int a1Value = 0;
int a2Value = 0;
if (A1.getMonth() != A2.getMonth()){
for (int i = 0; i < months.length; i++){
if (a1Month == months[i])
a1Value = i;
}
for (int i = 0; i < months.length; i++){
if (a2Month == months[i])
a2Value = i;
}
if (a1Value < a2Value)
return true;
else
return false;
}
else if (A1.getDay() != A2.getDay()){
if (A1.getDay() < A2.getDay())
return true;
else
return false;
}
else if (A1.getHour() != A2.getHour()) {
if (A1.getHour() < A2.getHour())
return true;
else
return false;
}
else if (A1.getMinute() != A2.getMinute()){
if (A1.getMinute() < A2.getMinute())
return true;
else
return false;
}
else
System.out.println("Appointments are identical.");
return true;
}
public void insertAppointment(Appointment A1){
int index = 0;
for (int i = 0; i < numAppointments; i++){
if (compareAppointment(A1, planner[i]))
index = i - 1;
}
numAppointments++;
Appointment temp1 = planner[index];
planner[index] = A1;
for (int i = index; i < numAppointments; i++){
Appointment temp = planner[i+1];
planner[i+1] = temp1;
temp1 = temp;
}
}
public void listAppointment(){
int number = 1;
for(int i = 0; i < numAppointments; i++) {
System.out.println(number + ". " + planner[i].toString());
number++;
}
}
public void deleteAppointment(){
listAppointment();
System.out.println("Please select an appointment to
delete.");
int value = UserInput.getInt();
numAppointments--;
for (int i = value - 1; i < numAppointments; i++){
if (i + 1 <= numAppointments)
planner[i] = planner[i + 1];
}
}
public void addAppointment(){
Appointment A1 = new Appointment();
A1.inputAppointment();
insertAppointment(A1);
}
public static void main(String args[]) {
Planner p1 = new Planner();
p1.run();
}
}
Source code for UserInput.java:
/*
* This UserInput.java file is going to be used for all the projects
we have in this class.
* this is used for all user input so we do not have to make new
scanners everytime we are gettin
* input from the user.
*/
import java.util.Scanner;
public class UserInput {
static Scanner scanner = new Scanner(System.in);
public static int getInt (){
System.out.print("Integer input: ");
return Integer.parseInt(scanner.nextLine());
}
public static int getInt(int min, int max){
boolean userValue = true;
int x;
do {
System.out.print("Enter an integer between " + min + " and " + max
+ ". ");
x = getInt();
if (x >= min && x <= max){
userValue = true;
}
else {
System.out.println ("Error. Input must be between " + min + " and "
+ " " + max + ". ");
userValue = false;
}
} while (!userValue);
return x;
}
public static char getChar(){
System.out.print("Char input: ");
return scanner.nextLine().charAt(0);
}
public static char getChar(char min, char max) {
min = Character.toUpperCase(min);
max = Character.toUpperCase(max);
boolean userValue = true;
char x;
do{
System.out.println("Please enter a character between " + min + "
and " + max + ". ");
x = getChar();
char temp = x;
temp = Character.toUpperCase(temp);
if (temp >= min && temp <= max){
userValue = true;
}
else {
System.out.println ("Error. Input must be between " + min + " and "
+ max + ". ");
userValue = false;
}
} while (!userValue);
return x;
}
public static double getDouble(){
System.out.print ("Double input: ");
return Double.parseDouble(scanner.nextLine());
}
public static double getDouble(double min, double max) {
boolean userValue = true;
double x;
do {
System.out.print("Enter an double value between " + min + " and " +
max + ". ");
x = getDouble();
if (x >= min && x <= max){
userValue = true;
}
else {
System.out.println ("Error. Input must be between " + min + " and "
+ " " + max + ". ");
userValue = false;
}
} while (!userValue);
return x;
}
public static String getString(){
System.out.println("String: ");
return scanner.nextLine();
}
public static String getString(int min, int max){
boolean userValue = true;
String x;
do {
System.out.print("Enter an String between " + min + " and " + max +
" characters: ");
x = getString();
if (x.length() >= min && x.length() <= max){
userValue = true;
}
else {
System.out.println ("Error. Input must be between " + min + " and "
+ " " + max + " characters. ");
userValue = false;
}
} while (!userValue);
return x;
}
public static void main(String args[]) {
System.out.println(getInt(0, 30));
System.out.println(getChar('a', 'z'));
System.out.println(getDouble(0.0, 30.0));
System.out.println(getString(0, 40));
}
}
Sample Output Screenshots:
Hope it helps, if you like the answer give it a thumbs up. Thank you.