Solved

Applet is missing conditions

Posted on 1997-06-28
1
255 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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now