Define a class Person that holds person\'s name and appropriate constructors, ge
ID: 3709672 • Letter: D
Question
Define a class Person that holds person's name and appropriate constructors, get/set methods, display and hasSameName methods. hasSameName method will return true if two objects of data type Person have the same name.
Then define a class named Doctor whose objects are records for a clinic's doctors. Drive this class from the class Person. A Doctor record has doctor's name, a specialty, and office visit fee. Give your class a reasonable complement of constructors and get/set methods, and an equals method as well. The equals method returns true if two doctor records are the same. Name this class BillingRecordDemo.
Drive Patient form the class Person. A patient record has the Patient's name, and an identification number.
A Billing object will contain a Patient object and a Doctor object. Give your class a reasonable complement of constructors, get/set methods, display and an equals method.
Sample run:
Note: In Doctor class use the following display method:
Use the following template for your Main application Java class.
Explanation / Answer
Below is your code. I have matched the output you have provided. Please comment if you have any issue
Person.java
public class Person {
// instance variable
private String name;
// Parameterized Constructor
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean hasSameName(Person p) {
return this.getName().equalsIgnoreCase(p.getName());
}
@Override
public String toString() {
return "Name=" + name;
}
public void display() {
System.out.printf("%-20s %s ", "Name", name);
}
@Override
public boolean equals(Object obj) {
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
Doctor.java
public class Doctor extends Person {
private String specialty;
private double visitFee;
private double income;
public Doctor(String name, String specialty, double visitFee) {
super(name);
this.specialty = specialty;
this.visitFee = visitFee;
}
public String getSpecialty() {
return specialty;
}
public void setSpecialty(String specialty) {
this.specialty = specialty;
}
public double getvisitFee() {
return visitFee;
}
public void setvisitFee(double visitFee) {
this.visitFee = visitFee;
}
public boolean equals(Doctor obj) {
Doctor other = (Doctor) obj;
if(!this.getName().equals(obj.getName())) {
return false;
}
if (this.income != other.income)
return false;
if (specialty == null) {
if (other.specialty != null)
return false;
} else if (!specialty.equals(other.specialty))
return false;
if (visitFee != other.visitFee)
return false;
return true;
}
public void visit(Patient p) {
income += visitFee;
}
@Override
public String toString() {
return "Doctor [" + super.toString() + " , specialty=" + specialty + ", visitFee=" + visitFee + "]";
}
public void display() {
super.display();
System.out.printf("%-20s %s ", "Specialty", specialty);
System.out.printf("%-20s $%.2f ", "Office Visit Fee", visitFee);
System.out.printf("%-20s $%.2f ", "Total Income", income);
System.out.println("------------------------------------");
}
}
Patient.java
public class Patient extends Person {
private int patientId;
public Patient(String name, int id) {
super(name);
this.patientId = id;
}
public int getPatientId() {
return patientId;
}
public void setPatientId(int patientId) {
this.patientId = patientId;
}
@Override
public String toString() {
return "Patient [patientId=" + patientId + "]" + super.toString();
}
@Override
public boolean equals(Object obj) {
Patient other = (Patient) obj;
if(!this.getName().equals(other.getName())) {
return false;
}
if (patientId != other.patientId)
return false;
return true;
}
}
Billing.java
public class Billing {
Patient patient;
Doctor doctor;
public Billing() {
patient = new Patient("", 0);
doctor = new Doctor("", "", 0);
}
public Billing(Patient patient, Doctor doctor) {
super();
this.patient = patient;
this.doctor = doctor;
}
public Patient getPatient() {
return patient;
}
public void setPatient(Patient patient) {
this.patient = patient;
}
public Doctor getDoctor() {
return doctor;
}
public void setDoctor(Doctor doctor) {
this.doctor = doctor;
}
@Override
public boolean equals(Object obj) {
Billing other = (Billing) obj;
if (doctor == null) {
if (other.doctor != null)
return false;
} else if (!doctor.equals(other.doctor))
return false;
if (patient == null) {
if (other.patient != null)
return false;
} else if (!patient.equals(other.patient))
return false;
return true;
}
}
Main.java
public class Main {
public static void main(String[] args) {
// complete this part
Scanner input = new Scanner(System.in);
System.out.println("Enter number of doctors in the facility: ");
int numberOfDoctors = input.nextInt();
// Create Doctor Array
Doctor[] docArray = new Doctor[numberOfDoctors];
System.out.println("Enter number of patients in the facility: ");
int numberOfPatient = input.nextInt();
Patient[] patArray = new Patient[numberOfPatient];
System.out.println("-----------------------------");
System.out.println("Create Doctor Array: ");
System.out.println("-----------------------------");
for (int i = 0; i < numberOfDoctors; i++) {
System.out.println("Doctor " + (i + 1));
System.out.println("Enter doctor's name: ");
input.nextLine();
String name = input.nextLine();
System.out.println("Enter Specialty: ");
String speciality = input.nextLine();
System.out.println("Enter office visit fee: ");
double fee = input.nextDouble();
docArray[i] = new Doctor(name, speciality, fee);
}
System.out.println("-----------------------------");
System.out.println("Create Patient Array: ");
System.out.println("-----------------------------");
for (int i = 0; i < numberOfPatient; i++) {
System.out.println("Patient " + i);
System.out.println("Enter Patient's name: : ");
input.nextLine();
String name = input.nextLine();
System.out.println("Enter Patient ID: ");
int id = input.nextInt();
patArray[i] = new Patient(name, id);
}
// create a arraylist of Billing objects
ArrayList<Billing> billing = new ArrayList<>();
String choice = "y";
do {
System.out.println("Enter Patient index: ");
int pIndex = input.nextInt();
System.out.println("Enter Doctor index: ");
int dIndex = input.nextInt();
billing.add(new Billing(patArray[pIndex], docArray[dIndex]));
// Visit
docArray[dIndex].visit(patArray[pIndex]);
input.nextLine();
System.out.println("Do you want to set another appointment? (y/n");
choice = input.nextLine();
} while (choice.equalsIgnoreCase("y"));
// Print Doctor Details
ArrayList<Doctor> doctorsForBilling = new ArrayList<>();
for (Billing billing2 : billing) {
if(!doctorsForBilling.contains(billing2.getDoctor())) {
doctorsForBilling.add(billing2.getDoctor());
}
}
for(Doctor doc : doctorsForBilling) {
doc.display();
}
// check object classes
System.out.println("Do you want check Doctor Object class: (y/n");
char respond = input.nextLine().charAt(0);
if (respond == 'y' || respond == 'Y')
checkObjectClassDoctor(input, docArray[0]);
input.nextLine();
System.out.println("Do you want check Patient object class: (y/n");
respond = input.nextLine().charAt(0);
if (respond == 'y' || respond == 'Y')
checkObjectClassPatient(input, patArray[0]);
}
public static void checkObjectClassDoctor(Scanner input, Doctor d) {
System.out.println("Enter Doctor's name: ");
String name = input.nextLine();
System.out.println("Enter Doctor's specialty: ");
String specialty = input.nextLine();
System.out.println("Enter office visit fee: ");
double fee = input.nextDouble();
Doctor doc = new Doctor(name, specialty, fee);
if (d.hasSameName(doc))
System.out.println("Same name");
else
System.out.println("Not have same name");
if (d.equals(doc))
System.out.println("Same doctor");
else
System.out.println("Not the same doctor");
}
public static void checkObjectClassPatient(Scanner input, Patient p) {
System.out.println("Enter Patient's name: ");
String name = input.nextLine();
System.out.println("Enter Patients's ID:");
int id = input.nextInt();
Patient pat = new Patient(name, id);
if (p.hasSameName(pat))
System.out.println("Same name");
else
System.out.println("Not have same name");
if (p.equals(pat))
System.out.println("Same patient");
else
System.out.println("Not the same patient");
}
}
Output
Enter number of doctors in the facility:
2
Enter number of patients in the facility:
3
-----------------------------
Create Doctor Array:
-----------------------------
Doctor 1
Enter doctor's name:
Garry Allen
Enter Specialty:
Family Medicine
Enter office visit fee:
180.65
Doctor 2
Enter doctor's name:
Sarah Adler
Enter Specialty:
Cardiology
Enter office visit fee:
540.98
-----------------------------
Create Patient Array:
-----------------------------
Patient 0
Enter Patient's name: :
Bruce Ammar
Enter Patient ID:
1234
Patient 1
Enter Patient's name: :
Mike Anderson
Enter Patient ID:
2345
Patient 2
Enter Patient's name: :
Jenna Baily
Enter Patient ID:
5678
Enter Patient index:
0
Enter Doctor index:
0
Do you want to set another appointment? (y/n
y
Enter Patient index:
0
Enter Doctor index:
1
Do you want to set another appointment? (y/n
y
Enter Patient index:
1
Enter Doctor index:
0
Do you want to set another appointment? (y/n
y
Enter Patient index:
2
Enter Doctor index:
0
Do you want to set another appointment? (y/n
y
Enter Patient index:
2
Enter Doctor index:
1
Do you want to set another appointment? (y/n
n
Name Garry Allen
Specialty Family Medicine
Office Visit Fee $180.65
Total Income $541.95
------------------------------------
Name Sarah Adler
Specialty Cardiology
Office Visit Fee $540.98
Total Income $1081.96
------------------------------------
Do you want check Doctor Object class: (y/n
y
Enter Doctor's name:
Garry Allen
Enter Doctor's specialty:
Family Medicine
Enter office visit fee:
180.65
Same name
Not the same doctor
Do you want check Patient object class: (y/n
y
Enter Patient's name:
Bruce Ammar
Enter Patients's ID:
4567
Same name
Not the same patient
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.