Main Class
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// write your code here
Payable[] p1 = new Payable[2];
ArrayList<MedicalTest> test = new ArrayList<MedicalTest>();
Patient patient=new Patient(1,"Umer","Outdoor");
Doctor doc = new Doctor("Ahmed", "Cardiologist", 2000.0);
ArrayList<Medicine> medInfo = new ArrayList<Medicine>();
medInfo.add(new Medicine(87, "Nims", 20, 63.2));
ArrayList<MedicalTest> test2 = new ArrayList<MedicalTest>();
Doctor doc2 = new Doctor("Ali", "Surgeon", 2000.0);
ArrayList<Medicine> medInfo2 = new ArrayList<Medicine>();
test.add(new MedicalTest(1, "ABC", 320));
test2.add(new MedicalTest(2, "Def", 4500.2));
test2.add(new MedicalTest(3, "ghi", 410.2));
medInfo2.add(new Medicine(1, "AbC", 20, 63.2));
medInfo2.add(new Medicine(2, "def", 10, 55.2));
p1[0] = new Billing(test, doc, medInfo,patient);
p1[1] = new Billing(test2, doc2, medInfo2,patient);
for (Payable a : p1) {
if (a instanceof Billing) {
((Billing) a).printInfo();
}
}
}
}
Billing Class
import java.util.ArrayList;
public class Billing implements Payable{
static int totalBills;
private ArrayList<MedicalTest> test;
private Doctor doc;
private ArrayList<Medicine> medInfo;
private int Billid;
private Patient pInfo;
public Billing(ArrayList<MedicalTest> test, Doctor doc, ArrayList<Medicine> medInfo,
Patient pInfo) {
totalBills++;
this.test = test;
this.doc = doc;
this.medInfo = medInfo;
Billid = getTotalBills();
this.pInfo = pInfo;
}
public void printInfo(){
System.out.println(toString());
}
@Override
public double getPaymentamount() {
double MedicineTotal=0;
double TestsToal=0;
for(Medicine
a:medInfo){MedicineTotal=MedicineTotal+a.getPrice()*a.getQuantity();}
for(MedicalTest a:test){TestsToal=TestsToal+a.getTestFee();}
return MedicineTotal+TestsToal+doc.getDocFee();
}
@Override
public String toString() {
double TestTotal=0;
for(MedicalTest a:test){TestTotal=TestTotal+a.getTestFee();}
double MedicineTotal=0;
for(Medicine
a:medInfo){MedicineTotal=MedicineTotal+a.getPrice()*a.getQuantity();}
return "MedCare Services\n" + "\nBillId:" + Billid + "\nDoc Name:" +
doc.getDocName()+"\nPatient Name:"+pInfo.getName() + "\nName" +" Quantity" +"
Price" +" Total" + "\n" +medInfo+"\nMed Total: "+MedicineTotal+"\nTests
Total:"+TestTotal+"\nDoc Fee: "+doc.getDocFee()+"\nGrand
Total:"+getPaymentamount();
}
public static int getTotalBills() {
return totalBills;
}
}
MedicalTest Class
public class MedicalTest {
private int id;
private String name;
private double testFee;
public MedicalTest(int id, String name, double testFee) {
this.id = id;
this.name = name;
this.testFee = testFee;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getTestFee() {
return testFee;
}
public void setTestFee(double testFee) {
this.testFee = testFee;
}
@Override
public String toString() {
return "MedicalTest{" + "id=" + id + ", name=" + name + ", testFee=" + testFee +
'}';
}
}
Medicine Class
public class Medicine {
private int medicineId;
private String medName;
private int quantity;
private double price;
public Medicine(int medicineId, String medName, int quantity, double price) {
this.medicineId = medicineId;
this.medName = medName;
this.quantity = quantity;
this.price = price;
}
public int getMedicineId() {
return medicineId;
}
public void setMedicineId(int medicineId) {
this.medicineId = medicineId;
}
public String getMedName() {
return medName;
}
public void setMedName(String medName) {
this.medName = medName;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return medName + " " + quantity + " " + price +"
"+price*quantity+"\n";
}
}
Patient Class
public class Patient {
private int id;
private String name;
private String type;
public Patient(int id, String name, String type) {
this.id = id;
this.name = name;
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Patient{" + "id=" + id + ", name=" + name + ", type=" + type + '}';
}
}
Doctor Class
package com.company;
public class Doctor {
private String docName;
private String docSpec;
private double docFee;
public Doctor(String docName, String docSpec, double docFee) {
this.docName = docName;
this.docSpec = docSpec;
this.docFee = docFee;
}
public String getDocName() {
return docName;
}
public void setDocName(String docName) {
this.docName = docName;
}
public String getDocSpec() {
return docSpec;
}
public void setDocSpec(String docSpec) {
this.docSpec = docSpec;
}
public double getDocFee() {
return docFee;
}
public void setDocFee(double docFee) {
this.docFee = docFee;
}
@Override
public String toString() {
return "Doctor{" + "docName=" + docName + ", docSpec=" + docSpec + ",
docFee=" + docFee + '}';
}
}
Payable Interface
public interface Payable {
public double getPaymentamount();