【问题标题】:inserting multiple objects to arraylist将多个对象插入arraylist
【发布时间】:2019-04-27 13:32:19
【问题描述】:

我希望用户输入预约变量(姓名、医生姓名、部门、​​日期、时间)并将其存储在一个对象中,然后将其添加到 ArrayList。它应该能够添加另一个约会。直到用户决定不预约。告诉我我的代码中缺少什么和平?

public static void main(String[] args) {
    Scanner input =new Scanner(System.in);
    ArrayList<Object> appointments = new ArrayList<>();
    appointment xx = new appointment();

    System.out.println("do you want to book an appointment? (0/yes , 1/no) ");
    int ch = input.nextInt();

    while (ch==0){
    System.out.println("** Book appointment **");
    System.out.println("* to book an appointment choose a doctor, section, time and day from below :");
    System.out.println("Doctors : Dr.ahmed , Dr.sara , Dr.ali , Dr.maha ");
    System.out.println("Sections : Skin , Dental "); 
    System.out.println("Times : from 8AM to 4PM");
    System.out.println("Days : from Sunday to Thursday");
    System.out.println("          ");

    System.out.println("please enter your name :");
    String a1 = input.nextLine();
    xx.setPatient(a1);

    System.out.println("please enter the doctor name : ");
    String a2 = input.nextLine();
    xx.setDoctor(a2);

    System.out.println("please enter the section : ");
    String a3 = input.nextLine();
    xx.setSection(a3);

    System.out.println("please enter the time : ");
    String a4 = input.nextLine();
    xx.setTime(a4);

    System.out.println("please enter the day : ");
    String a5 = input.nextLine();
    xx.setDay(a5);

    appointments.add(xx);
    System.out.println(appointments);

    System.out.println("do you want to book another appointment? (0/yes , 1/no) ");
    ch = input.nextInt();
 }

}

}

 class appointment {
    public String Day;
    public String Time;
    public String Doctor;
    public String Section;
    public String Patient;
    public static int numberOfApp;



    public appointment(){

 }


    public appointment(String Day, String Time, String Doctor, String Section, String Patient) {
        this.Day = Day;
        this.Time = Time;
        this.Doctor = Doctor;
        this.Section = Section;
        this.Patient = Patient;
    }

    public static void setNumberOfApp(int numberOfApp) {
    appointment.numberOfApp = numberOfApp;
    }

    public void setDay(String Day) {
        this.Day = Day;
    }

    public void setTime(String Time) {
        this.Time = Time;
    }

    public void setDoctor(String Doctor) {
        this.Doctor = Doctor;
    }

    public void setSection(String Section) {
        this.Section = Section;
    }

    public void setPatient(String Patient) {
        this.Patient = Patient;
    }

    public static int getNumberOfApp() {
    return numberOfApp;
    }

    public String getDay() {
        return Day;
    }

    public String getTime() {
        return Time;
    }

    public String getDoctor() {
        return Doctor;
    }

    public String getSection() {
        return Section;
    }

    public String getPatient() {
        return Patient;
    }


    @Override
    public String toString(){
        return " ";
    }

    public void add_appointment (){

    }

 }

【问题讨论】:

  • 您只创建一个约会对象,并不断添加对列表的引用。使用调试器,您应该能够看到您需要为要添加的每个约会创建一个约会对象。

标签: java oop object arraylist


【解决方案1】:

类名应以大写字符开头

appointment xx = new appointment();

您只能创建一个 Appointment 对象。不能仅仅因为将它添加到 ArrayList 就重复使用它。

您需要在您的 while 循环中创建 Appointment 对象,以便每次约会都有不同的对象。

【讨论】:

    猜你喜欢
    • 2012-05-18
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多