Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: problem with choice for airline reservation

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem with choice for airline reservation

    need to 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 available 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"

    when i run my applet and click firstclass or economy all i get in the outputField is "Next flight leaves in 3 hours." it doesn't go to seats 1 through 5 for firstclass or 6 through 10 for economy.

    also as for the my 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"

    i know its
    	choice = Integer.parseInt(JOptionPane.showInputDialog("Are you OK with First Class. (click OK or Cancel"));
    	               if (choice == (not sure what i put here??)
    	 else
    	    JOptionPane.showMessageDialog("Next flight leaves in 3 hours.");
    {
    }

    here is my code
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    	public class Assign7 extends JApplet implements ActionListener
    	   {
    	   JTextField outputField;
    	   JButton firstClassButton, economyButton;
    	   int section, seats[], firstclass;
    	   int economy, people, choice;
     
    	   public void init()
    	   {
     
    	      Container container = getContentPane();
    	      container.setLayout(new FlowLayout());
     
     
    	      firstClassButton = new JButton( "First Class" );
    	      firstClassButton.addActionListener( this );
    	      container.add(firstClassButton);
     
    	      economyButton = new JButton( "Economy" );
    	      economyButton.addActionListener( this );
    	      container.add(economyButton);
     
    	      outputField = new JTextField ( 17 );
    	      outputField.setEditable(false);
    	      container.add(outputField);
     
    	      seats = new int[ 11 ];   
    	      firstclass = 1;
    	      economy = 6;       
    	   } 
    	   public void actionPerformed( ActionEvent actionEvent )
    	   {
     
    	      if ( actionEvent.getSource() == firstClassButton && people <=10)
    	        {
    	             if ( firstclass <= 5 && seats[ firstclass ] == 0 )
    	              {
    	                 outputField.setText ("Boarding Pass: Seat " + firstclass+ " FirstClass");
    	                 seats[ firstclass++ ] = 1;
    	                 people++;
    	              }
    	             else if ( firstclass > 0 && economy <= 6)
    	              {
    	            outputField.setText ("FirstClass is full. Economy?");
    	              }
    	             else
    	            outputField.setText("Next flight leaves in 3 hours.");
     
    	        }    
    	      else if (actionEvent.getSource() == economyButton && people <=10)
    	      {
     
    	              if ( economy >= 5 && seats[ economy ] == 0)
    	               {
    	                  outputField.setText ("Boarding Pass: Seat " +economy+ " Economy");
                      seats[ economy++ ] = 1;
    	                  people++;
                   }
                  else if ( economy > 0 && firstclass <= 6 )
                   {
                      outputField.setText("Economy is full. FirstClass?");
                   }
     
                  else
                      outputField.setText ("Next flight leaves in 3 hours.");
     
              }
     
    	   }
     
    	}
    {
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: problem with choice for airline reservation

    I think you are making it harder than it needs to be. For starters I see no need for the seat array. Unless you are going to provide further functionality such as seat assignment. All you really need is two variable: number of first class passengers and number of economy class passengers.
    if first class and economy equals 10 {
        flight is full
    } else if booking first class {
        if first class < 5 {
            increase first class
        } else {
            ask if economy os OK
        }
    } else if booking economy {
        if economy < 5 {
            increase econmy
        } else {
            ask if first class is OK
        }
    }
    It's not perfect and needs refining.
    if (choice == (not sure what i put here??)
    Check out the constants in the JOptionPane class.

Similar Threads

  1. Java Assignment 2011 - reservation
    By bs09it in forum Member Introductions
    Replies: 2
    Last Post: March 1st, 2011, 08:25 PM
  2. Java, A good choice?
    By BillnTexas in forum Java Theory & Questions
    Replies: 7
    Last Post: November 16th, 2010, 11:05 AM
  3. Which interface gives more control on serialization of an object?
    By abhishekraok2003 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 16th, 2009, 10:17 AM
  4. Replies: 2
    Last Post: May 16th, 2009, 05:23 AM