嘿,我正在尝试为飞机创建一个 hashMap 存储。但是当我添加时,它只会打印出一个航班。谁能帮我这个。这是我的代码:
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 aerLingus = new Airline("AerLingus");
PlaneStore planeStore = new PlaneStore("Aer Lingus");
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.addFlight("",p1);
planeStore.addFlight("",p2);
planeStore.addFlight("",p3);
planeStore.print();
aerLingus.add(planeStore);
aerLingus.add(planeStore);
aerLingus.add(planeStore);
aerLingus.printPlane();
}
}
import java.util.TreeMap;
public class PlaneStore
{
private String airlineName;
private TreeMap<String,Flight> planeMap;
public PlaneStore(String airlineName)
{
this.airlineName = "";
planeMap = new TreeMap<String,Flight>();
}
public String getAirlineName() {
return airlineName;
}
public TreeMap<String, Flight> getFlightList() {
return planeMap;
}
public void addFlight(String airline,Flight flight)
{
planeMap.put(airline, flight);
}
// ---------------------------------------------------------------------------------------
// Name: Print.
// ---------------------------------------------------------------------------------------
public void print()
{
System.out.println("\n********Student's in the Company.********");
for (Flight flight : planeMap.values()) {
// System.out.println(employee); to print the toString of Employee
// class
// or:
System.out.println("Airline:\t" + flight.getAirline());
System.out.println("Flight Number:\t" + flight.getFlightNumber());
System.out.println("Fuel Remaining:\t" + flight.getFuelRemaining());
}
}
}
public class Flight
{
private String airline;
private String flightNumber;
private double fuelRemaining;
private double overdue;
private int passengerNumber;
private AIRPLANETYPE planeType;
public enum AIRPLANETYPE
{
AIRBUS("1"), CORPORATE("2"), PRIVATE("3");
private String planeName;
private AIRPLANETYPE(String planeName)
{
this.planeName = planeName;
}
}
public Flight(String airline, String flightNumber, double fuelRemaining,
double overdue, int passengerNumber, AIRPLANETYPE planeType)
{
super();
this.airline = airline;
this.flightNumber = flightNumber;
this.fuelRemaining = fuelRemaining;
this.overdue = overdue;
this.passengerNumber = passengerNumber;
this.planeType = planeType;
}
public String getAirline() {
return airline;
}
public void setAirline(String airline) {
this.airline = airline;
}
public String getFlightNumber() {
return flightNumber;
}
public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}
public double getFuelRemaining() {
return fuelRemaining;
}
public void setFuelRemaining(double fuelRemaining) {
this.fuelRemaining = fuelRemaining;
}
public double getOverdue() {
return overdue;
}
public void setOverdue(double overdue) {
this.overdue = overdue;
}
public int getPassengerNumber() {
return passengerNumber;
}
public void setPassengerNumber(int passengerNumber) {
this.passengerNumber = passengerNumber;
}
public AIRPLANETYPE getPlaneType() {
return planeType;
}
public void setPlaneType(AIRPLANETYPE planeType) {
this.planeType = planeType;
}
public String toString()
{
return "Flight [airline=" + airline + ", flightNumber=" + flightNumber
+ ", fuelRemaining=" + fuelRemaining + ", overdue=" + overdue
+ ", passengerNumber=" + passengerNumber + ", planeType="
+ planeType + "]";
}
}