0

Hey im using a hashmap of string and flights to create a flight store called planeStore. So then i made another store(Airline store) also using a hashmap. I put the planeStore into the AirlineStore. But i cant get the airlines printing out with the planes.

I thought by putting string airlineName into Airlines constrcutor. And passing in "Plane Name" When creating Airline airline = new Airline("PlaneName"); that this would work but it hasnt.

here is my code:

Airline

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 is the toString of the PlaneStore:

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



    }

}
4

3 回答 3

1

您将 airportName 设置为 "" 而不是通过构造函数传递给它的字符串。

于 2012-12-10T22:13:55.470 回答
1

您的Airline构造函数收到一个String参数,但您没有将airlineNameAirline 类的实例变量分配给该参数。您只是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());
        }

    }

}

于 2012-12-10T22:14:34.160 回答
0

一旦您将代码更改为阅读this.airlineName = airlineName;,您应该可以接受这部分。

顺便说一句,您添加的每架飞机看起来仍然使用相同的密钥。这将不断用新的飞机覆盖您添加的最后一架飞机。如果您希望能够打印出所有的飞机,您需要给它们每一个唯一的钥匙。否则这是行不通的。

于 2012-12-10T22:16:50.823 回答