嘿,我正在制作一个 planeManagerApp。我有一个 TreeMap 包含 Airline , HashMap(String,Plane);
我希望航空公司打印出属于它的飞机。因此,对于 RyanAir,我希望为他们打印 3 架飞机,对于 AerLingus,我希望为他们打印 3 架飞机。但是当我跑步时,我最后添加了航空公司,并添加了所有飞机。
这是我的代码:
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class MainApp
{
public static void main(String[] args)
{
//PlaneStore map = new PlaneStore();
Airline a1 = new Airline("Aer Lingus");
Plane p1 = new Plane("A01", 150.5, 10.5, 500, Plane.AIRPLANETYPE.AIRBUS);
Plane p2 = new Plane("B01", 50.3, 1.5, 91, Plane.AIRPLANETYPE.CORPORATE);
Plane p3 = new Plane("C01", 12.2, -3.1, 56, Plane.AIRPLANETYPE.AIRBUS);
Airline a2 = new Airline("Brian Air");
Plane p4 = new Plane("D01", 10.5, 1.5, 430, Plane.AIRPLANETYPE.PRIVATE);
Plane p5 = new Plane("E01", 0.3, 2.1, 101, Plane.AIRPLANETYPE.CORPORATE);
Plane p6 = new Plane("F01", 2.2, -3, 291, Plane.AIRPLANETYPE.AIRBUS);
HashMap<String, Plane> airlineMap = new HashMap<String, Plane>();
airlineMap.put(p1.getFlightNumber(), p1);
airlineMap.put(p2.getFlightNumber(), p2);
airlineMap.put(p3.getFlightNumber(), p3);
airlineMap.put(p4.getFlightNumber(), p4);
airlineMap.put(p5.getFlightNumber(), p5);
airlineMap.put(p6.getFlightNumber(), p6);
TreeMap<Airline, HashMap<String, Plane>> map = new TreeMap<Airline, HashMap<String, Plane>>();
map.put(a1, airlineMap);
map.put(a2, airlineMap);
System.out.println("\n-------- PRINT KEY DETAILS --------");
for(Map.Entry theEntry : map.entrySet())
System.out.println(theEntry.getKey());
for(Map.Entry theEntry : map.entrySet())
{
HashMap<String, Plane> map2= (HashMap<String, Plane>)theEntry.getValue();
System.out.println("\n-------- PRINT CLIENT DETAILS --------");
for(Map.Entry theNextEntry : map2.entrySet())
System.out.println(theNextEntry.getValue());
}
//Plane p4 = new Plane("D01", 300.9, 45, 402, Plane.AIRPLANETYPE.PRIVATE);
//Plane p5 = new Plane("E01", 455, 23, 100, Plane.AIRPLANETYPE.AIRBUS);
//Airline.airlineMap.add("Aer Lingus", p1);
// map.put(Airline.getAirlineName(), p1);
}
}