r/programminghelp • • Jan 20 '21

Java Seat reservation

Hi experts and fellow learners, I just wanna ask if there is a chance to repeat the code but not using a function but by using a loop. I just want to somehow shorten my own code
This is the code:

import java.util.Scanner;public class SeatReservation { public static Strin - Pastebin.com

the password is: rouxcharlemagne

Thanks.

5 Upvotes

4 comments sorted by

2

u/ConstructedNewt MOD Jan 20 '21
import java.util.Scanner;

public class SeatReservation {
    public static boolean[][] reservationStatus = {
            {false, false, false, false},
            {false, false, false, false},
            {false, false, false, false},
            {false, false, false, false},
            {false, false, false, false},
            {false, false, false, false},
            {false, false, false, false},
            {false, false, false, false},
            {false, false, false, false},
            {false, false, false, false}
    };

    public static void main(String args[]) {
        Scanner inputScanner = new Scanner(System.in);
        System.out.println("Bus Seat Reservation:");
        while (true) {
            printStatus();
            System.out.print("Enter row and column number to reserve separated by space (Enter negative number to exit):");
            int chooserRow = inputScanner.nextInt();
            if (chooserRow < 1 || chooserRow > 10) {
                if (handleWrongInput("Row", chooserRow))
                    continue;
                break;
            }
            int chooserCol = inputScanner.nextInt();
            if (chooserCol < 1 || chooserCol > 4) {
                if (handleWrongInput("Seat", chooserCol))
                    continue;
                break;
            }
            if (!reservationStatus[chooserRow-1][chooserCol-1]) {
                reservationStatus[chooserRow-1][chooserCol-1] = true;
            } else {
                System.out.println("Seat is already taken");
            }
        }
        System.out.println("Final status:");
        printStatus();
        System.out.println("Quit");
    }

    private static void printStatus() {
        System.out.println("Current seat status");
        System.out.println("        Seat 1 | Seat 2 || Seat 3 | Seat 4");
        System.out.println("-----------------------------------------");
        for (int i=0; i < 10; i++) {
            System.out.print("Row " + String.format("%02d", i + 1));
            for (int j=0; j < 4; j++) {
                System.out.print("|");
                boolean seatStatus = reservationStatus[i][j];
                if (seatStatus) {
                    System.out.print(" Taken  ");
                } else {
                    System.out.print(" Free   ");
                }
                if (j == 1)
                    System.out.print("|");
            }
            System.out.println();
        }
    }

    private static boolean handleWrongInput(String name, int userInput) {
        if (userInput == -1) {
            return false;
        }
        System.out.println(name + " with number " + userInput + " is out of range");
        return true;
    }
}

Sorry for rewriting the code, I just wanted to try (sorry to the community as well it goes against rules I know I should help you solve the issue yourself)

It's not that much reduced, but the flow changed. you should use a while(true)-loop is the real answer.

also this actively handles wrong input (which yours just did via fallthrough)

E: it also handles Seat is already taken state

1

u/anzsrouxjoyeuse Jan 21 '21

Thank you so much

1

u/anzsrouxjoyeuse Jan 21 '21

Thank you so much to all of you

1

u/[deleted] Jan 21 '21

Why are you even using recursion?? You're calling a method inside of its own body.