arrayList
unknown
plain_text
a year ago
4.1 kB
5
Indexable
import java.util.ArrayList; // Import ArrayList class public class Medic { public static void main(String[] args) { // Create an instance of the Hospital class Hospital hosp = new Hospital(); hosp.setHospitalName("HSA"); // Set the hospital name // Create instances of the Doctors class with birthdates and set doctor names Doctors doc1 = new Doctors(1980, 12, 1); doc1.setDoctorName("Dr.Abu Ali"); Doctors doc2 = new Doctors(1981, 1, 1); doc2.setDoctorName("Dr.Kim Lee"); Doctors doc3 = new Doctors(1985, 12, 12); doc3.setDoctorName("Dr.Nadela A/P Ram"); // Create an ArrayList to store Doctor objects ArrayList<Doctors> doctorList = new ArrayList<>(); // Add Doctor objects to the ArrayList doctorList.add(doc1); doctorList.add(doc2); doctorList.add(doc3); // Set the list of doctors in the Hospital hosp.setDoctors(doctorList); // Display the size of the doctorList System.out.println("Size of list: " + doctorList.size()); // Display the names of doctors and the hospital they belong to System.out.println(hosp.getDoctors() + " are Doctors of " + hosp.getHospName()); // Remove the doctor at index 1 from the list doctorList.remove(1); // Display the updated list of doctors System.out.println("Display list:" + doctorList); // Create new Doctor objects and add them to specific positions in the list Doctors newDoc1 = new Doctors(1984, 5, 8); newDoc1.setDoctorName("Dr.Badariah"); doctorList.add(0, newDoc1); Doctors newDoc2 = new Doctors(1982, 22, 5); newDoc2.setDoctorName("Dr.Zang Yu"); doctorList.add(3, newDoc2); // Display the final list of doctors System.out.println("Display list: " + doctorList); // Display the names and birthdays of doctors using a loop System.out.println("Doctor list and their birthday: "); for (Doctors doctor : doctorList) { System.out.println(doctor.getDoctorName() + " - " + doctor.getBday()); } } } // Hospital class definition class Hospital { private String hospitalName; private ArrayList<Doctors> doctors; // ArrayList to store Doctor objects // Getter method for hospital name public String getHospName() { return hospitalName; } // Setter method for hospital name public void setHospitalName(String hospName) { hospitalName = hospName; } // Getter method for the list of doctors public ArrayList<Doctors> getDoctors() { return doctors; } // Setter method for the list of doctors public void setDoctors(ArrayList<Doctors> doc) { doctors = doc; } } // Doctors class definition class Doctors { private String doctorName; private Birthday bday; // Birthday object to store birthdate // Constructor for Doctors class Doctors(int y, int m, int d) { bday = new Birthday(y, m, d); // Create a Birthday object with the given birthdate } // Getter method for doctor name public String getDoctorName() { return doctorName; } // Setter method for doctor name public void setDoctorName(String docName) { doctorName = docName; } // toString method to return the doctor name public String toString() { return doctorName; } // Getter method for Birthday object public Birthday getBday() { return bday; } } // Birthday class definition class Birthday { int year, month, day; // Constructor for Birthday class Birthday(int y, int m, int d) { year = y; month = m; day = d; } // toString method to return the formatted birthday public String toString() { return String.format("%s-%s-%s", year, month, day); } }
Editor is loading...
Leave a Comment