简体   繁体   中英

Printing objects in an array list

I am making a Hospital Management system project in Java. I have 3 classes. One class has the main method, the other class is the Hospital class which has methods printing out the details of the hospital and another method printing out the staff of the hospital. The third class is the HospitalStaff class which has a toString method for all details of the Staff, and an addStaff method. The staff are added in an arraylist. i want the method that prints all the staff to exist in the Hospital class how do I do that?

Here is my code:

import java.util.ArrayList;
import java.util.List;

public class HospitalProject {

    public static void main(String[] args) {
        Hospital h = new Hospital();
        h.Hospitalinformation();
        HospitalStaff hs = new HospitalStaff();
        hs.addHospitalStaff("A103", "Name1", "Lastname1", "Surgery", "Doctor");
        hs.addHospitalStaff("A124", "Name2", "Lastname2", "Surgery", "Doctor");
        
        
    }

}
/* This is the Hospital class. It has 3 methods. The HospitalInformation method prints the name and the address, name, email address and phone number of the hospital. 
 * It declares and array list by the name of listofStaff of the type HospitalStaff. 
 * It consists of a seeStaff method which is supposed to show all staff working at the hospital. 
 */

class Hospital{
    public String name = "Red Cross";
    public String Address = "whatever";
    public String phonenum = "whatever2";
    private String email = "whatever@gmail.com";
    
    public void Hospitalinformation() {
        System.out.println("The name of the hospital is: " +name + " \nThe name of the Address: " + Address + "\nPhone number: "+phonenum + " \nEmail Address: " + email);
    }
    public void seeStaff() {
        HospitalStaff hs1 = new HospitalStaff();
        for(String stafflist: hs1.listofStaff) {
            System.out.println(stafflist);
        }
    }
    
}

/*The Hospital Staff method consists of toString method as well as a addHospitalStaff method. 
 * The seeStaff method is added for debugging purposes, it is however, supposed to exist in the Hospital method. 
 */
class HospitalStaff {
    
    private String StaffId;
    public String firstname;
    public String lastname;
    public String department;
    public String stafftype;
    List<String>listofStaff =  new ArrayList<String>();
    
        
    public void addHospitalStaff(String StaffId, String firstname, String lastname, String department, String stafftype) {
        String here = StafftoString(StaffId, firstname, lastname, department, stafftype);
        listofStaff.add(here);
    }
    public String StafftoString(String StaffId, String firstname, String lastname, String department, String stafftype) {
        return String.valueOf(firstname) + " " + String.valueOf(lastname) +" " + String.valueOf(StaffId) +" " + String.valueOf(department) +" " + String.valueOf(stafftype);
    }
    public void seeStaff() {
        for(String stafflist: listofStaff) {
            System.out.println(stafflist);
        }
    }

}


I tried creating a method called seeStaff() in the Hospital class and created an HospitalStaff object by the name of "hs1" in that method. I then used the HospitalStaff object to call the arraylist (which was called listofStaff) that was created in the HospitalStaff class. I used it in the for each loop as (String stafflist: hs1.listofStaff). However, when i create a Hospital class object in the main and call the seeStaff() method, it does not print anything. I am not sure why that happens.

Inside your seeStaff method, you are creating a new instance of HospitalStaff , which when created has no staff members in it. Because of this, when you iterate over the list of staff members in that empty instance, you get no output. You create another instance of HospitalStaff in your main() but you don't do anything with that instance.

One way to fix this would be to create your HospitalStaff instance first. Then, when you create your Hospital instance, pass the HospitalStaff instance into the constructor for Hospital so that it can be stored in the instance. Then your showStaff method can print the values in your HospitalStaff instance. Here's what that looks like:

import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) {

        // Create an object containing the hospital's staff
        HospitalStaff hs = new HospitalStaff();
        hs.addHospitalStaff("A103", "Name1", "Lastname1", "Surgery", "Doctor");
        hs.addHospitalStaff("A124", "Name2", "Lastname2", "Surgery", "Doctor");

        // Create a Hospital object, passing it the `HospitalStaff` instance containing the hospital's staff.
        Hospital h = new Hospital(hs);
        h.Hospitalinformation();

        h.seeStaff();  // <- Print a list of hospital staff members
    }

}
/* This is the Hospital class. It has 3 methods. The HospitalInformation method prints the name and the address, name, email address and phone number of the hospital.
 * It declares and array list by the name of listofStaff of the type HospitalStaff.
 * It consists of a seeStaff method which is supposed to show all staff working at the hospital.
 */

class Hospital{
    public String name = "Red Cross";
    public String Address = "whatever";
    public String phonenum = "whatever2";
    private String email = "whatever@gmail.com";

    private HospitalStaff hs;

    public Hospital(HospitalStaff hs) {
        this.hs = hs;  // <- Associate the passed in HospitalStaff instance with this `Hospital` instance.
    }

    public void Hospitalinformation() {
        System.out.println("The name of the hospital is: " +name + " \nThe name of the Address: " + Address + "\nPhone number: "+phonenum + " \nEmail Address: " + email);
    }

    // Print the members of the hospital's staff
    public void seeStaff() {
        
        for(String stafflist: hs.listofStaff) {
            System.out.println(stafflist);
        }
    }

}

/*The Hospital Staff method consists of toString method as well as a addHospitalStaff method.
 * The seeStaff method is added for debugging purposes, it is however, supposed to exist in the Hospital method.
 */
class HospitalStaff {

    private String StaffId;
    public String firstname;
    public String lastname;
    public String department;
    public String stafftype;
    List<String>listofStaff =  new ArrayList<String>();


    public void addHospitalStaff(String StaffId, String firstname, String lastname, String department, String stafftype) {
        String here = StafftoString(StaffId, firstname, lastname, department, stafftype);
        listofStaff.add(here);
    }
    public String StafftoString(String StaffId, String firstname, String lastname, String department, String stafftype) {
        return String.valueOf(firstname) + " " + String.valueOf(lastname) +" " + String.valueOf(StaffId) +" " + String.valueOf(department) +" " + String.valueOf(stafftype);
    }
    public void seeStaff() {
        for(String stafflist: listofStaff) {
            System.out.println(stafflist);
        }
    }

}

Result:

The name of the hospital is: Red Cross 
The name of the Address: whatever
Phone number: whatever2 
Email Address: whatever@gmail.com
Name1 Lastname1 A103 Surgery Doctor
Name2 Lastname2 A124 Surgery Doctor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM