java 获取以下代码的错误“无法解析为变量”

lmyy7pcs  于 2023-05-05  发布在  Java
关注(0)|答案(1)|浏览(183)

appointmentP 1、appointmentP 2和appointmentP 3都给了我医生示例的错误。以及患者示例的注解P1-P5。

package controller;

import java.util.ArrayList;
import java.util.Date;

import model.Appointment;
import model.Doctor;
import model.DoctorList;
import model.Notes;
import model.Patient;
import model.PatientList;
import view.MainGUI;

public class MainDriver {

    public static void main(String[] args) {
        
        Doctor d1 = new Doctor("D1", "Dr. ", "Wyatt", "(Cardiology)", appointmentP1); 
        Doctor d2 = new Doctor("D2", "Dr. ", "Brandanowitz", "(Respiratory Therapy)", appointmentP2);
        Doctor d3 = new Doctor("D3", "Dr. ", "Perkins", "(Obstetrics & Gynocology)", appointmentP3);
        
        DoctorList dlist = new DoctorList(); 
        dlist.Insert(d1);
        dlist.Insert(d2);
        dlist.Insert(d3);
        
        Patient p1 = new Patient("P1", 51, "Leslie ", "Knope", "9/16/1971", notesP1); 
        Patient p2 = new Patient("P2", 52, "Ron ", "Swanson", "6/26/1970", notesP2); 
        Patient p3 = new Patient("P3", 38, "April ", "Ludgate", "6/26/1984", notesP3); 
        Patient p4 = new Patient("P4", 43, "Andy ", "Dwyer", "6/21/1979", notesP4); 
        Patient p5 = new Patient("P5", 53, "Donna ", "Meagle", "4/20/1970", notesP5); 
        
        PatientList plist = new PatientList(); 
        plist.Insert(p1);
        plist.Insert(p2);
        plist.Insert(p3);
        plist.Insert(p4);
        plist.Insert(p5);
    
        
        Date date = new Date();
        System.out.printf("%1$s %2$tB %2$td, %2$tY", "Appointment date:", date);
        
        
        
        //LESLIE
        Appointment apptP1 = new Appointment("A1", date, d3, p1);
        Appointment apptP2 = new Appointment("A2", date, d3, p1);
        
        //RON
        Appointment apptP3 = new Appointment("A3", date, d1, p2);
        Appointment apptP4 = new Appointment("A4", date, d1, p2);
        Appointment apptP5 = new Appointment("A5", date, d1, p2);
        
        //APRIL
        Appointment apptP6 = new Appointment("A6", date, d3, p3);
        
        //ANDY
        Appointment apptP7 = new Appointment("A7", date, d1, p4);
        Appointment apptP8 = new Appointment("A8", date, d1, p4);
        
        //DONNA
        Appointment apptP9 = new Appointment("A9", date, d2, p5);
        Appointment apptP10 = new Appointment("A10", date, d2, p5);
        
        
        ArrayList<Appointment> appointmentP1 = new ArrayList<Appointment>();
        appointmentP1.add(apptP1);
        appointmentP1.add(apptP2);
        
        ArrayList<Appointment> appointmentP2 = new ArrayList<Appointment>();
        appointmentP2.add(apptP3);
        appointmentP2.add(apptP4);
        appointmentP2.add(apptP5);
        
        ArrayList<Appointment> appointmentP3 = new ArrayList<Appointment>();
        appointmentP3.add(apptP6);
        
        ArrayList<Appointment> appointmentP4 = new ArrayList<Appointment>();
        appointmentP4.add(apptP7);
        appointmentP4.add(apptP8);
        
        ArrayList<Appointment> appointmentP5 = new ArrayList<Appointment>();
        appointmentP5.add(apptP9);
        appointmentP5.add(apptP10);
        
    
    
    
        Notes n1 = new Notes("patient note goes here", d3, "P1");
        Notes n2 = new Notes("patient note goes here", d3, "P1");
        Notes n3 = new Notes("patient note goes here", d3, "P1");
        Notes n4 = new Notes("patient note goes here", d3, "P1");
        Notes n5 = new Notes("patient note goes here", d1, "P2");
        Notes n6 = new Notes("patient note goes here", d1, "P2");
        Notes n7 = new Notes("patient note goes here", d1, "P2");
        Notes n8 = new Notes("patient note goes here", d1, "P2");
        Notes n9 = new Notes("patient note goes here", d3, "P3");
        Notes n10 = new Notes("patient note goes here", d3, "P3");
        Notes n11 = new Notes("patient note goes here", d3, "P3");
        Notes n12 = new Notes("patient note goes here", d1, "P4");
        Notes n13 = new Notes("patient note goes here", d1, "P4");
        Notes n14 = new Notes("patient note goes here", d1, "P4");
        Notes n15 = new Notes("patient note goes here", d2, "P5");
        Notes n16 = new Notes("patient note goes here", d2, "P5");
        Notes n17 = new Notes("patient note goes here", d2, "P5");
        Notes n18 = new Notes("patient note goes here", d2, "P5");
        
        ArrayList<Notes> notesP1 = new ArrayList<Notes>();
        notesP1.add(n1);
        notesP1.add(n2);
        notesP1.add(n3);
        notesP1.add(n4);
        
        ArrayList<Notes> notesP2 = new ArrayList<Notes>();
        notesP2.add(n5);
        notesP2.add(n6);
        notesP2.add(n7);
        notesP2.add(n8);
        
        ArrayList<Notes> notesP3 = new ArrayList<Notes>();
        notesP3.add(n9);
        notesP3.add(n10);
        notesP3.add(n11);
        
        ArrayList<Notes> notesP4 = new ArrayList<Notes>();
        notesP4.add(n12);
        notesP4.add(n13);
        notesP4.add(n14);
        
        ArrayList<Notes> notesP5 = new ArrayList<Notes>();
        notesP5.add(n15);
        notesP5.add(n16);
        notesP5.add(n17);
        notesP5.add(n18);
                
    }

}

不管我把它们放在代码的哪个地方,总会有一些错误。我甚至试着让它们成为全局变量,但没有成功。任何建议将不胜感激。谢谢!

ffx8fchx

ffx8fchx1#

你有循环依赖关系,即:

A Doctor requires an appointment list

An appointment list requires appointments 

An appointment requires a Doctor.

Doctor构造函数(List<Appointment>)中删除第4个参数,并在DoctorsetAppointments(List<Appointment> appointments)中为约会列表定义一个setter。在创建了appointmentP1等之后使用此setter,例如:

d1.setAppointments(appointmentP1);

此外,最好用接口名声明一个变量,用类示例化,用〈〉推断泛型类型:

List<Appointment> appointmentP1 = new ArrayList<>();

上面的代码解决了你的“不能作为变量解决”的问题,但我认为你的数据结构很奇怪。我会删除医生的预约列表,并从所有的预约中为医生生成它。

相关问题