Results 1 to 6 of 6
Thread: Airplane Reservation
- 04-20-2012, 03:03 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
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"
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".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; } }
- 04-20-2012, 03:47 AM #2
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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:
That's just redundant and a distraction. It's better just to use:Java Code:if (firstClass) { } else if (!firstClass) { }
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.Java Code:if (firstClass) { } else { }
- 04-20-2012, 05:11 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Re: Airplane Reservation
Yes, I want 1 reserved when I click either button. How would I code that?
- 04-20-2012, 09:23 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Airplane Reservation
Though jlczuk is correct that the above should be just if (firstClass), the above is wrong because it will always be true.Java Code:if(firstClass = 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.
- 04-20-2012, 10:22 AM #5
Re: Airplane Reservation
Not an Eclipse question. Moving to New to Java.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-20-2012, 12:21 PM #6
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Similar Threads
-
flight reservation system...need help with the booking section. thanks
By codename47 in forum New To JavaReplies: 3Last Post: 12-18-2011, 12:11 PM -
Hotel Reservation in JAVA (help)
By BattalGazi in forum New To JavaReplies: 4Last Post: 07-31-2011, 08:32 PM -
java reservation class plane seat and reservation classes
By cool_guy364 in forum Advanced JavaReplies: 2Last Post: 10-15-2009, 09:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks