繁体   English   中英

如何使用定义的 class 填充 ArrayList?

[英]How do I populate an ArrayList with the defined class?

我是 CompSci 的一年级学生,为一项任务创建医院管理系统。 我为用户填充的患者创建了一个 arraylist。 我还为医生创建了一个预定义的 arraylist。 问题是 ArrayList 无法识别紧接其下方定义的 class Doctor。 这也会在我试图调用数组以链接 HashMap 中的医生和患者的代码中进一步导致问题。

代码如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.*;

public class AandEDepartment {

public static void main(String[] args) {

    ArrayList<Doctor> doctors = new ArrayList<>();
            doctors.add(new Doctor("James", "Cardiovascular"));
            doctors.add(new Doctor("Richard", "Dermatology"));
            doctors.add(new Doctor("Peter", "Orthopedics"));

        class Doctor {
            String name;
            String specialisation;
            
            public Doctor(String name, String specialisation){
                this.name = name;
                this.specialisation = specialisation;
            }
            //create getters for each of these variables
            public String getName() {
                return name;
            }
            public String getSpecialisation() {
                return specialisation;
            }
            
        class Patient {
            int patientID;
            String patientFirstName;
            String patientLastName;
            int patientAge;
            String condition;
            double cost;
            
            public Patient (int patientID, String patientFirstName, String patientLastName, int patientAge, String condition, double cost)

这与拥有多个班级(医生/病人)有关吗? 我试过将“医生”和“病人”重新定义为公开的,但这会产生不同的错误(非法修饰符)。 有任何想法吗?

谢谢。

在 java 中,最佳做法是将每个类分隔在自己的文件中。 您目前正在AandEDepartment中定义DoctorPatient他们都应该在不同的文件中。 当在main class 中调用时,您的 IDE 应该会自动导入这些类。

此外,您不应在方法内声明类。 如果您绝对想在AandEDepartment中声明DoctorPatient ,请参阅https://www.w3schools.com/java/java_inner_classes.asp ,即使您不应该在编程过程中养成这种习惯。

希望这有帮助

正如其他人评论的那样:

您的DoctorPatient类是业务逻辑的核心部分。 它们很可能用于您应用程序的各个部分。 所以它们应该存储在自己的.java文件中,而不是嵌套在另一个 class 中。

此外,如果您的 class 的主要目的是透明且不变地传递数据,请将您的 class 定义为record 在记录中,编译器隐式创建构造函数、getter、 equals & hashCodetoString

在单独的Doctor.java文件中:

package work.basil.example.hospital;

public record Doctor(String name , String specialization ) { }

在单独的Patient.java文件中:

package work.basil.example.hospital;

public record Patient(
        int patientID ,
        String patientFirstName ,
        String patientLastName ,
        int patientAge ,
        String condition ,
        double cost
) { }

顺便说一下,您的Patient class 看起来您正在将两个不同的实体混为一谈,即患者(人)与他们的治疗。 但我会在这里忽略这个问题。

为部门定义一个 class。 同样,当且仅当 class 的主要目的是透明且不变地传递数据时,才使用记录。 否则重写为常规的 class。

在单独的Department.java文件中:

package work.basil.example.hospital;

import java.util.Set;

public record Department( String name , Set < Doctor > doctors ) { }

编写一个应用程序 class 来填充一些数据。

在单独的App.java文件中:

package work.basil.example.hospital;

import java.util.Set;

public class App
{
    public static void main ( String[] args )
    {
        String deptName = "A&E";
        Set < Doctor > doctorSet =
                Set.of(
                        new Doctor( "James" , "Cardiovascular" ) ,
                        new Doctor( "Richard" , "Dermatology" ) ,
                        new Doctor( "Peter" , "Orthopedics" )
                );
        Department aAndE = new Department( deptName , doctorSet );

        System.out.println( "aAndE = " + aAndE );
    }
}

运行时:

aAndE = Department[name=A&E, doctors=[医生[name=James, specialization=Cardiovascular], Doctor[name=Peter, specialization=Orthopedics], Doctor[name=Richard, specialization=Dermatology]]]

暂无
暂无

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

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