Solved

Applet is missing conditions

Posted on 1997-06-28
1
288 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to execute a Python program and gather return output in Java 2 60
how to debug htl and js pages 8 77
Java syntax, or is it Selenium 6 58
junit example issue 2 53
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

739 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