Please comment code import java.util.ArrayList; import java.util.Scanner; abstra
ID: 3754319 • Letter: P
Question
Please comment code
import java.util.ArrayList;
import java.util.Scanner;
abstract class Appointment{
private String description;
private String lastName;
private int hours=0;
private int minutes=0;
public void setDescription(String description){
this.description = description;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public void setHours(int hours){
this.hours = hours;
}
public void setMinutes(int minutes){
this.minutes = minutes;
}
public String getDescription(){
return this.description;
}
public String getLastName(){
return this.lastName;
}
public int getHours(){
return this.hours;
}
public int getMinutes(){
return this.minutes;
}
abstract public boolean occursOn(int year,int month,int day);
}
class Onetime extends Appointment{
private int year;
private int month;
private int day;
public void setYear(int year) {
this.year=year;
}
public void setMonth(int month) {
this.month=month;
}
public void setDay(int day) {
this.day=day;
}
public int getYear() {
return this.year;
}
public int getMonth() {
return this.month;
}
public int getDay() {
return this.day;
}
@Override
public boolean occursOn(int year, int month, int day) {
if(this.year==year && this.month==month && this.day==day)
return true;
return false;
}
}
class Daily extends Appointment{
@Override
public boolean occursOn(int year, int month, int day) {
// TODO Auto-generated method stub
return true;
}
}
class Monthly extends Appointment{
private int day;
public void setDay(int day) {
this.day=day;
}
public int getDay() {
return this.day;
}
@Override
public boolean occursOn(int year, int month, int day) {
if(this.day==day)
return true;
return false;
}
}
public class TestAppointment {
static Scanner sc= new Scanner(System.in);
public static void main(String[] args) {
ArrayList<Appointment> al = new ArrayList<Appointment>();
System.out.println("How many appointments do you with to make?");
int num = sc.nextInt();
for(int i = 1;i<=num;i++) {
System.out.println("Please make a selection:");
System.out.println("1. One Time Appointment");
System.out.println("2. Daily Appointment");
System.out.println("3. Monthly Appointment");
int selection = sc.nextInt();
al.add(makeAppointment(selection));
}
System.out.println("*******Thank you for making all your appointments*********");
char choice='y';
while(choice=='y') {
System.out.println("What is the date you wish to look up in your Appointment's Calendar");
System.out.print("Enter the month: ");
int month=sc.nextInt();
System.out.print("Enter the day: ");
int day=sc.nextInt();
System.out.print("Enter the year: ");
int year=sc.nextInt();
System.out.println("On "+month+"/"+day+"/"+year+" you have the following appointments:");
for(Appointment a:al) {
boolean isPresent=checkAppointment(a,year,month,day);
if(isPresent) {
System.out.println(a.getDescription()+" with "+a.getLastName()+" at "+a.getHours()+":"+a.getMinutes());
}
}
System.out.println("Do you wish to look up another date?");
choice=sc.next().charAt(0);
}
System.out.println("Thank you for using Appointment Calendar.");
}
public static Appointment makeAppointment(int selection) {
Appointment app=null;
String description="";
String lastName="";
int hours=0;
int minutes = 0;
int day=0;
int year=0;
int month=0;
sc.nextLine();
switch (selection) {
case 1:
Onetime Onetime();
System.out.println("What is the description of your appointment?");
description = sc.nextLine();
System.out.println("What is your person's name?");
lastName = sc.nextLine();
System.out.println("What is the year of your appointment?(2017-2018)");
year = sc.nextInt();
System.out.println("What is the month of your appointment?(1-12)");
month = sc.nextInt();
System.out.println("What is the day of your appointment?(1-31)");
day = sc.nextInt();
System.out.println("What is the hour of your appointment?(1-31)");
hours = sc.nextInt();
System.out.println("What is the minutes of your appointment?(1-31)");
minutes = sc.nextInt();
onetime.setDescription(description);
onetime.setHours(hours);
onetime.setLastName(lastName);
onetime.setMonth(month);
onetime.setMinutes(minutes);
onetime.setYear(year);
onetime.setDay(day);
System.out.println("Appointment added with "+lastName+" on "+month+"/"+day+"/"+year+" at "+hours+":"+minutes);
System.out.println();
return onetime;
case 2:
Daily daily = new Daily();
System.out.println("What is the description of your appointment?");
description = sc.nextLine();
System.out.println("What is your person's name?");
lastName = sc.nextLine();
System.out.println("What is the hour of your appointment?(1-31)");
hours = sc.nextInt();
System.out.println("What is the minutes of your appointment?(1-31)");
minutes = sc.nextInt();
daily.setDescription(description);
daily.setHours(hours);
daily.setLastName(lastName);
daily.setMinutes(minutes);
System.out.println("Appointment added with "+lastName+" Daily at "+hours+":"+minutes);
System.out.println();
return daily;
case 3:
Monthly monthly = new Monthly();
System.out.println("What is the description of your appointment?");
description = sc.nextLine();
System.out.println("What is your person's name?");
lastName = sc.nextLine();
System.out.println("What day of the month is your appointment?(1-12)");
day = sc.nextInt();
System.out.println("What is the hour of your appointment?(1-31)");
hours = sc.nextInt();
System.out.println("What is the minutes of your appointment?(1-31)");
minutes = sc.nextInt();
monthly.setDescription(description);
monthly.setHours(hours);
monthly.setLastName(lastName);
monthly.setMinutes(minutes);
monthly.setDay(day);
System.out.println("Appointment added with "+lastName+" Monthly on day "+day+" at "+hours+":"+minutes);
System.out.println();
return monthly;
default:
System.out.println("Invalid Choice..!!");
return app;
}
}
public static boolean checkAppointment(Appointment a, int year, int month, int day) {
return a.occursOn(year, month, day);
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.Scanner;
abstract class Appointment{ // abstract class declaration
private String description; // variable declaration
private String lastName; // variable declaration
private int hours=0; // variable declaration and assign hours as 0
private int minutes=0; // variable declaration and assign minutes as 0
public void setDescription(String description){ // called method
this.description = description; // since parameter and member same use this to assign parameter
}
public void setLastName(String lastName){ // called method
this.lastName = lastName; // since parameter and member same use this to assign parameter
}
public void setHours(int hours){ // called method
this.hours = hours; // since parameter and member same use this to assign parameter
}
public void setMinutes(int minutes){ // called method
this.minutes = minutes; // since parameter and member same use this to assign parameter
}
public String getDescription(){ // called method
return this.description; // return descripton what we assign from parameter
}
public String getLastName(){ // called method
return this.lastName; // return lastName what we assign from parameter
}
public int getHours(){ // called method
return this.hours; // retuen hours what we assign from parameter
}
public int getMinutes(){ // called method
return this.minutes; // retuen minutes what we assign from parameter
}
abstract public boolean occursOn(int year,int month,int day); // calling method
}
class Onetime extends Appointment{ // derived class
/* members declaration*/
private int year;
private int month;
private int day;
public void setYear(int year) { // called method
this.year=year; // since parameter and member same use this to assign parameter
}
public void setMonth(int month) { // called method
this.month=month; // since parameter and member same use this to assign parameter
}
public void setDay(int day) { // called method
this.day=day; // since parameter and member same use this to assign parameter
}
public int getYear() { // called method
return this.year; // since parameter and member same use this to assign parameter
}
public int getMonth() { // called method
return this.month; // since parameter and member same use this to assign parameter
}
public int getDay() { // called method
return this.day; // since parameter and member same use this to assign parameter
}
@Override
public boolean occursOn(int year, int month, int day) {
// method to check overide or same data
if(this.year==year && this.month==month && this.day==day) // check overide or same data
return true; // if exist return true
return false; // if not return false
}
}
class Daily extends Appointment{ // DERIVED CLASS
@Override
public boolean occursOn(int year, int month, int day) {
// TODO Auto-generated method stub
return true;
}
}
class Monthly extends Appointment{ // DERIVED CLASS
private int day; // member declaration
public void setDay(int day) { // called method
this.day=day;
}
public int getDay() { // called method
return this.day; // since parameter and member same use this to assign parameter
}
@Override
public boolean occursOn(int year, int month, int day) {
// called method
if(this.day==day) // check same day or not
return true; // same day return true
return false; // not same day return false
}
}
public class TestAppointment { // class declaration
static Scanner sc= new Scanner(System.in); // scanner to accept data
public static void main(String[] args) { // main function
ArrayList<Appointment> al = new ArrayList<Appointment>(); // array list declaration
System.out.println("How many appointments do you with to make?");
int num = sc.nextInt(); // Accept number of appointments
for(int i = 1;i<=num;i++) { // Loop to appoint ment menu selections
System.out.println("Please make a selection:");
System.out.println("1. One Time Appointment");
System.out.println("2. Daily Appointment");
System.out.println("3. Monthly Appointment");
int selection = sc.nextInt(); // Accept the selection
al.add(makeAppointment(selection)); // add the appointments to array llist
}
System.out.println("*******Thank you for making all your appointments*********");
char choice='y'; // variable declaration
while(choice=='y') {
System.out.println("What is the date you wish to look up in your Appointment's Calendar");
System.out.print("Enter the month: ");
int month=sc.nextInt(); // Acceept the month
System.out.print("Enter the day: ");
int day=sc.nextInt(); // Acceept the day
System.out.print("Enter the year: ");
int year=sc.nextInt(); // Acceept the year
System.out.println("On "+month+"/"+day+"/"+year+" you have the following appointments:"); // print the appointments
for(Appointment a:al) { // Loop to run for all appointments
boolean isPresent=checkAppointment(a,year,month,day); // calling method
if(isPresent) { // check appointment exist
System.out.println(a.getDescription()+" with "+a.getLastName()+" at "+a.getHours()+":"+a.getMinutes()); // print appointment details
}
}
System.out.println("Do you wish to look up another date?");
choice=sc.next().charAt(0); // accept choice
}
System.out.println("Thank you for using Appointment Calendar.");
}
public static Appointment makeAppointment(int selection) { // called method
/* variable declaration*/
Appointment app=null;
String description="";
String lastName="";
int hours=0;
int minutes = 0;
int day=0;
int year=0;
int month=0;
sc.nextLine();
switch (selection) { // switch statement to selection
case 1:
Onetime Onetime();
System.out.println("What is the description of your appointment?");
description = sc.nextLine(); // Accept description for appointment
System.out.println("What is your person's name?");
lastName = sc.nextLine(); // Accept person's name
System.out.println("What is the year of your appointment?(2017-2018)");
year = sc.nextInt(); // Accept year
System.out.println("What is the month of your appointment?(1-12)");
month = sc.nextInt(); // Accept month
System.out.println("What is the day of your appointment?(1-31)");
day = sc.nextInt(); // Accept day
System.out.println("What is the hour of your appointment?(1-31)");
hours = sc.nextInt(); // Accept hours
System.out.println("What is the minutes of your appointment?(1-31)");
minutes = sc.nextInt(); // Accept minutes
/*calling methods*/
onetime.setDescription(description);
onetime.setHours(hours);
onetime.setLastName(lastName);
onetime.setMonth(month);
onetime.setMinutes(minutes);
onetime.setYear(year);
onetime.setDay(day);
System.out.println("Appointment added with "+lastName+" on "+month+"/"+day+"/"+year+" at "+hours+":"+minutes); // print appointment holder details
System.out.println();
return onetime;
case 2:
Daily daily = new Daily();
System.out.println("What is the description of your appointment?");
description = sc.nextLine(); // Accept description
System.out.println("What is your person's name?");
lastName = sc.nextLine(); // Accept lastName
System.out.println("What is the hour of your appointment?(1-31)");
hours = sc.nextInt(); // Accept hours
System.out.println("What is the minutes of your appointment?(1-31)");
minutes = sc.nextInt(); // Accept minutes
/* calling methods*/
daily.setDescription(description);
daily.setHours(hours);
daily.setLastName(lastName);
daily.setMinutes(minutes);
System.out.println("Appointment added with "+lastName+" Daily at "+hours+":"+minutes); // print appointment holder details
System.out.println();
return daily;
case 3:
Monthly monthly = new Monthly();
System.out.println("What is the description of your appointment?");
description = sc.nextLine(); // Accept description
System.out.println("What is your person's name?");
lastName = sc.nextLine(); // Accept lastName
System.out.println("What day of the month is your appointment?(1-12)");
day = sc.nextInt(); // Accept day
System.out.println("What is the hour of your appointment?(1-31)");
hours = sc.nextInt(); // Accept hours
System.out.println("What is the minutes of your appointment?(1-31)");
minutes = sc.nextInt(); // Accept minutes
/* calling methods*/
monthly.setDescription(description);
monthly.setHours(hours);
monthly.setLastName(lastName);
monthly.setMinutes(minutes);
monthly.setDay(day);
System.out.println("Appointment added with "+lastName+" Monthly on day "+day+" at "+hours+":"+minutes);
// print appointment holder details
System.out.println();
return monthly;
default:
System.out.println("Invalid Choice..!!");
return app;
}
}
public static boolean checkAppointment(Appointment a, int year, int month, int day) { // calling method to check appointments
return a.occursOn(year, month, day); // pass details
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.