Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Applet is missing conditions

Posted on 1997-06-28
1
Medium Priority
?
298 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 300 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

664 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