Hi the code you see below is based on a train booking system interface, I have included one of the methods called addCustomer,which prompts a textfield to request customer name(Hashmap Key), then as soon as the name is submitted, the seat buttons are then projected allowing the customer to select multiple seats under his or her name,which the selected seat number is stored as the Key value for the customer name.The seat capacity is limited to 42 Seats.
[Heres how the interface looks like:IMAGE But setting the Hashmap is my main issue, it is storing only one seat number for each customer name.
public static final int SEATING_CAPACITY = 42;
public static void addCustomer(Button[] viewBtn, GridPane pane, int[] keep, TextField tf, HashMap customerBooked, Button submit) {
pane.setVgap(10);
pane.setHgap(35);
pane.setPadding(new Insets(10, 10, 10, 280));
submit.setOnAction(event -> {
int i = 0;
for (int y = 1; y <= 14; y++) {
for (int x = 1; x <= 3; x++) {
i++;
String num = "" + i;
i--;
viewBtn[i] = new Button(num);
viewBtn[i].setId("Seat " + i);
viewBtn[i].setDisable(true);
pane.add(viewBtn[i], x, y);
viewBtn[i].setMaxSize(50, 150);
viewBtn[i].setDisable(false);
if (keep[i] == 1) {
viewBtn[i].setStyle("-fx-background-color: red;");
} else {
viewBtn[i].setStyle("-fx-background-color: #03fc98;");
int finalI = i;
viewBtn[i].setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
viewBtn[finalI].setStyle("-fx-background-color: red;");
keep[finalI] = 1;
String name = new String(tf.getText());
customerBooked.put(name, viewBtn[finalI]);
}
});
}
i++;
}
}
});
System.out.println(customerBooked);
}
I noticed this happening once i started printing the Hashmap,for e.g.When i type the name Sam and clicked on the following button seats 24,27,33.It ends up taking the last seat.
{Sam=Button[id=Seat 32, styleClass=button]'33'}
Along with that the button styles and seat ID are also placed to the seat value which i don't want to consider.
I need to fix it so it looks like the one below.That way i can adapt it to the rest of my other methods like searching customer seat by name or deleting based on the name.
{Sam='24','27','33',Jane='25'}