Question
pls help me with it. you just need to answer the question in Appointment.Java, There is only 1 question u need to answer! Appointment.Java package
pls help me with it.
you just need to answer the question in Appointment.Java, There is only 1 question u need to answer!
Appointment.Java
package option1.stage3;
import option1.stage1.Doctor;
import option1.stage1.Patient;
import option1.stage2.TimeSlot;
public class Appointment {
private Doctor doctor;
private Patient patient;
private TimeSlot timeSlot;
public Doctor getDoctor() {
return doctor;
}
public void setDoctor(Doctor doctor) {
this.doctor = doctor;
}
public Patient getPatient() {
return patient;
}
public void setPatient(Patient patient) {
this.patient = patient;
}
public TimeSlot getTimeSlot() {
return timeSlot;
}
public void setTimeSlot(TimeSlot timeSlot) {
this.timeSlot = timeSlot;
}
public Appointment(Doctor doctor, Patient patient, TimeSlot timeSlot) {
setDoctor(doctor);
setPatient(patient);
setTimeSlot(timeSlot);
}
public Appointment(Patient patient, TimeSlot timeSlot) {
setDoctor(null);
setPatient(patient);
setTimeSlot(timeSlot);
}
public String toString() {
if(doctor != null)
return patient+" with "+doctor+" for "+timeSlot;
else
return patient+" for "+timeSlot;
}
/**
*
* @param other
* @return true if calling object conflicts with parameter Appointment.
* we say two appointments conflict if they are with the same doctor
* and the timeSlots overlap.
* HINT: Use method equals from Doctor class and method overlapsWith from TimeSlot class
*/
public boolean conflictsWith(Appointment other) {
return false; //TO BE COMPLETED
}
}
This is the test file to test if the codes are working.
AppointmentTest.JAVA
package option1.stage3;
import common.Graded;
import option1.stage1.*;
import option1.stage2.TimeSlot;
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.text.SimpleDateFormat;
import org.junit.AfterClass;
import org.junit.Test;
public class AppointmentTest {
private static int score = 0;
private static String result = "";
@Test @Graded(marks=5, description="Appointment:conflictsWith(Appointment)")
public void testConflictsWith() {
Doctor d1 = new Doctor("Rachel", "Pediatrician");
Doctor d2 = new Doctor("James", "Oncologist");
Doctor d3 = new Doctor("Rachel", "General Practitioner");
Doctor d5 = new Doctor("rachel", "pediatrician");
Time a = new Time(12, 0);
Time b = new Time(12, 15);
Time c = new Time(12, 30);
Time d = new Time(12, 45);
Time e = new Time(13, 0);
TimeSlot slot1 = new TimeSlot(a, b);
TimeSlot slot3 = new TimeSlot(c, d);
TimeSlot slot4 = new TimeSlot(b, d);
TimeSlot slot7 = new TimeSlot(a, e);
Patient randomPatient = new Patient("Dummy");
Appointment a1 = new Appointment(d1, randomPatient , slot1);
Appointment a2 = new Appointment(d1, randomPatient , slot3);
assertFalse(a1.conflictsWith(a2));
Appointment a3 = new Appointment(d2, randomPatient , slot1);
assertFalse(a1.conflictsWith(a3));
Appointment a4 = new Appointment(d1, randomPatient , slot7);
assertTrue(a2.conflictsWith(a4));
Appointment a5 = new Appointment(d5, randomPatient , slot4);
assertFalse(a1.conflictsWith(a5));
Appointment a6 = new Appointment(d3, randomPatient , slot1);
assertFalse(a1.conflictsWith(a6));
Appointment a7 = new Appointment(d5, randomPatient , slot7);
assertTrue(a1.conflictsWith(a7));
score+=5;
result+="Appointment:conflictsWith(Appointment) passed ";
}
//log reporting
@AfterClass
public static void wrapUp() throws IOException {
System.out.println(result.substring(0,result.length()-1));
System.out.println("--------------------------------------------");
System.out.println("Total: "+score+" out of 5");
System.out.println("-------------------------------------------- ");
String timeStamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
File file = new File("log/Appointment"+timeStamp+".txt");
FileWriter writer = new FileWriter(file);
writer.write(result.substring(0,result.length()-1)+" ");
writer.write("-------------------------------------------- ");
writer.write(score+" ");
writer.flush();
writer.close();
}
}
The two files below are also you will need to make the Appointment.JAVA run
TimeSlot.JAVA
package option1.stage2;
import option1.stage1.Time;
public class TimeSlot {
private Time startTime;
private Time endTime;
public Time getStartTime() {
return startTime;
}
public void setStartTime(Time startTime) {
this.startTime = startTime;
}
public Time getEndTime() {
return endTime;
}
/**
* TimeSlots can be of 15 minutes, 30 minutes, 45 minutes and 1 hour duration.
* You may assume startTime is already set and is in multiples of 15 minutes.
* if endTime is more than 60 minutes away from startTime, set endTime to 60 minutes
* after startTime
* if endTime is less than 15 minutes away from startTime (or for that matter, before startTime),
* set endTime to 15 minutes after startTime
* in all other cases, set endTime to passed time.
* @param endTime
*/
public void setEndTime(Time endTime) {
if (endTime.diff(startTime) > 60) {
endTime = startTime.advance(4);
} else if (endTime.diff(startTime) < 15) {
endTime = startTime.advance(1);
} this.endTime = endTime;
}
public TimeSlot(Time startTime, Time endTime) {
setStartTime(startTime);
setEndTime(endTime);
}
public TimeSlot(Time time) {
this.startTime = time;
this.endTime = time;
}
public String toString() {
return startTime+" - "+endTime;
}
/**
*
* @param other
* @return true if there is any overlap between calling object and parameter object, false otherwise
* For example,
* there is an overlap between slot (12:00-12:30) and slot (12:15-13:00)
* there is an overlap between slot (12:00-12:30) and slot (11:15-12:45)
* there is an overlap between slot (12:00-12:30) and slot (11:15-12:15)
* there is an no overlap between slot (12:00-12:30) and slot (12:30-13:00)
*
*/
public boolean overlapsWith(TimeSlot other) {
if((this.getEndTime().compareTo(other.getStartTime()) == 1)&&
(this.getEndTime().compareTo(other.getEndTime()) == -1)){
return true;
}else if((this.getStartTime().compareTo(other.getStartTime()) == 1) && (this.getStartTime().compareTo(other.getEndTime()) == -1)){ return true; }else if(this.getStartTime().compareTo(other.getStartTime()) == 0){ return true; }else if(this.getEndTime().compareTo(other.getEndTime()) == 0){ return true; } return false;
Time.JAVA
package option1.stage1;
public class Time {
private int hour, minute;
public Time(int hour, int minute) {
setHour(hour);
setMinute(minute);
}
public Time(Time t) {
setHour(t.hour);
setMinute(t.minute);
}
public Time() {
setHour(0);
setMinute(0);
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
hour = hour%24;
this.hour = hour;
}
public int getMinute() {
return minute;
}
/**
*
* @param minute
*/
public void setMinute(int minute) {
if(minute >= 45)
this.minute = 45;
else if(minute >= 30)
this.minute = 30;
else if(minute >= 15)
this.minute = 15;
else
this.minute = 0;
}
public String toString() {
if(hour < 10 && minute < 10)
return "0"+hour+":0"+minute;
if(hour < 10)
return "0"+hour+":"+minute;
if(minute < 10)
return hour+":0"+minute;
return hour+":"+minute;
}
public boolean equals(Object other) {
if(other instanceof Time)
return hour == ((Time)other).hour && minute == ((Time)other).minute;
else
return false;
}
/**
*
* @param other
* @return the difference in minutes between calling object and other object.
* note that the value returned will be positive if calling object comes after
* parameter object and negative if calling object comes before parameter object.
* for example,
* if calling object represents 14:00 and parameter represents 12:30, return 90.
* if calling object represents 13:00 and parameter represents 17:30, return -270.
*/
public int diff(Time other) {
return(this.getHour()*60 + this.getMinute())-(other.getHour()*60 + other.getMinute()); //TO BE COMPLETED
}
/**
* @param other: object against which calling object should be compared
* @return 1 if calling object is after parameter object
* -1 if calling object is before parameter object
* 0 if calling object and parameter object are at the same time
*/
public int compareTo(Time other) {
int difference=this.diff(other);
if(difference>0)
return 1;
else if(difference<0)
return -1;
else
return 0;
//TO BE COMPLETED
}
/**
*
* @param quarterHourCount (assumed to be non-negative).
* @return a Time object that represents the calling object shifted forward by
* quarterHourCount increments of 15 minutes.
* for example,
* if calling object represents 14:30 and quarterHourCount = 5,
* return Time object representing 15:45.
*/
public Time advance(int quarterHourCount) {
Time a =new Time(hour,minute);
for(int x =1; x<= quarterHourCount;x++) {
a.minute +=15;
if(a.minute== 60)
{
a.minute = 0;
a.hour++;
if(a.hour == 24)
a.hour=0;
}
}
return a;
//TO BE COMPLETED
}
}
Doctor.JAVA
package option1.stage1;
import java.util.ArrayList;
import java.util.Arrays;
public class Doctor {
private String name;
private String speciality;
/*
* note specialities is static means that only one instance is created
* irrespective of number of Doctor instances and that one instance of
* specialities is shared by all instances. it can be accessed by Doctor.specialities
*/
public static ArrayList
"Pediatrician", "Podiatrician", "Dermatologist",
"Radiologist", "Pathologist", "Physician",
"Obstetrician", "Gynaecologist", "Oncologist"));
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSpeciality() {
return speciality;
}
/**
* set speciality of the calling object to the passed value IF the value
* exists (case insensitive) in the ArrayList specialities.
* If the passed value doesn't exist in the ArrayList specialities, set
* the speciality of the calling object to "General Practitioner".
* For example, if spec = "oncologist" or if spec = "O
* ncologist" or spec = "ONCOLOGIST",
* instance variable speciality should become "Oncologist"
* @param spec
*/
public void setSpeciality(String spec) {
boolean nco=false;
for (String str:specialities)
{
if(str.equalsIgnoreCase(spec))
{
nco=true;
this.speciality=str;
break;
}
}
if(nco==false)
{
this.speciality ="General Practitioner";
}
}
//TO BE COMPLETLED}
public Doctor(String name, String spec) {
setName(name);
setSpeciality(spec);
}
public String toString() {
return "Dr. "+name+" ("+speciality+")";
}
/**
*
* @param other
* @return true if the names and specialities of the calling object and the parameter object
* are the same (case insensitive)
*/
public boolean equals(Object other) {
if(other instanceof Doctor)
return name.equalsIgnoreCase(((Doctor)other).name) && speciality.equalsIgnoreCase(((Doctor)other).speciality);
else
return false;
}
}
Patient.JAVA
package option1.stage1;
public class Patient {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Patient(String name) {
setName(name);
}
public String toString() {
return name;
}
}
Let me know if any codes above are no working
thank you so much
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started