简体   繁体   English

打印数组列表中的对象

[英]Printing objects in an array list

I am making a Hospital Management system project in Java. I have 3 classes.我正在 Java 做一个医院管理系统项目。我有 3 个班级。 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.一个 class 有主要方法,另一个 class 是医院 class,它有方法打印出医院的详细信息,另一个方法打印出医院的工作人员。 The third class is the HospitalStaff class which has a toString method for all details of the Staff, and an addStaff method.第三个 class 是 HospitalStaff class,它有一个用于员工所有详细信息的 toString 方法和一个 addStaff 方法。 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?员工被添加到 arraylist 中。我想要打印所有员工的方法存在于医院 class 中,我该怎么做?

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.我尝试在 Hospital class 中创建一个名为 seeStaff() 的方法,并在该方法中创建了一个名为“hs1”的 HospitalStaff object。 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).然后,我使用 HospitalStaff object 调用在 HospitalStaff class 中创建的 arraylist(称为 listofStaff)。我在 for each 循环中将其用作(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.但是,当我在 main 中创建 Hospital class object 并调用 seeStaff() 方法时,它不会打印任何内容。 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.在您的seeStaff方法中,您正在创建HospitalStaff的新实例,创建时其中没有任何工作人员。 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.因此,当您遍历该空实例中的工作人员列表时,您不会得到 output。您在main()中创建了另一个HospitalStaff实例,但您不对该实例执行任何操作。

One way to fix this would be to create your HospitalStaff instance first.解决此问题的一种方法是首先创建您的HospitalStaff实例。 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.然后,当您创建Hospital实例时,将HospitalStaff实例传递到Hospital的构造函数中,以便它可以存储在实例中。 Then your showStaff method can print the values in your HospitalStaff instance.然后您的showStaff方法可以打印HospitalStaff实例中的值。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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