Results 1 to 4 of 4
- 12-17-2011, 03:50 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 2
- Rep Power
- 0
flight reservation system...need help with the booking section. thanks
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.Arrays;
public class flight {
Scanner input = new Scanner(System.in);
static flight plane = new flight();
public static int seats_left = 5;
final int total_seats = 5;
public static int pos = 5;
public void initialise()
{
boolean []seats = new boolean [10];
Arrays.fill(seats , false);
menu(seats);
}
public void menu(boolean []seats)
{
String entry = JOptionPane.showInputDialog(" 1->Business\t 2->Economy");
int choice = Integer.parseInt(entry);
switch(choice)
{
case 1:
{
plane.allot_seats_business(seats,total_seats);
break;
}
[problem with this part] case 2:
{
// here even the position is getting updated in the function itself
// but when the menu function is called position retains its value 5
// how can I update it?, same goes for the seats_left variable.
int seat_check = plane.allot_seats_economy(seats,total_seats);// collects the seats_available
if(seat_check <= 0 )// this part is not getting displayed
{
String option = JOptionPane.showInputDialog(null, "Seats are full\nSwitch to Business Class?\n 0- Yes 1- No\n");
int select = Integer.parseInt(option);
if(select == 0)
{
menu(seats);
}
}
else
{
String message = String.format("Seat(s) booked Successfully");
JOptionPane.showMessageDialog(null, message);
// menu(seats);
}
break;
}
[till here] default:
JOptionPane.showMessageDialog(null, "\t Invalid Entry");
break;
}
menu(seats);
}
// when the allot_seats_economy function returns seats_left they are not getting updated the next_time the function is called,
// also no message in the if else's statements is getting printed , I can't fathom why?
public int allot_seats_economy(boolean []a,int total_seats)// improve this part, check the seat availability before the seat is actually alloted
{
String message = String.format( "Seats Available" + " " + flight.seats_left);
JOptionPane.showMessageDialog(null, message);
JOptionPane.showInputDialog("Enter number of seats ");
int choice = input.nextInt();
// this part is not getting executed
if(choice <= flight.seats_left )// if the choice is valid , the while loop marks the given seat positions as true
{
while( pos < pos+choice)
{
a[flight.pos] = true;
flight.pos++;// position is incremented
}
flight.seats_left = total_seats - choice;
}
else // if not , then again the choice of the customer is compared with the total_seats available , if the given condition becomes
// false the following message gets printed
{
if(choice > total_seats)
{
String show = String.format("Requested seats greater than total seats available");
JOptionPane.showMessageDialog(null, show);
}
}
return flight.seats_left;// returns the no of seats_left in the economy class
}
public void allot_seats_business(boolean []a,int total_seats)// improve this part, check the seat availability before the seat is actually alloted
{
JOptionPane.showInputDialog("Enter number of seats ");
int choice = input.nextInt();
int seats_left = total_seats - choice;
while(choice < 6 && seats_left >= 0)
{
for(int pos = 5; pos < pos+choice ; pos++)
{
a[pos] = true;
}
}
if(seats_left == 0 )
{
JOptionPane.showMessageDialog(null, "Seats are full\nNext flight leaves in 3 hours\n");
}
else
{
JOptionPane.showMessageDialog(null, "Seat(s) booked Successfully");
}
}
public static void main(String args[])
{
plane.initialise();
}
}[/QUOTE]Last edited by codename47; 12-18-2011 at 09:54 AM.
-
Re: flight reservation system...need help with the booking section. thanks
We're not psychic here!! What specific problem(s) are you having with it?
Also, edit your post and insert CODE tags around your code.
- 12-18-2011, 09:49 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 2
- Rep Power
- 0
Re: flight reservation system...need help with the booking section. thanks
see the thing is , whenever I enter the option for economy class, the initial number of seats in the economy class gets displayed (here 5). after that it asks me to enter the no
of seats I require. as soon as I enter the no of seats and press ok in the dialog box, the program terminates. and no such message as "seats reserved successfully " is displayed.
- 12-18-2011, 12:11 PM #4
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
Re: flight reservation system...need help with the booking section. thanks
You seem to got confused with your code yourself. The problem why the program doesn't work is because you couldn't decide which input method to use. If you get your attention to allot_seats_economy method, you will see that there is an input field which asks the user how many seats they need. Your code expects the input to come from Scanner and not from the input field. The only thing I did was changed the lines to:
String s = JOptionPane.showInputDialog("Enter number of seats ");
int choice = Integer.parseInt(s);
If you are providing users with input fields, there is no reason to use Scanner.
EDIT: Just a heads up. The while loop in he same function is an infinite loop. You might want to re-think the logic behind it.Last edited by FlyNn; 12-18-2011 at 12:14 PM.
Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
Similar Threads
-
Hotel Reservation in JAVA (help)
By BattalGazi in forum New To JavaReplies: 4Last Post: 07-31-2011, 08:32 PM -
Difficulty in booking movie tickets
By Jubic in forum New To JavaReplies: 5Last Post: 07-24-2010, 06:31 AM -
Masking passwords & booking movies
By suneko in forum New To JavaReplies: 19Last Post: 07-22-2010, 04:46 AM -
Implementing an Adjacency List for a Flight Map
By javaRancher2009 in forum New To JavaReplies: 3Last Post: 12-07-2009, 03:45 PM -
java reservation class plane seat and reservation classes
By cool_guy364 in forum Advanced JavaReplies: 2Last Post: 10-15-2009, 09:56 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks