将多个对象插入arraylist

时间:2018-11-25 21:17:34

标签: java oop object arraylist

我希望用户输入约会变量(名称,医生姓名,部门,日期,时间)并将其存储在对象中,然后将其添加到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 (){

    }

 }

1 个答案:

答案 0 :(得分:1)

类名应以大写字母开头

appointment xx = new appointment();

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

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