• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Help with Arrays

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program that adds passengers to a seating chart. I have a first class array and a second class array.

In my main class, option 1 to add a passenger. Right now im trying to get it working for 1 passenger and then i'll move getting it with up 2 passengers at a time for first class and up to 3 passengers at a time for economy class. Which if done in groups, it need to be on side same side and same row and give back an error msg.

Heres my problem. When im done adding the first seat it goes back to the airline class for the main menu which asks if the user wants to add more passengers. If so then it goes back to the Reservation class, but then the first and economy class array are set back to blank. I need to somehow keep the information that has already been added and build onto that array. As you see i tried using tempArray to figure it out, but can't seem to get it working.

Heres the main Airline class




Reservation Class



Display class



[ April 02, 2007: Message edited by: Jon Martin ]
[ April 02, 2007: Message edited by: Jon Martin ]
 
Marshal
Posts: 78671
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the while loop which calls up the main menu . . .
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look closely at what it is that is being zero'd out each time.

Also, would it be worthwhile to have a separate seat class and then have your reservation object hold two arrays (first and second class) of seat objects?
[ April 02, 2007: Message edited by: pete stein ]
 
Mike Brooks
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know its being set back to blank seat "-", but if that is removed then the array is not initialized.

I need it to be initialized the first time around and then be able to keep modifing the already initialized array, but it keeps getting set back to "-".

I updated it some which still doesn't work....code above in first post.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error is not hard to find. Look at everywhere you are calling your object's constructor. Each time you call a constructor the object starts afresh. That is why I wonder if you need a "seat" class that you can initialize instead.
 
Mike Brooks
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nvm.....working on addPassengers method. Thanks for help, hopefully this does it.
[ April 02, 2007: Message edited by: Jon Martin ]
 
Mike Brooks
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, with that working I came across one more problem.

If I choose display the seating chart before adding any seat, I get back null. I figured before the arrays are not initialized, but even if I add some passengers and display the seating chart I get back null. Any suggestions?

THE CODE IS UPDATED IN THE FIRST POST
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've noticed several things of concern in your code:
  • In your airline class you have two distinct objects, reserveSeat an object of Reservation Class) and seatingClass an object of SeatingChart Class which is a child of the Reservation class. When you are adding reservations, all of your interactions are with the reserveSeat object, never with the SeatingClass object. The seatingClass object can't output any useful information if no information has been stored into it.
  • I don't see the purpose that the SeatingChart class serves, and why it is a child class of Reservation when it inherits no methods and no states from the parent class.
  • Your child class (SeatingChart) contains an instance of the parent class (Reservation) object called displaySeat. In SeatingChart's (the child object's) initialization code you instantiate the displaySeat object, and then in the constructor you call its display method. I think that you assume that the displaySeat object, since it is an instance of the parent Reservation class will automatically have all of the information from the reserveSeat object (another Reservation class object contained in the main method of the Airline class), but this assumption is wrong. Each object is unique and contains its own independent non-static information.

  • I am no expert on OOP design, so I can only tell you so much, but I believe that your overall object model is broken and that you should rethink the object model design from square one.
    Possible ideas include:
  • Have a Seat class that contains a boolean "occupied" variable, a "seatNumber" String var, a "passengerName" String var and a "seatingLevel" variable (i.e., first or second class).
  • An class called seatingLevel that holds arrays of seats and methods to access them, change them, and display them, can display number of seats available,...
  • An airplane class that holds two instances of seatingLevel, one for first class and one for coach.

  • There are many possible iterations that you could use. You might even want to put this project on hold for a bit and restudy a chapter or two on object oriented design before redesigning this. Learn how the pros do it first. I recommend Just Java or Head First Java as good beginners texts, but there are many others that are worthwhile too.

    Good luck!

    Pete

    ps: have you thought about joining the Cattle Drive here? I have recently done just that and am learning quite a bit.
    [ April 04, 2007: Message edited by: pete stein ]
     
    Stop it! You're embarassing me! And you are embarrassing this tiny ad!
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic