//****************************************************
// 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<MAX_SEATS_PER_CLASS))
              {
              seatLayout[ctrFirstClass]=true;
     		  output.setText("  ******************************  \n");
   			  output.append( "  Kelli Wiseth No-Frills Airline  \n");
   			  output.append( "     *****Boarding pass*****      \n");
   			  output.append("                             \n");
   			  output.append("  Your seat assignment\n ");
   			  output.append("  in First Class is " + (ctrFirstClass+1)+ "\n");
   			  ctrFirstClass++;
	         
			    			 
      		  }
	        else if ((seatLayout[MAX_FIRST_CLASS_IDX]) & (!econoClassFull))
	          {
	          firstClassFull=true;
              output.setText("First class is full.\nWould you like an economy seat?");
              noButton.setEnabled(true);
              yesButton.setEnabled(true);
              }
	        } //closing brace for classChoice 1
	     
        else if (classChoice==2)
          {	
          // I wonder if this next if statement should have just been written using
          // the literal values? I had literals originally, but then changed to 
          // use these constants thinking that that approach is more 'scalable',
          // but perhaps that's just overkill for this problem???
          
           if ((!seatLayout[seatLayout.length-1]) & (ctrEconoClass+MAX_SEATS_PER_CLASS<=MAX_ECONO_CLASS_IDX))
              {
         	  seatLayout[(ctrEconoClass+5)]=true;
         	  output.setText("  ******************************  \n");
      		  output.append( "  Kelli Wiseth No-Frills Airline  \n");
      		  output.append( "     *****Boarding pass*****      \n");
      		  output.append( "                                  \n");
      		  output.append( "  Your seat assignment           \n ");
      		  output.append( "  in Economy is " + (ctrEconoClass+6) + "\n");
     		 
//debug      		  output.append("show my variables:\n");
//debug      		  output.append("seatLayout[ctrEconoClass] w/o +5 is " + (seatLayout[ctrEconoClass]) + "\n");
//debug      		  output.append("counter alone is " + ctrEconoClass + "\n");
//debug      		  output.append("counter plus 5 is " + (ctrEconoClass+5)+ "\n");
//debug      		  output.append("seat layout counter plus 5" + (seatLayout[ctrEconoClass+5]) + "\n");
      		  
      		  ctrEconoClass++;
         	   		  
         	  }
           else if (seatLayout[MAX_ECONO_CLASS_IDX])
	          {
	          econoClassFull=true;
	          output.setText(" ******************************** \n");
      		  output.append("  Economy class is full.          \n");
      		  output.append("  Will you take a seat           \n");
      		  output.append("  in first class?                 \n");
              noButton.setEnabled(true);
              yesButton.setEnabled(true);
              }
         	} // closing brace for classChoice 2
         	           
     if ((classChoice!=2) & (classChoice!=1))
         	{
         	output.setText("  ********************************  \n");
      		output.append( "  Not a valid entry.                 \n" );
      		output.append("  Enter a 1 for First Class           \n");
		    output.append("  or a 2 for Economy                  \n");
             
         	}            
     }// big if
              	
	   
	else if ( actionEvent.getSource() == yesButton ) 
	   { 
	   
	 // We should only give the user the option to get a seat in the other class
	 // if there are seats available. (So we should only end up in this section
	 // if there are seats available in the alternate seating class.)
	 
			
		if ((classChoice==1) & (!econoClassFull))
			{
		    classChoice=2;
		    seatLayout[(ctrEconoClass+5)]=true;
            output.setText("  ******************************  \n");
      		output.append( "  Kelli Wiseth No-Frills Airline  \n");
      		output.append( "      *****Boarding pass*****     \n");
      		output.append( "                                   \n");
      		output.append( "  Your seat assignment             \n");
      		output.append( "  in Economy is " + (ctrEconoClass+6) + "\n");
			ctrEconoClass++;
         	 
			}

		else if ((classChoice==2) &  (!firstClassFull))
			{
				
			classChoice=1;
	        seatLayout[ctrFirstClass]=true;
     		output.setText("  ******************************  \n");
   			output.append( "  Kelli Wiseth No-Frills Airline  \n");
   			output.append( "     *****Boarding pass*****      \n");
   			output.append("                                   \n");
   			output.append("  Your seat assignment             \n ");
   			output.append("  in First Class is " + (ctrFirstClass+1) + "\n");
      		ctrFirstClass++;
			}
	    } 

	else if ( actionEvent.getSource() == noButton ) 
	   { 
	   // User rejects alternate seating class.
	    
	   output.setText( "Next flight leaves in 3 hours.              \n" );
	
	    }
    
   } // end method actionPerformed


} // end class Airline

