如何通过航空公司打印出飞机

时间:2012-12-10 22:06:56

标签: java hashmap

嘿我正在使用字符串和航班的散列图来创建一个名为planeStore的航班商店。那么我还使用hashmap创建了另一家商店(航空商店)。我将planeStore放入AirlineStore。但我不能让航空公司打印出这些飞机。

我想把字符串airlineName放入航空公司的constrcutor。并且在创建航空公司航空公司=新航空公司(“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 + "]";
    }

MainApp

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();



    }

}

3 个答案:

答案 0 :(得分:1)

您将airlineName设置为“”而不是通过构造函数传递给它的String。

答案 1 :(得分:1)

您的Airline construtor收到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());
        }

    }

}

答案 2 :(得分:0)

将代码更改为this.airlineName = airlineName;后,您应该对此部分感到满意​​。

顺便说一下,你仍然看起来对你添加的每个平面使用相同的键。这将不断覆盖您使用新平面添加的最后一个平面。如果您希望能够打印出所有飞机,则需要为每个飞机分配一个唯一的钥匙。否则这将无效。