1

I have code that looks like this:

public Flight{
String code;
char status;
char type;

   Flight(String code, char status, char type){
    this.code = code;
    this.status = status;
    this.type = type;
   }
 }


public Menu{
 Flight flight1 = new Flight("DL123",'A','D');
 Flight flight2 = new Flight("DL146",'A','I');
 flightMap.put("DL123", flight1)
 flightMap.put("DL146", flight2)
 }


  if(schedule.flightMap.containsKey(decision))
  {

  }

If the user enters DL123 and containsKey returns true, I want to return only flight1's object attributes. How would I be able to do this? I've tried overwriting toString but because toString can only return as a String, I don't know how I'd return the status and type attributes which are characters.

Please ask if you need more information!

Brian
  • 115
  • 2
  • 3
  • 15
  • 1
    Do get from map and call toString() assuming toString() is overridden with whatever you want to print. – kosa Dec 21 '12 at 04:04
  • If you want all of the attributes to be returned from a single function, they will need to be wrapped inside a single object as functions can only return one thing... so you'd just be returning the whole object anyway. The fields (or attributes) of an object do not exist independent of that object. – ApproachingDarknessFish Dec 21 '12 at 04:06
  • Please explain your question in detail. You are adding flight objects in map with string key say "DL123" when you call flightMap.get("DL123") it will automatically return the object associated with that key. Are you looking to return only one property or flight object. – Rais Alam Dec 21 '12 at 04:13

4 Answers4

3

Define getter methods in Flight class and then:

 if(schedule.flightMap.containsKey(decision)){
   Fligth matchingFlight = schedule.flightMap.get(decision);
   String code = matchingFlight.getCode();
   char status = matchingFlight.getStatus();
   char type = matchingFlight.getType();
 }
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
1

Flight flight = schedule.flightMap.get(decision);

Then from flight object, you can retrieve all the values

Renjith
  • 3,274
  • 19
  • 39
1

what you need is

Flight flight = schedule.flightMap.get(decision);

with these you can simply access the object since their visibility is default like this

flight.code
flight.status

but the more ethical way is to define getters and setters of all the variables like this

public void setCode(String code)
{
     this.code = code;
}
public String getCode()
{
    return this.code;
}

this way you can get the variables using this

String code = flight.getCode();

Also Refer

Why use getters and setters in java

Community
  • 1
  • 1
Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40
  • I thought Java automatically assumes those fields are private due to OO principles. – Brian Dec 21 '12 at 04:26
  • 1
    @Brian : Nope , they have default visibility. for more refer this http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – Bhavik Shah Dec 21 '12 at 04:31
0

I tried to solve your question and come to conclusion. See below code.

package com.rais;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Rais.Alam
 * @project Utils
 * @date Dec 21, 2012
 */


public class FlightClient
{
/**
 * @param args
 */
public static void main(String[] args)
{
    Map<String,Flight> flightMaps = new HashMap<String, Flight>();
    Flight flight1 = new Flight("DL123", "STATUS-1", "TYPE-1");
    Flight flight2 = new Flight("DL124", "STATUS-2", "TYPE-2");
    Flight flight3 = new Flight("DL125", "STATUS-3", "TYPE-3");

    flightMaps.put("DL123", flight1);
    flightMaps.put("DL124", flight2);
    flightMaps.put("DL125", flight3);

    System.out.println(getValue(flightMaps, "DL123"));


}


public static String getValue(Map<String,Flight> flightMaps, String key)    
{
    if(flightMaps !=null && flightMaps.containsKey(key))
    {
        return flightMaps.get(key).status;
    }
    else
    {
        throw new RuntimeException("Flight does not exists");
    }


    }
}

    class Flight
    {
    String code;
    String status;
    String type;
    /**
     * @param code
     * @param status
     * @param type
     */
    public Flight(String code, String status, String type)
    {
        super();
        this.code = code;
        this.status = status;
        this.type = type;
    }



}
Rais Alam
  • 6,970
  • 12
  • 53
  • 84