wood burning stoves 2.0*
The moose likes Java in General and the fly likes Plane reservation code help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Plane reservation code help" Watch "Plane reservation code help" New topic
Author

Plane reservation code help

George Avilez
Ranch Hand

Joined: Apr 26, 2013
Posts: 60
Sorry for the mess in my coding since I'am no where being complete but I really need help in the AssignFirstClass() method. The displaySeatArray() method will help to show my problem. The program is for the user to enter either 1 for first class or 2 for economy seats. The code needs to fill the index (which is at zero) at 1 to show that the seat is taken. The problem I'am having is that when the user enters 1, it takes TWO (filling 2 indexes with the number 1) seats instead of 1. Hopefully some one can help me out =( The AssignEconomy() method is the same thing but I can fix that once someone helps me with the other method.

Heres the class:



For the driver I used two tests to enter 1 (the first class) and it will show that there are FOUR seats taken... so they increment by two for some reason when I need only one seat taken...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Welcome to the Ranch

I have added code tags, which you should always use, and you can see how much better the code looks
Why have you got public static fields? Fields should always be private (unless they are used as constants) and should only be static if there is a good reason to be static.
Why are you using 1=true and 0=false? That might work in some languages, but Java has been designed specifically to avoid that. You can use booleans, or better still create an enumerated type with values like VACANT, BOOKED.
The book method with the 4 in looks as if you had been guessing. Write down an array of seats, rather like thisNow go through it with a pencil and work out how you are going to allocate 4 more seats, among the 5 vacant seats.
Now write down what you did, in plain English in small words. Once you have that, you can turn it into code more easily.
It is worthwhile learning the conventional forms for loops; there are good reasons for those conventional forms, one being that experience shows that they always work.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
George Avilez wrote:The AssignEconomy() method is the same thing but I can fix that once someone helps me with the other method.

Actually fixing the AssignFirst problem will fix the AssignEconomy problem as well, because the error is in the AssignSeats method.
You've got two if/else statements in there. In the first, you AssignFirstClass or you AssignEconomy. In the seccond you AssignEconomy or you AssignFirstClass. You are calling both methods twice whatever the value of choice is.

You've also got a syntax error on line 27, so this code won't actually compile at the moment.
Rafael Prado Oliveira
Greenhorn

Joined: Apr 30, 2013
Posts: 28
George Avilez wrote:Sorry for the mess in my coding since I'am no where being complete but I really need help in the AssignFirstClass() method. The displaySeatArray() method will help to show my problem. The program is for the user to enter either 1 for first class or 2 for economy seats. The code needs to fill the index (which is at zero) at 1 to show that the seat is taken. The problem I'am having is that when the user enters 1, it takes TWO (filling 2 indexes with the number 1) seats instead of 1. Hopefully some one can help me out =( The AssignEconomy() method is the same thing but I can fix that once someone helps me with the other method.

Heres the class:



For the driver I used two tests to enter 1 (the first class) and it will show that there are FOUR seats taken... so they increment by two for some reason when I need only one seat taken...








Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
I haven’t bothered to read that code thoroughly, but there is something very wrong with if else.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Plane reservation code help
 
Similar Threads
Need some help with Java program Airline.java
help with program
Array Problem....sort of
print invalid selction not working
Trouble getting passed value into array declaration