Q:

JAVA OOP pharmacy and medicine class question

0

Write a complete Java program given the following UML class diagrams:

Pharmacy
- name: String
- medicines: ArrayList<Medicine>
+ Pharmacy (name:String, medicines: Medicine)
+ getName(): String
+ setName(n : String)
+ getMedicines (): Medicine
+ addMed(newMed : Medicine)
+ computePrices(): double
+ toString(): String

-------------------

Medicine
- name: String
- code: int
- price: double 
+ Medicine (n:String, c:int, p:double)
+ getName(): String
+ getCode(): int
+ getPrice(): double
+ setName(n : String)
+ setCode(c : int)
+ setPrice(p : double)
+ toString(): String

Class Pharmacy and class Medicine have “Has-a” relationship. An object of type Pharmacy may have more than one object of type Medicine.

* getMedicines () returns “medicines” of type ArrayList

* addMed() takes and object of type “Medicine” and add it to the “medicines” list.

* computePrices(): returns the total price of all medicines In the main method in your driver class:

* Define 2 objects of type “Medicine” and add them to an ArrayList, then use this list when creating an object of type “Pharmacy”.

* Define third object of type “Medicine” and use “addMedicine” function to add it to the pharmacy.

* Print the total price of all medicines The output may be as the following:

Medicine name: Contra, code: 1234, price: 22.75

Medicine name: Exylin, code: 5678, price: 6.0

Medicine name: Propolsaft, code: 9101, price: 49.5

Sum of all medicines = 78.25

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.ArrayList;

public class Driver {
    public static void main(String[] args) {
        Medicine m1 = new Medicine("contra", 1234, 22.75);
        Medicine m2 = new Medicine("Exylin", 5678, 6.0);

        ArrayList<Medicine> Med = new ArrayList<Medicine>();
        Med.add(m1);
        Med.add(m2);
        Pharmacy ph = new Pharmacy("my new pharmacy", Med);
        Medicine m3 = new Medicine("Propolsaft", 9101, 49.5);
        ph.addMed(m3);
        System.out.println(ph.toString());
        System.out.println("Sum of all medicines= " + ph.computePrices());
    }

}

class Pharmacy {
    private String name;
    private ArrayList<Medicine> medicines;

    public Pharmacy(String name, ArrayList<Medicine> medicines) {
        this.name = name;
        this.medicines = medicines;
    }

    public String getName() {
        return name;
    }

    public void setName(String n) {
        n = name;
    }

    public ArrayList<Medicine> getMedicines() {
        return medicines;
    }

    public void addMed(Medicine newMed) {
        medicines.add(newMed);
    }

    public double computePrices() {
        double totalPrices = 0;
        for (Medicine med : medicines) {
            totalPrices += med.getPrice();
        }
        return totalPrices;
    }

    @Override
    public String toString() {
        return medicines.toString();
    }
}

class Medicine {

    private String name;
    private int code;
    private double price;

    public Medicine(String n, int c, double p) {
        name = n;
        code = c;
        price = p;
    }

    public String getName() {
        return name;
    }

    public void setName(String n) {
        name = n;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int c) {
        code = c;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double p) {
        price = p;
    }

    public String toString() {
        return " Medicicne name: " + getName() + ", code " + getCode() + ", price " + getPrice();
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now