import java.io.
BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
//main class
public class PharmacyHMSystem {
public static void main(String[] args) {
// Create a BufferedReader for user input
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// Initialize variables
int totalBill = 0;
try {
// Pharmacy Health Medicare System
System.out.println("\t\t\t\t Pharmacy Health Medicare System");
System.out.println("\t\t\t-----------------------------------------------------");
// Medicine Inventory //to store data
Map<String, Medicine> medicineInvt = new HashMap<>();
medicineInvt.put("Paracetamol", new OverTheCounterMedicine("Paracetamol", 15, 10));
medicineInvt.put("Indapamine", new OverTheCounterMedicine("Indapamine", 25, 19));
medicineInvt.put("Antacids", new OverTheCounterMedicine("Antacids", 52, 23));
medicineInvt.put("Insulin", new PrescriptionMedicine("Insulin", 50, 36));
medicineInvt.put("Vitamin C", new VitaminMedicine("Vitamin C", 50, 40));
// Display available medicines with stock and price
System.out.println("\n\t\t\t\tAvailable Medicines:");
System.out.println("\t\t\t\tCode\tMedicine\tStock\tPrice");
int Mcode = 1;
for (Map.Entry<String, Medicine> entry : medicineInvt.entrySet()) {
Medicine medicine = entry.getValue();
System.out.println("\t\t\t\t" + Mcode + "\t" + medicine.getName() + "\t" +
medicine.getStock() + "\tRM" + medicine.getPrice());
Mcode++;
System.out.println();
// Ask user for medicine selection and quantity
System.out.print("\t\t\t\tEnter medicine code or name: ");
String selectedMedicineInput = reader.readLine();
Medicine selectedMedicine = getMedicine(selectedMedicineInput, medicineInvt);
if (selectedMedicine != null) {
System.out.print("\t\t\t\tEnter quantity: ");
int quantityMed = Integer.parseInt(reader.readLine());
// Check if requested quantity is available in stock
int availableStock = selectedMedicine.getStock();
if (quantityMed <= availableStock) {
// Calculate total bill
int bill = selectedMedicine.getPrice() * quantityMed;
totalBill += bill;
// Update stock
selectedMedicine.setStock(availableStock - quantityMed);
System.out.println("\t\t\t\tMedicine added to the bill.");
System.out.println("\t\t\t\tTotal Bill: RM" + totalBill);
// Ask user if they want to add more medicines
System.out.print("\t\t\t\tDo you want to add more medicines? (yes='Y'/no='N'): ");
String choice = reader.readLine();
while (choice.equalsIgnoreCase("Y")) {
// Display available medicines with stock and price
System.out.println("\n\t\t\t\tAvailable Medicines:");
System.out.println("\t\t\t\tCode\tMedicine\tStock\tPrice");
Mcode = 1;
for (Map.Entry<String, Medicine> entry : medicineInvt.entrySet()) {
Medicine medicine = entry.getValue();
System.out.println("\t\t\t\t" + Mcode + "\t" + medicine.getName() + "\t" +
medicine.getStock() + "\tRM" + medicine.getPrice());
Mcode++;
System.out.println();
System.out.print("\t\t\t\tEnter medicine code or name: ");
selectedMedicineInput = reader.readLine();
selectedMedicine = getMedicine(selectedMedicineInput, medicineInvt);
if (selectedMedicine != null) {
System.out.print("\t\t\t\tEnter quantity: ");
quantityMed = Integer.parseInt(reader.readLine());
// Check if requested quantity is available in stock
availableStock = selectedMedicine.getStock();
if (quantityMed <= availableStock) {
// Calculate total bill
bill = selectedMedicine.getPrice() * quantityMed;
totalBill += bill;
// Update stock
selectedMedicine.setStock(availableStock - quantityMed);
System.out.println("\t\t\t\tMedicine added to the bill.");
System.out.println("\t\t\t\tTotal Bill: RM" + totalBill);
System.out.print("\t\t\t\tDo you want to add more medicines? (yes='Y'/no='N'):
");
choice = reader.readLine();
} else {
System.out.println("\t\t\t\tQuantity stock is not available.");
break;
} else {
System.out.println("\t\t\t\tInvalid medicine code or name.");
break;
// Totaling (calculation)
System.out.print("\t\t\t\tEnter amount of payment: ");
int payment = Integer.parseInt(reader.readLine());
if (payment < totalBill) {
System.out.println("\t\t\t\tInsufficient payment amount. Please provide the correct
amount.");
} else {
int balance = payment - totalBill;
System.out.println("\t\t\t\tBalance: RM" + balance);
} else {
System.out.println("\t\t\t\tQuantity stock is not available.");
} else {
System.out.println("\t\t\t\tInvalid medicine code or name.");
} catch (IOException e) {
e.printStackTrace();
private static Medicine getMedicine(String input, Map<String, Medicine> medicineInvt) {
Medicine selectedMedicine = null;
if (input.matches("\\d+")) {
int selectedMCode = Integer.parseInt(input);
selectedMedicine = getMedicineByMCode(selectedMCode,
medicineInvt.values().toArray(new Medicine[0]));
} else {
selectedMedicine = medicineInvt.get(input);
return selectedMedicine;
private static Medicine getMedicineByMCode(int Mcode, Medicine[] medicines) {
if (Mcode >= 1 && Mcode <= medicines.length) {
return medicines[Mcode - 1];
System.out.println("\t\t\t\tThank you for using Pharmacy Health Medicare System!");
return null;
//Superclass
class Medicine {
private String name;
private int stock;
private int price;
public Medicine(String name, int stock, int price) {
this.name = name;
this.stock = stock;
this.price = price;
public String getName() {
return name;
public int getStock() {
return stock;
public void setStock(int stock) {
this.stock = stock;
}
public int getPrice() {
return price;
//subclasses
class PrescriptionMedicine extends Medicine {
private String prescriptionCode;
public PrescriptionMedicine(String name, int stock, int price, String prescriptionCode) {
super(name, stock, price);
this.prescriptionCode = prescriptionCode;
public String getPrescriptionCode() {
return prescriptionCode;
class OverTheCounterMedicine extends Medicine {
public OverTheCounterMedicine(String name, int stock, int price) {
super(name, stock, price);
class AntibioticMedicine extends PrescriptionMedicine {
public AntibioticMedicine(String name, int stock, int price, String prescriptionCode) {
super(name, stock, price, prescriptionCode);
}
class VitaminMedicine extends OverTheCounterMedicine {
public VitaminMedicine(String name, int stock, int price) {
super(name, stock, price);