Solved

Applet is missing conditions

Posted on 1997-06-28
1
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

691 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