嗨,我使用字符串和航班的哈希图创建了一个名为planeStore的航班商店。因此,我也使用哈希图制作了另一家商店(航空公司商店)。我将planeStore放入AirlineStore。但是我不能让航空公司把飞机打印出来。

我以为可以通过将AirlinesName字符串放入Airlines构造函数中。并在创建Airline航空公司= new Airline(“ PlaneName”)时传递“飞机名称”;这将工作,但没有。

这是我的代码:

航空公司

import java.util.HashMap;


public class Airline
{
    private String airlineName;
    private HashMap<String, PlaneStore> map;

    public Airline(String airlineName)
    {
        this.airlineName = "";
        map = new HashMap<String, PlaneStore>();
    }
    public void add(PlaneStore plane)
    {
        map.put(airlineName, plane);
    }
    public void remove(String flight)
    {
        map.remove(flight);
    }
    public void printPlane()
    {
        System.out.println("\n********Flight List********");
        for (PlaneStore plane: map.values()) {
             //System.out.println(plane);
            // class
            // or:
            System.out.println(airlineName);
            System.out.println(plane.toString());

        }

    }

}


plane.toString是PlaneStore的toString:

public String toString() {
        return "PlaneStore [airlineName=" + airlineName + ", planeMap="
                + planeMap + "]";
    }


主程序

import java.util.Scanner;


public class MainApp
{
    private Scanner keyboard = new Scanner(System.in);
    public static void main(String[] args)
    {
        new MainApp().start();
    }

    public void start()
    {
        Airline airline1 = new Airline("AerLingus");
        Airline airline2 = new Airline("Ryan Air");
        PlaneStore planeStore = new PlaneStore("Aer Lingus");
        PlaneStore planeStore2 = new PlaneStore("Ryan Air");

        Flight p1 = new Flight("Aer Lingus","A01", 150.5, 10.5, 500, Flight.AIRPLANETYPE.AIRBUS);
        Flight p2 = new Flight("Aer Lingus","B01", 50.3, 1.5, 91, Flight.AIRPLANETYPE.CORPORATE);
        Flight p3 = new Flight("Aer Lingus","C01", 12.2, -3.1, 56, Flight.AIRPLANETYPE.AIRBUS);


        Flight p4 = new Flight("Ryan Air","D01", 10.5, 1.5, 430, Flight.AIRPLANETYPE.PRIVATE);
        Flight p5 = new Flight("Ryan Air","E01", 0.3, 2.1, 101, Flight.AIRPLANETYPE.CORPORATE);
        Flight p6 = new Flight("Ryan Air","F01", 2.2, -3, 291, Flight.AIRPLANETYPE.AIRBUS);
        planeStore.add(p1);
        planeStore.add(p2);
        planeStore.add(p3);
        planeStore.print();

        airline1.add(planeStore);
        airline1.add(planeStore);
        airline1.add(planeStore);
        airline1.printPlane();

        planeStore2.add(p4);
        planeStore2.add(p5);
        planeStore2.add(p6);

        airline2.add(planeStore2);
        airline2.add(planeStore2);
        airline2.add(planeStore2);
        airline2.printPlane();



    }

}

最佳答案

您的Airline构造函数收到一个String参数,但是您没有将Airline类的实例变量airlineName分配给该参数。您只是将airlineName设置为“”。

public Airline(String airlineName)
    {
        this.airlineName = "";
        map = new HashMap<String, PlaneStore>();
    }


你的意思是:

public Airline(String airlineName)
        {
            this.airlineName = airlineName;
            map = new HashMap<String, PlaneStore>();
        }


在代码中进一步查看,似乎每个Airline类将具有一个map,但是您只向该映射添加了一个键以及值PlaneStore。因此,对于每个map类中的每个Airline,您似乎永远不会拥有两个或多个键。因此,使用map没有意义,可以使用ArrayList代替。

如果我正确理解了您的目标,则可以将AirLine类简化为以下形式:

public class Airline
{
    private String airlineName;                // Name of the company
    private ArrayList <PlaneStore> planeStore;

    public Airline(String airlineName)
    {
        this.airlineName = airlineName;
        map = new ArrayList<PlaneStore>();
    }
    public void add(PlaneStore plane){ planeStore.add(plane);}


    public void printPlane()
    {

        System.out.println(airlineName);
        System.out.println("\n********Flight List********");
        for (PlaneStore plane: planeStore)
        {
            System.out.println(plane.toString());
        }

    }


}

09-28 14:01