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!
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