Solved

Applet is missing conditions

Posted on 1997-06-28
1
281 Views
Last Modified: 2012-06-27
I am trying to create an airline reservation system applet  for a single airplane airline that seats 10.  As shown in my code below, the plane has seats 1-5 designated as smoking and 6-10 as non-smoking.  My problem is I can't figure out where or how to add a method(?) that would ask a person if he/she wants to move to another section if the section (i.e. , smoking) he/she wants to seat in is full.  The passenger needs to be able to choose between two options, one is YES which would assign him/her a seat in the other section and would print out his/her "boarding pass" and NO which would then output a message "Next flight leaves in 3 hours."  

//Airline.java
  import java.awt.*;
  import java.applet.Applet;

  public class Airline extends Applet {
     int seats[];
     int type;
     String smoker, nsmoker;
     Label prompt1;
     TextField input1;
     Label prompt2;
     TextField input2;
     Button button1, button2;

     public void init()
     {
        seats = new int [10];
       
        for (int i = 0; i <= 10; i++ );
        seats[i] = i

        prompt1 = new Label ( "Please enter seat type" );
        input1 = new TextField (5);
        prompt2 = new Label ( "Your seating info:" );
        input2 = new TextField (25);
        button1 = new Button( "Yes" );
        button2 = new Button( "No" );
        add(prompt1);
        add(input1);
        add(prompt2);
        add(input2);
        add(button1);
        add(button2);
     }
     
     public void paint( Graphics g )
     {  
        g.drawString( "Welcome to Alpha Airlines", 40, 105 );
        g.drawString( "Airline Reservation System", 40, 120 );
        g.drawString( "Please type 1 for smoking", 40, 145 );
        g.drawString( "Please type 2 for nonsmoking", 40, 160 );
     }
     
     public boolean action( Event e, Object o)
     {
        if ( e.target == input1 ) {
           type = Integer.parseInt(input1.getText() );
       
               if ( type == 1 )
                  input2.setText( "seat #" + nsmoker + "- non-smoking" );
                  nsmoker = findSeat1(Integer.parseInt (type) );    
               else if ( type == 2 )
                  input2.setText( "seat #" + smoker + "- smoking" );
                  smoker = findSeat2(Integer.parseInt (type) );
        }
        return true;
     }            
     
     public int findSeat1( int x )
     {
         for ( int x = 1; x <=5; x++ )
             if ( seats[x] <= 5 )
                return x;
             else
                input2.setText( "All non-smoking seats taken.  Move to smoking?");            
     }

     public int findSeat2( int y )
     {
         for ( int y = 6; y <=10; y++ )
             if ( seats[y] <= 10 )
                return y;
             else
                input2.setText( "All smoking seats taken.  Move to non-smoking?");    
     }      
  }
     
   
   

0
Comment
Question by:banderas
1 Comment
 
LVL 2

Accepted Solution

by:
mgk earned 100 total points
ID: 1222402
I would probably just write some sort of Dialog into your code to get a boolean response. This is just a simple example but you'll need to write some extra code to handle the events. There's a good example of this yes/no type Dialog in David Flanagan's "Java in a Nutshell" book from O'Reilly. Basically if the user clicks "YES" you return true and if they click "NO" you return false.


public int findSeat1( int x ) {
  for ( int x = 1; x <=5; x++ ) {
    if ( seats[x] <= 5 ) return x;
  }
  else {
    input2.setText( "All non-smoking seats taken.");
    if (askDialog("Do you want a smoking seat")==true) {
      findSeat2();
    }
  }
}

public boolean askDialog(String str){
  Dialog d=new Dialog();
  d.setLayout(new BorderLayout());
  d.add("Center",new Label(str);
  Button yes=new Button("YES");
  Button no=new Button("NO");
  Panel p=new Panel();
  p.add("West"),no);
  p.add("East",yes);
  d.add("South",p);
  d.pack();
  d.show();
}
 
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question