Answer to Question #297448 in Java | JSP | JSF for Omar

Question #297448

A local waterfront tour guide recently purchased a computer and needs a reservation system created. The sailboat has enough seating for 10 people. There will be 5 premium seats and 5 regular seats.


Your program should contain an array of Strings to hold up to ten people. Use seats (1-5) for the premium seats and seats (6 - 10) for the regular seats. Ask the user for a full name and whether they want a premium seat or not. The program should then assign them a seat (this can be done randomly, manually assigned by the user, or any other way as long as they are assigned to the correct section). It should then display a ticket / boarding pass indicating the person's name and their seat number.


Your application should never assign a seat to a person who already has a seat. It should also tell the user once all of the tickets have been assigned and the boat is full.


Display all of the tickets to the user once you have assigned the seats.


1
Expert's answer
2022-02-13T18:12:40-0500
import java.util.Scanner;

public class Main {

    static boolean checkSeats(String[] seats, String fullName) {
        boolean hasSeat = false;
        for (String seat : seats) {
            if (fullName.equals(seat)) {
                hasSeat = true;
                break;
            }
        }
        return hasSeat;
    }

    public static void main(String[] args) {
        String[] seats = new String[10];
        int premiumIndex = 0;
        int regularIndex = 5;
        Scanner in = new Scanner(System.in);
        while (premiumIndex < 5 || regularIndex < 10) {
            System.out.println("\nEnter your full name");
            String fullName = in.nextLine();
            if (premiumIndex < 5) {
                System.out.println("Type 'P' for the premium seat");
            }
            if (regularIndex < 10) {
                System.out.println("Type 'R' for the regular seat");
            }
            String choice = in.nextLine().toUpperCase();
            if (choice.equals("P") && premiumIndex < 5) {
                if (!checkSeats(seats, fullName)) {
                    seats[premiumIndex++] = fullName;
                    System.out.println("Your ticket: [" + premiumIndex + "] " + fullName);
                }
            } else if (choice.equals("R") && regularIndex < 10) {
                if (!checkSeats(seats, fullName)) {
                    seats[regularIndex++] = fullName;
                    System.out.println("Your ticket: [" + regularIndex + "] " + fullName);
                }
            }
        }
        System.out.println("All of the tickets have been assigned and the boat is full.");
        for (int i = 0; i < seats.length; i++) {
            System.out.println("[" + (i + 1) + "] " + seats[i]);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS