import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Plane extends JApplet
implements ActionListener {
JTextField input;
JLabel prompt1, prompt2;
JButton yesButton, noButton;
int section, seats[], smoking;
int nonsmoking, people;
public void init()
{
input = new JTextField( 4 );
input.addActionListener( this );
prompt1 = new JLabel( "Please type 1 for smoking" );
prompt2 = new JLabel( "Please type 2 for nonsmoking" );
yesButton = new JButton( "Yes" );
noButton = new JButton( "No" );
yesButton.addActionListener( this );
noButton.addActionListener( this );
// The enabled method has not been introduced up
// to this point in the book. It is used here to
// disable the buttons by "graying" them.
yesButton.setEnabled( false );
noButton.setEnabled( false );
seats = new int[ 11 ];
smoking = 6;
nonsmoking = 1;
Container c = getContentPane();
c.setLayout( new FlowLayout() );
c.add( prompt1 );
c.add( prompt2 );
c.add( input );
c.add( yesButton );
c.add( noButton );
}
public void actionPerformed( ActionEvent e )
{
if ( e.getSource() == input && people <= 10 ) {
section = Integer.parseInt( input.getText() );
String s = "";
if ( section == 1 ) {
if ( smoking <= 10 && seats[ smoking ] == 0 ) {
s = "Smoking. Seat #" + smoking;
seats[ smoking++ ] = 1;
people++;
}
else if ( smoking > 10 && nonsmoking <= 5 ) {
// enable buttons
yesButton.setEnabled( true );
noButton.setEnabled( true );
s = "Smoking is full. Non-smoking?";
}
else
s = "Next flight leaves in 3 hours.";
}
else if ( section == 2 ) {
if ( seats[ nonsmoking ] == 0 && nonsmoking <= 5 ) {
s = "Nonsmoking. Seat #" + nonsmoking;
seats[ nonsmoking++ ] = 1;
people++;
}
else if ( nonsmoking > 5 && smoking <= 10 ) {
// enable buttons
yesButton.setEnabled( true );
noButton.setEnabled( true );
s = "Nonsmoking is full. Smoking?";
}
else
s = "Next flight leaves in 3 hours.";
}
else
s = "Invalid input.";
showStatus( s );
}
else if ( e.getSource() == yesButton ) {
if ( section == 1 ) {
showStatus( "Your seat assignment is " + nonsmoking );
seats[ nonsmoking++ ] = 1;
}
else { // section is 2
showStatus( "Your seat assignment is " + smoking );
seats[ smoking++ ] = 1;
}
++people;
noButton.setEnabled( false );
yesButton.setEnabled( false );
}
else if ( e.getSource() == noButton ) {
showStatus( "Next flight leaves in 3 hours." );
noButton.setEnabled( false );
yesButton.setEnabled( false );
}
}
}
Jimmyteoh -1 Newbie Poster
abelLazm 183 Postaholic
drkybelk 0 Junior Poster in Training
abelLazm 183 Postaholic
Jimmyteoh -1 Newbie Poster
Jimmyteoh -1 Newbie Poster
TrustyTony 888 pyMod Team Colleague Featured Poster
Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster
jonsca commented: That's what I'm talkin' about! Bring those perps to justice. +14
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.