0

I take in a file which has a name (table) and the number of seats:

table1 6

table2 2

table3 4

I have an array of class Reservation which will take in the the name and the seat. I am having trouble converting the number of seats into the array. How do i go about doing so?

public class Reservable {

protected String id;
private Reservation[] arrayRes = new Reservation[10];



public Reservable (Scanner fileIn) {
    while(fileIn.hasNext()) {
        id = fileIn.next();
        for(int i = 0; i < arrayRes.length; i++) {
            int seat = fileIn.nextInt();
            arrayRes[i] = seat;

        }
    }


}

here is my Reservation class:

public class Reservation {
    private String name;
    private int timeSlot;
    private Reservable myReservable;

    public Reservation(String name, int timeSlot) {
        this.name = name;
        this.timeSlot = timeSlot;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getTimeSlot() {
        return timeSlot;
    }

    public void setTimeSlot(int timeSlot) {
        this.timeSlot = timeSlot;
    }

    public Reservable getMyReservable() {
        return myReservable;
    }

    public void setMyReservable(Reservable myReservable) {
        this.myReservable = myReservable;
    }

    public boolean isActive() {
        return false;
    }
0

2 Answers 2

1

You can read line by line since your file has a reservation by line. I propose you to have a Reservation constructor with two parameters (name and nbSeat).
A remark : you array of reservation has a fixed size : 10. If you file has more than 10 elements, a ArrayIndexOutOfBoundsException will be risen.
If the number of reservation may be superior to 10 or is variable, you should use a List rather than a array.

protected String id;
private Reservation[] arrayRes = new Reservation[10];


public Reservable (Scanner fileIn) {
    int i=0;
    while(fileIn.hasNextLine()) {
        String line = fileIn.nextLine();
        String[] token = line.split("\\s");
        String name = token[0];
        int nbSeat= Integer.valueOf(token[1)];
         // add in your array
         Reservation reservation = new Reservation(name,nbSeat);
        arrayRes[i] = reservation ;

    }
      i++;
}

And Reservation :

public class Reservation{

  public Reservation(String name, int nbSeat){
    this.name=name;
    this.nbSeat=nbSeat;
  }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

you are welcome :) I proposed you a constructor as it was not originally present in your question.
0

You need to show us what your Reservation class looks like, but if it uses the conventional getters and setters this is the correct way:

public Reservable (Scanner fileIn) {
    while(fileIn.hasNext()) {

        for(int i = 0; i < arrayRes.length; i++) {
            int seat = fileIn.nextInt();
            arrayRes[i].setSeat(seat);
            id = fileIn.next();
            arrayRes[i].setId(id);

            }
        }
}

1 Comment

whoops my apology, i just updated with the code but yeah its just typical getters and setters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.