Reduce Multiple Values of an Array

Wolfizzy :

I'm making a booking system where I need to 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:

private String[] stations = {"Lorem", "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");

    System.out.printf("%-20s %-20s %-21s %-23s %-20s\n", "DEPARTING", "ARRIVING", "FIRST CLASS", "STANDARD CLASS", "EXCURSION CLASS");
    System.out.println("-------------------------------------------------------------------------------------------------------");
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[0], stations[1], getNumOfFirstClassSeats()[0]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[1], stations[2], getNumOfFirstClassSeats()[1]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[2], stations[3], getNumOfFirstClassSeats()[2]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[3], stations[4], getNumOfFirstClassSeats()[3]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[4], stations[5], getNumOfFirstClassSeats()[4]);
}

BookingSystem.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.
if (boardingPoint.equals("0") && destinationPoint.equals("1")) {
  numOfFirstClassSeats[0] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("2")) {
  numOfFirstClassSeats[0-1] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("3")) {
  numOfFirstClassSeats[0-2] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("4")) {
  numOfFirstClassSeats[0-3] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("5")) {
  numOfFirstClassSeats[0-4] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
}

// The same goes for the other stations
...

else {
  System.out.println("ERROR: Invalid input!");
  return;
}

Problem: This way of checking for boarding and destination points (in BookingSystem.java file) is messy and repetitive; is there any other cleaner and efficient way of doing the same thing, e.g. using for loops? Thanks!

akuzminykh :

Yes, you can simplify your code with for statements. There are two places where the code is screaming for it.


    System.out.printf("%-20s %-20s %-21s %-23s %-20s\n", "DEPARTING", "ARRIVING", "FIRST CLASS", "STANDARD CLASS", "EXCURSION CLASS");
    System.out.println("-------------------------------------------------------------------------------------------------------");
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[0], stations[1], getNumOfFirstClassSeats()[0]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[1], stations[2], getNumOfFirstClassSeats()[1]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[2], stations[3], getNumOfFirstClassSeats()[2]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[3], stations[4], getNumOfFirstClassSeats()[3]);
    System.out.format("%-20s %-20s %-21s %-23s %-20s\n", stations[4], stations[5], getNumOfFirstClassSeats()[4]);

In this part you can see that you are getting the values from the arrays with an index that increases line by line. Just use a for statement.

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]);
}

// Reduce the number of available seats to the respective stations.
if (boardingPoint.equals("0") && destinationPoint.equals("1")) {
  numOfFirstClassSeats[0] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("2")) {
  numOfFirstClassSeats[0-1] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("3")) {
  numOfFirstClassSeats[0-2] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("4")) {
  numOfFirstClassSeats[0-3] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
} else if (boardingPoint.equals("0") && destinationPoint.equals("5")) {
  numOfFirstClassSeats[0-4] -= numOfFirstClassAdults;
  stationList.setNumOfFirstClassSeats(numOfFirstClassSeats);
}

This is the other place. Same story, a number that increases statement by statement.

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;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=341002&siteId=1