//**************************************************** // Kelli Wiseth // kelli_wiseth@yahoo.com // CIS 255WX // Program name: Airline // Program Description: Airline is an applet that accepts // input from user (a 1 for first class, a 2 for economy) // and assigns a seat to the user and issues a boarding // pass (onscreen display only). If there are no seats // available in the class requested by the user, the user // is prompted to accept a seat on the alternate class // (assuming seats are available in that class). // Assignment 1 // 9 September 2004 [Due 10-SEPT-2004] // //**************************************************** import java.awt.*; // Container, FlowLayout import java.awt.event.*; // ActionEvent, ActionListener import javax.swing.*; // JApplet, JButton, JLabel, JTextField public class Airline extends JApplet implements ActionListener { // Constants and class-wide variables // The problem states that there are a total of 10 seats on the plane and that // five seats are in first class and five are in economy (50% each) final int SEATS = 10; // total number of seats on the plane final int MIN_FIRST_CLASS_IDX = 0; //index location for beginning range, first class final int MAX_FIRST_CLASS_IDX = 4; //index location for end range, first class seats final int MIN_ECONO_CLASS_IDX = 5; //index location for begin range, economy final int MAX_ECONO_CLASS_IDX = 9; //index location for end range, economy final int MAX_SEATS_PER_CLASS =5; JLabel firstClassLabel, econoClassLabel, appletTitle; JTextField seatClassInputField; JButton yesButton, noButton; JTextArea output; int classChoice; int ctrFirstClass=0; int ctrEconoClass=0; boolean seatLayout[]; // seating chart boolean firstClassFull=false; boolean econoClassFull=false; //**************************************************** // Setup for graphical user interface components // //**************************************************** // set up GUI components and create array for seat public void init() { seatLayout = new boolean[SEATS]; // instantiate the array // obtain content pane and set layout to FlowLayout Container container = getContentPane(); container.setLayout( new FlowLayout() ); // create labels and input field appletTitle= new JLabel("Welcome to Kelli Wiseth's Mini Airline"); container.add(appletTitle); firstClassLabel = new JLabel( "Please type 1 for First Class ", SwingConstants.LEFT ); container.add( firstClassLabel ); econoClassLabel = new JLabel( "Please type 2 for Economy", SwingConstants.LEFT ); container.add( econoClassLabel ); seatClassInputField = new JTextField( 1 ); seatClassInputField.addActionListener(this); container.add( seatClassInputField ); output = new JTextArea(10, 20); container.add(output); // enable the buttons after all seats // for a particular class are full yesButton = new JButton( "Yes" ); yesButton.addActionListener( this ); container.add( yesButton ); yesButton.setEnabled( false ); noButton = new JButton( "No" ); noButton.addActionListener( this ); container.add( noButton ); noButton.setEnabled( false ); } // end method init //**************************************************** // actionPerformed method will handle responses from user. // Must handle a 1 or 2 for seating class, and also // must handle a Yes or No for alternate seat assignment //**************************************************** public void actionPerformed( ActionEvent actionEvent ) { if ( actionEvent.getSource() == seatClassInputField ) { String input=seatClassInputField.getText(); classChoice=Integer.parseInt(input); if ((seatLayout[MAX_ECONO_CLASS_IDX]) & (seatLayout[MAX_FIRST_CLASS_IDX])) { output.setText(" ***************************** \n"); output.append(" The plane is completely full. \n"); output.append(" Next flight leaves in 3 hours. "); noButton.setEnabled(false); yesButton.setEnabled(false); } else if (classChoice==1) { if ((!seatLayout[ctrFirstClass]) & (ctrFirstClass