0

I'm building a booking system where I ask the user for a boarding and destination point. There are six stations, and I want to subtract the number of passengers from the boarding and destination point and all of the in-between stations.

TrainService.java class:

// Prompt user for the boarding point.
System.out.println("\n0. Lorem");
System.out.println("1. Ipsum");
System.out.println("2. Dolor");
System.out.println("3. Sit");
System.out.println("4. Amet\n");

System.out.print("Select a boarding point: ");
String boardingPoint = sc.nextLine();
int boardingPointNum = Integer.parseInt(boardingPoint);

// Prompt user for the destination point.
System.out.println("\n1. Ipsum");
System.out.println("2. Dolor");
System.out.println("3. Sit");
System.out.println("4. Amet");
System.out.println("5. Consectetur\n");

System.out.print("Select a destination point: ");
String destinationPoint = sc.nextLine();
int destinationPointNum = Integer.parseInt(destinationPoint);

// Reduce the number of available seats to the respective stations.
for (int i = 0; i < 5; i++) {
    if (boardingPoint.equals("0") && destinationPoint.equals(Integer.toString(i + 1))) {
        numOfFirstClassSeats[0 - i] -= numOfFirstClassAdults;
        stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
        break;
    }
}
...

BookingSystem.java class:

// Instance variables.
private String[] stations = {"Lorem", "Ipsum", "Dolor", "Sit", "Amet", "Consectetur"};
private int[] numOfFirstClassSeats = {48, 48, 48, 48, 48};

public int[] getNumOfFirstClassSeats() {
    return this.numOfFirstClassSeats;
}

public void setNumOfFirstClassSeats(int[] numOfFirstClassSeats) {
    this.numOfFirstClassSeats = numOfFirstClassSeats;
}

public void printStations() {
    System.out.println("\n~ AVAILABLE SEATS ~");
    System.out.println("-------------------\n");

    String pattern = "%-20s %-20s %-21s %-23s %-20s\n";

    System.out.printf(pattern, "DEPARTING", "ARRIVING", "FIRST CLASS", "STANDARD CLASS", "EXCURSION CLASS");
    System.out.println("-------------------------------------------------------------------------------------------------------");
    for (int i = 0; i < 5; i++) {
        System.out.format(pattern, stations[i], stations[i + 1], getNumOfFirstClassSeats()[i], getNumOfStandardSeats()[i], getNumOfExcursionSeats()[i]);
    }
}

Problem 1: When I run the code to book a First Class seat, the code only alters the available seats for "Lorem" and "Ipsum" if the boarding point is "Lorem" and the destination point is "Ipsum".

If I choose the boarding point as "Lorem" and the destination point is "Sit", the output is: Index -2 out of bounds for length 5.

Problem 2: If I have 5 passengers and "Lorem" is the boarding point and "Amet" is the destination point, I want the available seats to be reduced to 43 passengers from "Lorem" to "Amet".

How do I accomplish this? Thank you for your help!

3 Answers 3

2

An alternative way of doing this may be to consider using Maps to map different boarding points and destinations. I wouldn't say it would be too verbose because there are a limited number of combinations, but it could solve Problem 2.

If a passenger selects a boarding point and destination point, you can compare the values they entered to a Map, then reduce the passengers for that certain combination.

Could you also display the error log for Problem 1? That would give me a better idea of what the issue is.

Sign up to request clarification or add additional context in comments.

Comments

1

For problem 1 please post the whole code.

Problem 2:

It's only a thought-provoking impulse.

I would make it like this:

        System.out.println(Arrays.toString(numOfFirstClassSeats));        
        for(int i=0; i<numOfFirstClassSeats.length; i++){
            if((destinationPoint >= i)&(boardingPointNum <= i)){
                numOfFirstClassSeats[i] = numOfFirstClassSeats[i]-1;
            }
        }        
        System.out.println(Arrays.toString(numOfFirstClassSeats));

But you have to add some security if's. If you only go one station boardingPointNum = destinationPoint

Comments

1

Check this line in TrainService.java:

numOfFirstClassSeats[0 - i]

As i increments, the value goes to negative, so it should be:

numOfFirstClassSeats[i - 0]

2 Comments

If the answer helped u do u mind up voting and accepting the answer.. Thanks in advance
How about just numOfFirstClassSeats[i]?

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.