Results 1 to 2 of 2
Thread: Java Code Help
- 03-29-2012, 05:37 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
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:
Any help would be appreciated. Thank You.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; } }Last edited by anjee1; 03-29-2012 at 06:05 AM.
- 03-29-2012, 05:56 AM #2
Similar Threads
-
I want the source code of DUK's Bank , the example code in Java EE 5 Tutorial 2010
By zahra in forum New To JavaReplies: 16Last Post: 01-31-2012, 08:36 PM -
Translate Vb.net code into Java code
By Radu in forum New To JavaReplies: 5Last Post: 04-12-2011, 09:27 AM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks