//Represents a seat on a plane.
public class Seat {
//The section that the seat is in.
public int sectionId;
public int seatId;
public Seat(int seatId, int sectionId) {
this.seatId = seatId;
this.sectionId = sectionId;
}
}
Das oben genannte enthält nur die "Einstellungen", die ich für jeden Platz errate. Ich versuche, den Konstruktor in einer Karte rufen jeden eindeutigen Sitz mit einem Sitztyp zu (Erwachsene/Kinder)/hier ist, was ich kam mit:Konstruktor als Schlüssel in einer Hashmap?
public enum SeatType {
ADULT,
CHILD;
}
private static Map<Seat, SeatType> map = new LinkedHashMap<>();
public static void main(String[] args) {
int sectionId = 5;
map.put(new Seat(1, 5), SeatType.ADULT);
System.out.println(map.get(new Seat(1, 5)));
Ausführung dieses Codes heraus druckt „null“ zur Konsole. Ich weiß, dass ich für jeden Sitz leicht ein neues Objekt erstellen kann, aber das ist nicht wirklich eine Option, denn das würde bedeuten, dass ich mehr als 200 Objekte erstellen müsste.
Ich habe nicht erwartet, dass es funktioniert, aber ich suchte nach einer Erklärung, warum es nicht funktioniert und vielleicht eine mögliche Lösung für das Problem.
Vielen Dank im Voraus! (Und tut mir leid, noch ein Anfänger).