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();
}
}
Your
Airlineconstrutor receives aStringargument but you are not assignment the instance variableairlineNameof the class Airline to this argument. You are just makingairlineNameset to “”.you mean:
Looking further in your code, it appears that each
Airlineclass will have anmapbut you only add a key to this map, along with the valuesPlaneStore. So it appears that you will never have two or more keys for eachmapon eachAirlineclass. Therefore, there is no point in usingmap, you can use instead anArrayList.If I understand correctly your objective you can simplify your AirLine class to something like this:
}