嗨即時通訊使用字符串和航班的散列表創建一個名爲planeStore的航班商店。所以我做了另一個商店(Airline商店)也使用hashmap。我把飛機店放到了AirlineStore。但是我不能讓航空公司用飛機打印出來。如何通過航空公司打印出飛機
我想通過把航空公司名字放入航空公司的constrcutor。並傳入「平面名稱」創建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 + "]";
}
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();
}
}
是航空公司名稱曾分配給除「」之外的任何東西嗎? –
它看起來像你沒有使用你的地圖密鑰。所以你應該使用java.util.List而不是Map – micha