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!
-
1Do get from map and call toString() assuming toString() is overridden with whatever you want to print.kosa– kosa2012年12月21日 04:04:41 +00:00Commented Dec 21, 2012 at 4: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– ApproachingDarknessFish2012年12月21日 04:06:53 +00:00Commented Dec 21, 2012 at 4: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– Rais Alam2012年12月21日 04:13:18 +00:00Commented Dec 21, 2012 at 4:13
4 Answers 4
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();
}
2 Comments
Flight flight = schedule.flightMap.get(decision);
Then from flight object, you can retrieve all the values
Comments
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
2 Comments
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;
}
}