0

i am working in my project and it is about a clinic , my class is appointment , and i have to let the user to enter the day, time, section, doctor name, patient name. and store it an ArrayList i wrote the code but when i run the project there is no output and i don't know why :(

package appointments;
import java.util.Scanner;
import java.util.ArrayList;

public class Appointment {


  public static void main(String[] args) {

    ArrayList<Object> appointments = new ArrayList<>();
    Scanner input = new Scanner (System.in);
    System.out.println("enter day, time, section , doctor , you name in order to book appointment : ");

    appointment xx = new appointment();

    for (int i=0; i<5; ++i)
    xx.setAppDay(input.nextLine());
    xx.setAppTime(input.nextLine());
    xx.setAppSection(input.nextLine());
    xx.setAppDoctor(input.nextLine());
    xx.setAppPatient(input.nextLine());

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

}


public static class appointment {
    public String appDay;
    public String appTime;
    public String appSection;
    public String appDoctor;
    public String appPatient;


    public appointment(String appDay, String appTime, String appSection, String appDoctor, String appPatient) {
        this.appDay = appDay;
        this.appTime = appTime;
        this.appSection = appSection;
        this.appDoctor = appDoctor;
        this.appPatient = appPatient;

    }

    public appointment() {

    }

    public void setAppDay(String appDay) {
        this.appDay = appDay;
    }

    public void setAppTime(String appTime) {
        this.appTime = appTime;
    }

    public void setAppSection(String appSection) {
        this.appSection = appSection;
    }

    public void setAppDoctor(String appDoctor) {
        this.appDoctor = appDoctor;
    }

    public void setAppPatient(String appPatient) {
        this.appPatient = appPatient;
    }

    public String getAppDay() {
        return appDay;
    }

    public String getAppTime() {
        return appTime;
    }

    public String getAppSection() {
        return appSection;
    }

    public String getAppDoctor() {
        return appDoctor;
    }

    public String getAppPatient() {
        return appPatient;
    }

  }

}
Mark
  • 90,562
  • 7
  • 108
  • 148
ishadenkhaled
  • 33
  • 1
  • 6

3 Answers3

0

Your loop has no braces and you only instantiate one appointment. You wanted something like,

for (int i = 0; i < 5; ++i) {
    appointment xx = new appointment();
    xx.setAppDay(input.nextLine());
    xx.setAppTime(input.nextLine());
    xx.setAppSection(input.nextLine());
    xx.setAppDoctor(input.nextLine());
    xx.setAppPatient(input.nextLine());

    appointments.add(xx);
}

Then you need to override toString() in appointment.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Currently the same appointment object is being added to the list, so the list would only have a single entry in it.

So move the object creation and addition to the list along with setting the appointment fields inside the for loop. Please add the curly braces correctly as currently only the xx.setAppDay(input.nextLine()) is part of the for loop.

Also appointment shouldn't be a static class multiple objects need to be created.

Srijith Kartha
  • 296
  • 1
  • 2
  • 9
-1

You need to implement a toString() Method for this purpose. And print it like this.

public String toString(){
    return this.appDay; // return the output you want, so build a String using your attributes
}

System.out.println(Arrays.toString(appointments));

Edit: And do what Elliott Frisch said.

mejasper
  • 64
  • 8
  • i fix it but still doesn't show any output – ishadenkhaled Nov 21 '18 at 17:23
  • Whats your current code and where did you implement the toString function? – mejasper Nov 21 '18 at 18:26
  • it is the same code, but i fixed the for-loop with { } , and added toString inside appointment class. still no output and creating+declaring the object shows an error with (non-static variable...) and i don't know why. – ishadenkhaled Nov 21 '18 at 19:54
  • it is the same code, but i fixed the for-loop with { } , and added toString inside appointment class. still no output and creating+declaring the object shows an error with (non-static variable...) and i don't know why. – ishadenkhaled Nov 21 '18 at 19:54