Back again with another assignment...
everytime i chose a seat is says that the seat is taken, couldn't figure out where the problem is at:
public class Plane { char[][] chart; int ROWS = 7; int COLS = 5; Plane() { chart = new char[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { chart[i][0] = 'A'; chart[i][1] = 'B'; chart[i][2] = ' '; chart[i][3] = 'C'; chart[i][4] = 'D'; } } public boolean assignSeat(String str) { boolean result = false; str = str.toUpperCase(); if (!okRow(str.charAt(0)) || !okSeat(str.charAt(1))) { System.out.println("Seat format is invalid\n"); } else { result = markSeat(str.charAt(0), str.charAt(1)); if (result) { System.out.println("The seat is taken\n"); } else { System.out.println("The seat is busy, try another one\n"); } } return result; } public String toString() { String result = ""; for (int i = 0; i < ROWS; i++) { result += Integer.toString(i + 1); result += " "; for (int j = 0; j < COLS; j++) { result += chart[i][j]; } result += "\n"; } return result; } private boolean okRow(char r) { if (r >= '1' && r <= '7') { return true; } else { return false; } } private boolean okSeat(char s) { if (s >= 'A' && s <= 'D') { return true; } else { return false; } } private boolean markSeat(char row, char seat) { int rowIndex, seatIndex; boolean result = false; rowIndex = row - '1'; seatIndex = seat - 'A'; if (seatIndex > 1) { seatIndex++; } if (chart[rowIndex][seatIndex] != 'X') { result = true; chart[rowIndex][seatIndex] = 'X'; } return result; } public boolean isPlaneFull() { for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (chart[i][j] != 'X' && chart[i][j] != ' ') { return false; } } } return true; } }
and i'm using this to test it:
import java.util.Scanner; public class PlaneTest { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input = new Scanner(System.in); String cmd; System.out.println("Welcome, the plane is currently empty"); Plane commuter1 = new Plane(); System.out.println(commuter1.toString()); System.out.println(); System.out.println("Please enter a seat location to reserve and press enter to reserve row 1 seat A enter 1A followed by the enter key"); System.out.println("To exit type Q or q"); cmd = input.nextLine(); while(!cmd.equals("Q") && !cmd.equals("q") && !commuter1.isPlaneFull()){ commuter1.assignSeat(cmd); System.out.println(); System.out.println("Please enter a seat location to reserve and press enter to reserver row 1 seat A enter 1A followed by the enter key"); System.out.println("To exit type Q or q"); cmd = input.nextLine(); } System.out.println("You are now exiting."); System.out.println(commuter1.toString()); } }