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!

asked Dec 21, 2012 at 4:02
3
  • 1
    Do get from map and call toString() assuming toString() is overridden with whatever you want to print. Commented 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. Commented 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. Commented Dec 21, 2012 at 4:13

4 Answers 4

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();
 }
answered Dec 21, 2012 at 4:09
Sign up to request clarification or add additional context in comments.

2 Comments

@Brian You mean, this is helpful?
@Brian then accept the answer. people will consider answering other questions.
1

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

Then from flight object, you can retrieve all the values

answered Dec 21, 2012 at 4:10

Comments

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

answered Dec 21, 2012 at 4:15

2 Comments

I thought Java automatically assumes those fields are private due to OO principles.
@Brian : Nope , they have default visibility. for more refer this docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
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;
 }
}
answered Dec 21, 2012 at 4:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.