wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes building a simple airline reservation system exercise Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "building a simple airline reservation system exercise" Watch "building a simple airline reservation system exercise" New topic
Author

building a simple airline reservation system exercise

Jason Attin
Greenhorn

Joined: Feb 02, 2013
Posts: 22
HI all, I was wondering if anybody can help me with this exercise taken from the Deitel and Deitel Java how to program, chap 7 (I think I should clarify here that this is not an assignment because I am not on any course, I am just trying to learn java as hobby and having chose to use this book I have decided to do some of the exercises too):
"A small airline has just purchased a computer for its new automated reservation system. Write an application to assign seas on each flight of the airline's only plane
(capacity: 10 seats).
Your application should display the following alternatives: 'Please type 1 for First class and please type 2 for economy'. If the user types 1, your application
should assign a seat in the first class section (seats 1-5), if he types 2 in the economy section ( seats 6-10). Your application should then display
a boarding pass indicating the person's seat number and whether it's in the first-class or economy.
Use a 1 dimensional array of primitive type boolean to represent the seating chart of the plane. Initialize all the elements to false to indicate that all the
seats are empty. As each seat is assigned set the corresponding element of the array to true to indicate that the seat is no longer available. Your application
should never assign a seat that has already been assigned. When the economy section is full ask the person if it is acceptable to be placed in the first class section
and vice versa. If yes make the appropriate sear assignment. If not display the message 'next flight leaves in 3 hrs'"

I am having a problem with the last bit of the exercise, if a class is full then ask the user if he's ok to have his seat booked in 1st/2nd class...I just don't seem to be able to dfind a good way to implement that in my code, can somebody suggest what the best way this will be?

Just one thing: I am really new to java, so I know for a fact that there are millions of better ways to do this exercise, but please uderstand that my knowledge is still very basic (I only got as far as chapter 7 about arrays) so there are many many things, methods etc I am not aware of, so it would be very good if you bear in mind this when you reply. I say that becasue in my experience many people when I started programming were literally like "Oh you know it's easy do this and that" assuming I knew everything already...

In the meanwhile this is what I came up with so far



and this:



Ok a few things to say here:
1) the constructor, initializes the array - even if I believe that being a boolean array, its values are already initialized to false;
2) returns a string that I will use as a flag in the other file to determine whether the flight is full or not. This function generates a seat by calling , displays the boarding pass (well it just says which seat you are in), and using various counters, determines whether the plane is full or not. The 2 counters and are incremented based on where the seat is, first of second class. The exercise asks for number from 1 to 5 to be first class and from 6 to 10 second, so I have 'divided' the array effectively
3) generats the seats based on the class chosen by the user;
4) checks whether the seat chosen hasn't been assigned yet. You will noticed that if all the seats are assigned the while loop inside it will run forever, so to avoid that I have used the counters aforementioned to determine whether the checkSeats() should be run or not.

Right, all that said, I can't find a good way to plug in the assignment of seats in case one of the classes are already full. SHould this be in another function? I am a bit lost
thanks
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Jason Attin wrote:In the meanwhile this is what I came up with so far...

Jason,

Please DontWriteLongLines (←click), because it makes your post very hard to read.
I'd break them up myself, but there are a ton of them; so I suggest you use the 'Edit' button and correct them yourself.

Remember: 80 characters max; just like the old punched cards.

Thanks

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Jason Attin wrote:HI all, I was wondering if anybody can help me with this exercise...

Well one thing I can see straight away is that you're using Strings for flags, which is generally a big no-no. StringsAreBad (←click).

Winston
Jason Attin
Greenhorn

Joined: Feb 02, 2013
Posts: 22
ok sorry, I have amended the long lines, there were only a few, it is displaying ok now on my screen.
As for the string, not a problem I can change that and use int flag instead, so say if the returned value is 1 then that signifies that the airplane is full, etc. I will amend that, but I think there are more problems than just that...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Jason Attin wrote:ok sorry, I have amended the long lines, there were only a few, it is displaying ok now on my screen.

OK, thanks. Looks better to me now too.

As for the string, not a problem I can change that and use int flag instead, so say if the returned value is 1 then that signifies that the airplane is full, etc. I will amend that,

Why an int? You're overthinking this by a mile my son, because Java has a perfectly good type for a flag like yours, and it's called a boolean.

but I think there are more problems than just that...

I suspect you're right. I'll have a look now and come back with anything I find. However, a general piece of advice: Keep it simple.
It's when you start over-engineering that problems usually crop up.

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Jason Attin wrote:but I think there are more problems than just that...

PS: Did you actually read the page I pointed you to? Because it has an example that dovetails exactly with what you're trying to do.

[Edit] Oops. Just noticed that you've been told how to create your seat array (don't like that, but there you go). The link might still give you some food for thought though.

Winston
Jason Attin
Greenhorn

Joined: Feb 02, 2013
Posts: 22
PS: Did you actually read the page I pointed you to? Because it has an example that dovetails exactly with what you're trying to do.

Of course I did : - )! But in that exercise you had to use a 2 dimension array, whereas I have a 1 dimension, so quite a bit different. Perhaps my mistake was not to come up with any pseudo code before starting. I feel like I should scrap the whole ting and start from scratch. I mean if it is fixable then I will continue if not here's some pseudocode, what do you reckon?



cheers
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

OK, I'm going to go through your points one by one:
Jason Attin wrote:1) the constructor, initializes the array - even if I believe that being a boolean array, its values are already initialized to false;

I believe so too.

2)displays the boarding pass (well it just says which seat you are in), and using various counters, determines whether the plane is full or not.

Sounds much too procedural to me. You're writing this as if it was a C (or Basic) program, rather than a Java one (mind you, you have been somewhat hamstrung by over-specific instructions). Java is an Object-Oriented language, and that means that you should be dealing with objects.

So: a few classes for your consideration: Plane (maybe a better name for your Seats class) Booking (or BookingRequest) and BoardingPass.

The 2 counters....are incremented based on where the seat is, first of second class. The exercise asks for number from 1 to 5 to be first class and from 6 to 10 second, so I have 'divided' the array effectively

It's clever, but it's brittle (as most 'clever' code is). What if they'd said that it should be 1-4 and 5-10? You'd have written it completely differently, right? And that's because your design is founded entirely on how you want to do this, rather than what needs to be done.

3) generats the seats based on the class chosen by the user;

To my mind, that should be part of your Seats constructor.

4) checks whether the seat chosen hasn't been assigned yet. You will noticed that if all the seats are assigned the while loop inside it will run forever, so to avoid that I have used the counters aforementioned to determine whether the checkSeats() should be run or not.

You're still preoccupied with "how" here. What about a simple isFull() (or, possibly even better, hasOpenSeats()) method that returns a boolean? Then you can write:
while (hasOpenSeats()) { ...

Right, all that said, I can't find a good way to plug in the assignment of seats in case one of the classes are already full. SHould this be in another function? I am a bit lost

OK, well first you have to decide what the rules are. Once you have that, I suspect you could write logic based on your current seat assignment method.

Part of your problem is that you are trying to code your way out of trouble, which will NEVER work (at least, not in the long run). So StopCoding (←click).

HIH

Winston
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

Jason Attin wrote:
PS: Did you actually read the page I pointed you to? Because it has an example that dovetails exactly with what you're trying to do.

Of course I did : - )! But in that exercise you had to use a 2 dimension array, whereas I have a 1 dimension, so quite a bit different.

umm...Java does not have two (or higher) dimensional arrays. Java only has one-dimensional arrays. Now, an array can hold arrays (or more properly, it can hold references to arrays). So whatever the example showed, it was only using plain old arrays.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jason Attin
Greenhorn

Joined: Feb 02, 2013
Posts: 22
Hi thanks, how do I actually deal with more than one class? I thought having one was probably the easiest way. Point taken about "stop coding", happy to get everything right on paper first, which is quite a task on its own...I will go back and think of what needs to be achieved then, will post back
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Jason Attin wrote:here's some pseudocode, what do you reckon?

Well, it's still a bit preoccupied with the "how".

My advice: forget about all those display and input statements and think about the business of booking, and how it applies to your areoplane.

It's tough to begin with, but the more you can separate out the input and output from the actual process (ie, your booking system), the more flexible it's likely to be.

Ideally, you don't really want a single input or println() statement in your system classes - and you certainly don't want any references to any classes beginning with 'J'.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: building a simple airline reservation system exercise