简体   繁体   English

我如何将返回对象的结果调用到Hash / TreeMap中? -Java

[英]How would I call the results of a returned object into a Hash/TreeMap? - Java

I've never used HashMap and am not quite certain how it works. 我从未使用过HashMap,也不确定它是如何工作的。 I've got a MainMenu class, and from it the user chooses a number that will direct them to a separate class/method which requests information that will return to an object. 我有一个MainMenu类,用户从中选择一个数字,该数字会将它们定向到一个单独的类/方法,该类/方法请求返回将返回对象的信息。

I need the user to enter a code that'll identify which row I've created (say the user enters QA in one request, and 400 in another...) I'll ask the user to enter QA400 and they'll see the entire line of information they entered earlier (after a bit of formatting.) 我需要用户输入代码来标识我创建的行(例如,用户在一个请求中输入了质量检查,在另一个请求中输入了400…)。我将要求用户输入QA400,他们会看到他们之前输入的所有信息(经过一点格式化之后)。

I'm just not sure how to take those variables and have them be printed out from a HashMap (or maybe I'm supposed to be using TreeMap? I've read its more organized?) 我只是不确定如何获取这些变量并将其从HashMap中打印出来(或者也许我应该使用TreeMap?我读得井井有条了吗?)

Here is a piece of my main menu, from inside the Switch Case. 这是我在Switch Case内部的一个主菜单。

                case 3:
                A8AirlineAircraftData.AddAirline(sc);
                continue;
            case 4:
                A8FlightData.AddFlight(sc);
                continue;

The user presses 3 and is then redirected to: 用户按3,然后将其重定向到:

public class A8AirlineAircraftData {

private String airName; //these are all showing a warning saying 'Field ____ can be final'
private String airCode;
private String airCraft;
private int firstClass;
private int busiClass;
private int econClass;

public A8AirlineAircraftData(String airName, String airCode, String airCraft, int firstClass, int busiClass, int econClass) {
    this.airName = airName;
    this.airCode = airCode;
    this.airCraft = airCraft;
    this.firstClass = firstClass;
    this.busiClass = busiClass;
    this.econClass = econClass;
} 

public static A8AirlineAircraftData AddAirline(Scanner sc) {
    sc.nextLine();
    System.out.println("Please enter the Airline name:");
    String airName = sc.nextLine();

    System.out.println("Please enter the Airline code:");
    String airCode = sc.nextLine();
    System.out.println("Please enter the Delta Aircraft:");
    String airCraft = sc.nextLine();
    System.out.println("Please enter the first class seat capacity:");
    int firstClass = sc.nextInt();
    System.out.println("Please enter the business class seat capacity:");
    int busiClass = sc.nextInt();
    System.out.println("Please enter the economy class seat capacity:");
    int econClass = sc.nextInt();
    System.out.println("Airline name: " + airName);
    System.out.println("Airline code: " + airCode);
    System.out.println("Delta Aircraft: " + airCraft);
    //Splitting the first word from the rest of the string
    String arr[] = airCraft.split(" ", 2);
    String firstWord = arr[0];
    System.out.println(firstWord + " first class seat capacity: " + firstClass);
    System.out.println(firstWord + " business class seat capacity: " + busiClass);
    System.out.println(firstWord + " economy class seat capacity: " + econClass);
    //Airline object
    A8AirlineAircraftData airline = new A8AirlineAircraftData(airName, airCode, airCraft, firstClass, busiClass, econClass);
    System.out.println(airName + " successfully added. Press Enter to continue.");
    sc.nextLine();
    sc.nextLine();//Press Enter to continue 
    return airline;

I'd like the end-result to look like to simply restate the "airline" object from the main menu, or anywhere... I just am not sure where to begin with creating that. 我希望最终结果看起来像只是从主菜单或任何地方重新声明“航空公司”对象……我只是不确定从哪里开始创建。

Thank you for any help. 感谢您的任何帮助。

You have to catch the returned object, ie the airline object in your case, in the main menu. 您必须在主菜单中捕获返回的对象,即您的情况下的航空公司对象。

case 3: 
    A8AirlineAircraftData airline = A8AirlineAircraftData.AddAirline(sc);
    /*do something with the airline*/
    continue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM