Results 1 to 2 of 2

Thread: Java Code Help

  1. #1
    anjee1 is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Java Code Help

    I have a Java assignment which states:
    Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numberings as follows:

    1 A B C D
    2 A B C D
    3 A B C D
    4 A B C D
    5 A B C D
    6 A B C D
    7 A B C D

    The program should display the seat pattern, with an 'X' marking the seats already assigned. For example, after seats 1A, 2B, and 4C are taken, the display should look like:

    1 X B C D
    2 A X C D
    3 A B C D
    4 A B X D
    5 A B C D
    6 A B C D
    7 A B C D

    After displaying the seats available, the program prompts for the seat desired, the user types in a seat, and then the display of available seats is updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that that seat is occupied and ask for another choice.

    Everything works except that I can't get my program to exit when all fields have been inputed or the user decides to terminate the program.

    Here is my code:

    Java Code:
    package airplane.seats;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class AirplaneSeats {
    char[][] seats = new char[7][4];
    ArrayList<String> reservedSeats = new ArrayList<>();
    
    public static void main(String[] args) {
        AirplaneSeats reservation = new AirplaneSeats();
        reservation.buildSeats();
        reservation.printSeats();
        System.out.println();
        int filled = 0;
        System.out.println("Enter Seat number to assign (e.g. 1A or 1a) or a blank line to  quit:");
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            String s = scan.next();
            int row = Integer.parseInt(s.toUpperCase().substring(0, 1));
            char col = s.toUpperCase().charAt(1);
            reservation.reserveSeat(row, col);
        while((filled < 28) && (scan != null)){
            System.out.println("Thank you. Have a nice day!");
            System.exit(0);
        }
        System.out.println("Please enter next seat number:");
        }
    }
    
    
    public void buildSeats() {        
        for (int i = 0; i < 7; i++) {
                seats[i][0] = 'A';
                seats[i][1] = 'B';
                seats[i][2] = 'C';
                seats[i][3] = 'D';
        }
    }
    
    public void printSeats() {
        System.out.println("Available Seats:");
        for (int i = 0; i < seats.length; i++) {
            System.out.print((i + 1) + " ");
            for (int j = 0; j < seats[i].length; j++)
                System.out.print(seats[i][j] + " ");
                System.out.println();
        }
    }   
    
    public void reserveSeat(int row, char col) {
        String seatNo=String.valueOf(row)+col;
        if (checkAvailability(seatNo)) {
            reservedSeats.add(seatNo);
        for (int i = row - 1; i == row - 1; i++) {
            for (int j = 0; j < seats[i].length; j++) {
                if (seats[i][j] == col) {
                    seats[i][j] = 'X';
                }
            }
        }
        System.out.println("Seat " + seatNo + " is Reserved ");
        System.out.println();
        }
        else
        System.out.println("Sorry! The Seat "+seatNo+" is NOT available." +
                "Please select another seat.");
        printSeats();
    }
    
    
    public boolean checkAvailability(String seatNo) {
        boolean available = true;
        for(int i = 0; i < reservedSeats.size(); i++){
            if(reservedSeats.get(i).equalsIgnoreCase(seatNo)){
                available = false;
            }
        }
        return available;
    }
    }
    Any help would be appreciated. Thank You.
    Last edited by anjee1; 03-29-2012 at 06:05 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: Java Code Help

    Look around the FAQs section of this site and find out how to use the code tags so that your code retains its formatting.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  2. Translate Vb.net code into Java code
    By Radu in forum New To Java
    Replies: 5
    Last Post: 04-12-2011, 09:27 AM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  5. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •