Results 1 to 6 of 6
  1. #1
    RiterHeng is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Airplane Reservation

    Okay so my assignment is this:

    Write a Java Applet as a small airline reservation system. The system has only one airplane with 10 seats: 1 to 5 for first class and 6 to 10 for economy. The applet has two buttons and a text field for boarding pass. If the user clicked the First Class button, a currently availabe first class seat is printed in the boarding pass. If the user clicked the Economy button, a currently available economy seat is printed in the boarding pass. If the user selected section has no available seat, your program should ask if the user is OK with the other section seats. If the user clicked OK, you print the boarding pass of the assigned seat. If the user clicked Cancel, print "Next flight leaves in 3 hours"

    Java Code:
    	import java.awt.*;
    	import java.awt.event.*;
    	import javax.swing.*;
    	public class Assign7 extends JApplet implements ActionListener
    	{
    	 
    	 JButton firstClassButton, economyClassButton;
    	 JTextField displayField;
    	 
    	 boolean[] seat;
    	 
    	 int i;
    	 
    	 public void init()
    	 {
    	 
    	 Container container = getContentPane();
    	 container.setLayout(new FlowLayout());
    	 
    	 firstClassButton = new JButton("First Class");
    	 firstClassButton.addActionListener(this);
    	 container.add(firstClassButton);
    	 
    	 economyClassButton = new JButton("Economy");
    	 economyClassButton.addActionListener(this);
    	 container.add(economyClassButton);
    	 
    	 displayField = new JTextField(20);
    	 displayField.setEditable(false);
    	 container.add(displayField);
    	 
    	 seat = new boolean[11];
    	 
    	 }
    	 
    	 public void actionPerformed(ActionEvent actionEvent)
    	 {
    	 
    	 if( actionEvent.getSource() == firstClassButton )
    	 {
    	 reserve(true);
    	 displayField.setText("Seat " + i + " has been reserved");
    	 }
    	 else if( actionEvent.getSource() == economyClassButton )
    	 {
    	 reserve(false);
    	 displayField.setText("Seat " + i + " has been reserved");
    	 }
    	 }
    	 
    	 int reserve(boolean firstClass)
    	 {
    	 if(firstClass = true)
    	 {
    	 for(i = 1; i <= 5; i++)
    	 {
    	 seat[i] = true;
    	 }
    	 }
    	 else if(!firstClass)
    	 {
    	 for(i = 6; i <= 10; i++)
    	 {
    	 seat[i] = true;
    	 }
    	 }
    	 return i;
    	 }
    	}
    I seem to have messed up the logic in my head and with what I have right now when running the program, when I click first or economy button, I get "Seat 6 has been reserved".

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Airplane Reservation

    First, format that code so it has indentation so it is easier to read.

    Second, look at your for loops. You need to understand what they are doing because they are not doing what you want them to do. When you press either button, you want only ONE reservation right? And you want to be the next unreserved seat right?

    Third, firstClass is a boolean, therefore you don't need to use:
    Java Code:
    if (firstClass)  {
    }
    else if (!firstClass)  {
    }
    That's just redundant and a distraction. It's better just to use:
    Java Code:
    if (firstClass)  {
    }
    else  {
    }
    And lastly, you really should not be reusing/sharing the i variable like that. When you use a variable as a loop index, you should never reuse it outside the loop like that. You set a different variable to the index of interest, and in this case, should be returning that from the method.

  3. #3
    RiterHeng is offline Member
    Join Date
    Feb 2012
    Posts
    5
    Rep Power
    0

    Default Re: Airplane Reservation

    Yes, I want 1 reserved when I click either button. How would I code that?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Airplane Reservation

    Java Code:
    	 if(firstClass = true)
    	 {
    Though jlczuk is correct that the above should be just if (firstClass), the above is wrong because it will always be true.
    That is an assignment '=', not a boolean '==', so firstClass is always true after this line.

    That aside, try and do this one at a time.
    Have you managed to get working code where there is just the one class of seat?
    That's where I'd start.
    Then add in the concept of two classes.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Airplane Reservation

    Not an Eclipse question. Moving to New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Airplane Reservation

    Quote Originally Posted by Tolls View Post
    Java Code:
    	 if(firstClass = true)
    	 {
    Though jlczuk is correct that the above should be just if (firstClass), the above is wrong because it will always be true.
    That is an assignment '=', not a boolean '==', so firstClass is always true after this line.

    That aside, try and do this one at a time.
    Have you managed to get working code where there is just the one class of seat?
    That's where I'd start.
    Then add in the concept of two classes.
    That's what I get when I try to do a few quick replies before going to bed! I must have been seeing double! ;-)

Similar Threads

  1. Replies: 3
    Last Post: 12-18-2011, 12:11 PM
  2. Hotel Reservation in JAVA (help)
    By BattalGazi in forum New To Java
    Replies: 4
    Last Post: 07-31-2011, 08:32 PM
  3. java reservation class plane seat and reservation classes
    By cool_guy364 in forum Advanced Java
    Replies: 2
    Last Post: 10-15-2009, 09:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •