• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with HashMap storing 3 elements  RSS feed

 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my requirement specification:

addFlight
boolean addFlight(java.lang.String flight,
int seats,
java.lang.String description)
throws FlightAirlineExceptionAdd a new flight to the system. Each new flight is associated with an initial capacity and a description.

Parameters:
flight - Flight code (must be unique; cannot be null)
seats - The capacity of this flight (cannot be negative)
description - A brief description of this flight (may be null)
Returns:
true if flight was successfully added; false otherwise (if the specified capacity is negative, regardless of flight code)
Throws:
FlightAirlineException - if the flight code already exists in the system, or is null

--------------------------------------------------------------------------------

The test method calls the addFlight method passing three arguments to be added to the flight: flight, seats, description. I believe I am to create a solution where I can store many flights and recall by the flight code that must be unique and not null. I cannot figure out how you can store more than 2 elements in a HashMap or HashSet... Any ideas on how to create the solution?

Thanks in advance.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why cant you do as in

Flight has seatNum, name and desc...
 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.. For newbie purposes, can you be a little more specific on how I would be able to implement this structure. Here is my code so far



Tester
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's too little information to go on. First you have to define what these things are:

flights What is that? Where did you define it?
The class Flight.

These two things are the keys to the solution. Instead of storing 3 things in a Map, you store 2 (the key and the value). You have to define the value (the Flight class) and translate your inputs into that value. You then have to decide what to use as a key (which must be a unique non-null object) and store the key and value someplace (flights).

Then you have to tell us what errors you get when you run your code.

Finally, if I were you I would build a more robust test suite. For example, you know some rules as stated in your first post. In addition to some tests that should succedd you should run some tests that break those rules to make sure you handle them correctly. Then run some tests for things that are likely to get by the rules (what happens when you get the same flight two different times, or when you get the flight String "dst1" versus new String("dst1")? How do you know if your element was added to the map, or not?

Finally, I hope you don't expect us to write this for you, This is NotACodeMill, you are expected to write your own code and we can help with specific problems. You haven't yet told us what your specific problem is (other than you have a homework assignement).
 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve,

No, I don't want anyone writing code for me.. just to point me along on what it takes for the solution. I first tried to create a singleton class and store that in the Set but I was getting some type of value for it and was unable to retrieve the values back.

I'll take what you have and work on it a bit. Thanks.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron Ingram wrote:I first tried to create a singleton class and store that in the Set...


I am hoping that by singleton you don't mean the Singleton Design Pattern, but rather mean a single Object that holds the three values... If you meant the Singleton design pattern, forget you have heard of that until you know other design patterns to compare it to. For now just stick with normal Classes.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron Ingram wrote:Thank you.. For newbie purposes, can you be a little more specific on how I would be able to implement this structure.

1. Define a class called Flight that contains the attributes you need.
2. Define an equals() method for it that returns true if the object being compared with is a Flight and its code is the same as this one.
3. Define a hashCode() method that is consistent with your equals() method.
4. Define a Set<Flight> and initialize it with a HashSet.
5. For each call to addFlight(), create a Flight object and add it to the Set.

If you need any more information, you should probably consult the Java tutorials.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!