0

I'm new at coding and can't seem to find a solution for my problem here. My teacher told me I should be able to make an easier method than my current one. I'm making a medicine list where the current user should be able to add a medicine to the current patient. I have a Medicine class where I have my Indexable map stored. Now, I also have a class called "Patient", where I am creating my medicine menu which will contain a "add, remove and edit" method. My teacher says I should be able to find an easier way. Does anyone know how to create these methods easily?

( Indexable hashmap :)

  import java.util.*;

public class Medicine {
    public String medication_name;
    public String type;
    public String dosage;

    public Medicine(String name, String type, String dosage) {
        this.medication_name = name;
        this.type = type;
        this.dosage = dosage;
    }

    public static IndexableMap<String, Medicine> getMedicineList(){

        IndexableMap<String, Medicine> medicineList = new IndexableMap<>();
        medicineList.put("1.", new Medicine( "Diclofenac", "(Painkiller)", "2 x a day"));
        medicineList.put("2.", new Medicine("Amoxicilline", "(Antibiotic)", "3 x a week "));
        medicineList.put("3.", new Medicine("Omeprazol", "(Anti-inflammatory)", "4 x a day"));
        medicineList.put("4.", new Medicine("Doxycycline", "(Antibiotic)", "2 x a week"));
        medicineList.put("5.", new Medicine( "Ibuprofen", "(Anti-inflammatory)", "7 x a day"));
        medicineList.put("6.", new Medicine(" Metoprolol", "(B1-blocker)", "1 x a day"));
        medicineList.put("7.", new Medicine("Salbutamol", "(B2-receptor)", "1 x a week"));
        medicineList.put("8.", new Medicine( "Fusidinezuur", "(Antibiotic)", "2 x a week"));
        medicineList.put("9.", new Medicine( "Acenocoumarol", "(Anticoagulant)", "4 x a day"));

        TreeMap<String, Medicine> sorted_medicineList = new TreeMap<>();
        sorted_medicineList.putAll(medicineList);

        System.out.println("===== Available Medicine =====");
        for (String temp : sorted_medicineList.keySet()) {
            String key = temp.toString();
            String value = sorted_medicineList.get(temp).toString();
            System.out.println(key + " " + value);
            System.out.println(temp);
            
        }
        return medicineList;
    }
}    

so far i have this for my add method:

 Scanner scan = new Scanner(System.in);
System.out.print("Enter medicine id:");
int medicine_to_add = scan.nextInt();
scan.nextLine();

System.out.println("\n".repeat(80));
String medicine_name = String.valueOf(Medicine.getMedicineList().getKeyAt(medicine_to_add -1)).split(" ")[1];

System.out.println("Selected medicine" + medicine_name);   

0 Answers0