Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Plane reservation code help

 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven’t bothered to read that code thoroughly, but there is something very wrong with if else.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic