Help with a GUI and some errors

Airplane Reservation system

Page 1 of 1

2 Replies - 1353 Views - Last Post: 30 November 2010 - 09:09 PM Rate Topic: -----

#1 hbomb1013  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 11-July 10

Help with a GUI and some errors

Posted 30 November 2010 - 08:57 PM

Okay, so this is a homework assignment that I am getting a second chance to fix. But I am having troubles with it. I am working on the standard airplane reservation system that is homework for every CS student at one time or another. I could use someone to point me in the right direction for getting this changed over from console to GUI. Below is the code I turned in the first time.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.*;
import java.util.Scanner;


public class ASP {
	static Scanner input = new Scanner(System.in);

	
	public static void main(String[] args) throws IOException
    {

		
		char[][] seatPlan = new char[13][6];
		char resp;

    initiailzeSeatPlan(seatPlan);

    showMenu(seatPlan);

    System.out.println("To reserve a seat enter Y/y(Yes), N/n(No): ");
    resp = input.next().charAt(0);
    System.out.println();

    while(resp == 'y' || resp == 'Y')
    {
          assignSeat(seatPlan);
          showMenu(seatPlan);
          System.out.println("Reserve another seat Y/y(Yes), N/n(No): ");
          resp = input.next().charAt(0);
          System.out.println();
    }
}

public static void initiailzeSeatPlan(char[][] sPlan)
{
    int i, j;

    for(i = 0; i < sPlan.length; i++)
        for(j = 0; j < sPlan[0].length; j++)
            sPlan[i][j] = '*';
}

public static void showSeatAssignments(char[][] sPlan)
{
    int i, j;

    System.out.println("        A B C  D E F");

    for(i = 0; i < sPlan.length; i++)
    {
      if( i < 9)
            System.out.print("Row " + (i+1) + "   ");
      else
          System.out.print("Row " + (i+1) + "  ");

          for(j = 0; j < sPlan[0].length; j++)
          {
                System.out.print(sPlan[i][j] + " ");
                if(j == 2)
                    System.out.print(" ");
          }
          System.out.println();
    }

    System.out.println("* -- available seat");
    System.out.println("X -- occupied seat");
    System.out.println();
}

public static void assignSeat(char[][] sPlan) throws IOException
{
    char ticketType;
    char resp;

    System.out.print("Enter ticket type: F/f (First class) "
           + " B/b (Business class) or E/e (Economy class): ");
    ticketType = input.next().charAt(0);
    System.out.println();

    switch(ticketType)
    {
    case 'f':
    case 'F': if(!isFirstClassFull(sPlan))
                          assignFirstClassSeat(sPlan);
                    else
                    {
                          System.out.println("First Class is Full");
                          System.out.print("Press Y/y to continue: ");
                          resp = input.next().charAt(0);
                          System.out.println();
                    }

                    break;
    case 'b':
    case 'B': if(!isBusinessClassFull(sPlan))
    					assignBusinessClassSeat(sPlan);
    				else
    				{
    					System.out.println("Business Class is Full.");
    					System.out.print("Prss Y/y to continue: ");
    					resp = input.next().charAt(0);
    					System.out.println();
    				}
    				break;
    case 'e':
    case 'E': if(!isEconomyFull(sPlan))
                        assignEconomySeat(sPlan);
                    else
                  {
                          System.out.println("Economy Class is Full");
                          System.out.print("Press Y/y to continue: ");
                          resp = input.next().charAt(0);
                          System.out.println();
                    }
    }

    showSeatAssignments(sPlan);
}

public static void showMenu(char[][] sPlan)
{
    System.out.println("This program assigns seats for a commercial airplane.");
    System.out.println("The current seat assignments is as follows.");
    showSeatAssignments(sPlan);
    System.out.println("Rows 1 and 2 are for First Class passengers");
    System.out.println("Rows 3 through 7 are for Business Class passengers.");
}

public static boolean isFirstClassFull(char[][] sPlan)
{
    int i, j;

    int assignedSeats = 0;

    for(i = 0; i < 2; i++)
        for(j = 0; j < sPlan[0].length; j++)
              if(sPlan[i][j] == 'X')
                  assignedSeats++;

    return (assignedSeats == 2 * sPlan[0].length);
}

public static boolean isBusinessClassFull(char[][] sPlan)
{
    int i, j;

    int assignedSeats = 0;

    for(i = 2; i < 7; i++)
          for(j = 0; j < sPlan[0].length; j++)
                if(sPlan[i][j] == 'X')
                      assignedSeats++;

  return (assignedSeats == 5 * sPlan[0].length);
}

public static boolean isEconomyFull(char[][] sPlan)
{
    int i, j;

    int assignedSeats = 0;

  for(i = 7; i < 13; i++)
          for(j = 0; j < sPlan[0].length; j++)
                if(sPlan[i][j] == 'X')
                      assignedSeats++;

    return (assignedSeats == 6 * sPlan[0].length);
}

public static void selectSeatNumber(int startRows, int endRows,
                          int rowNo, char columnNo)  throws IOException
{

    System.out.print("Enter Row number " + startRows + " - "
                 + endRows + ": ");
    rowNo = input.nextInt();
    System.out.println();

    while(rowNo < startRows || rowNo > endRows)
    {
          System.out.print("Enter Row number " + startRows + " - "
                      + endRows + ": ");
          rowNo = input.nextInt();
          System.out.println();
    }

    System.out.print("Enter seat number (A - F): ");
    columnNo = input.next().charAt(0);
    System.out.println();

    while(columnNo < 'A' || columnNo > 'F')
    {
          System.out.print("Enter seat number (A - F): ");
          columnNo = input.next().charAt(0);
          System.out.println();
    }
}

public static void assignFirstClassSeat(char[][] sPlan) throws IOException
{
    if(!isFirstClassFull(sPlan))
    {
        int rowNum = 0;
		char columnPos = 0;
		
		selectSeatNumber(1, 2, rowNum, columnPos);

          if(sPlan[rowNum][columnPos] != '*')
          {
                System.out.println("*#*#*#*# This seat is occupied *#*#*#*#");
                System.out.println("Make another selection");

                showSeatAssignments(sPlan);

                selectSeatNumber(1, 2, rowNum, columnPos);
          }
          
          sPlan[rowNum][columnPos] = 'X';
          System.out.println("This seat is reserved for you");
    }
    else
          System.out.println("First Class is Full");
}

public static void assignBusinessClassSeat(char[][] sPlan) throws IOException
{
      
      if(!isBusinessClassFull(sPlan))
      {
            int rowNum = 0;
			char columnPos = 0;
			
			selectSeatNumber(3, 7, rowNum, columnPos);

            while(sPlan[rowNum][columnPos] != '*')
            {
                  System.out.println("*#*#*#*# This seat is occupied *#*#*#*#");
                  System.out.println("Make another selection");

                  showSeatAssignments(sPlan);

                  selectSeatNumber(3, 7, rowNum, columnPos);
            }

            sPlan[rowNum][columnPos] = 'X';
            System.out.println("This seat is reserved for you");
      }
      else
            System.out.println("Business Class is full");
}
public static void assignEconomySeat(char[][] sPlan) throws IOException
{
      

      if(!isEconomyFull(sPlan))
      {
            int rowNum = 0;
			char columnPos = 0;
			selectSeatNumber(8, 13, rowNum, columnPos);

            while(sPlan[rowNum][columnPos] != '*')
            {
                  System.out.println("*#*#*#*# This seat is occupied *#*#*#*#");
                  System.out.println("Make another selection");

                  showSeatAssignments(sPlan);

                  selectSeatNumber(8, 13, rowNum, columnPos);
            }

            sPlan[rowNum][columnPos] = 'X';
            System.out.println("This seat is reserved for you");
      }
      else
            System.out.println("Economy Class is full");
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with a GUI and some errors

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with a GUI and some errors

Posted 30 November 2010 - 09:05 PM

What did you try so far ? Sure that the Scanner will have to go.
A few JComboBox can do the job for the row, the seat the type...

Try to keep the GUI and the business rules as separated as possible, that is the trick and a good OO design
Was This Post Helpful? 0
  • +
  • -

#3 cdog5000  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 79
  • Joined: 31-October 09

Re: Help with a GUI and some errors

Posted 30 November 2010 - 09:09 PM

View Posthbomb1013, on 30 November 2010 - 07:57 PM, said:

Okay, so this is a homework assignment that I am getting a second chance to fix. But I am having troubles with it. I am working on the standard airplane reservation system that is homework for every CS student at one time or another. I could use someone to point me in the right direction for getting this changed over from console to GUI. Below is the code I turned in the first time.

DreamInCode.net has some awesome GUI Tutorials you should check out:
http://www.dreaminco...c-gui-concepts/
Just searc the forums for any good ones.

This post has been edited by cdog5000: 30 November 2010 - 09:10 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1