-1

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'}
Genesis_Kitty
  • 63
  • 2
  • 7
  • Why do you use ```new String(...)```? I see that almost every day but it was never good for anything. In Java all variables are references, so you can simply reference the existing string instead of copying it. The garbage collector will automatically move the object into another memory area if necessary. You don't need to be afraid of returning objects from local stack. – Stefan Mar 08 '20 at 10:51
  • please read up on java basics, in particular on the characteristics of Map. – kleopatra Mar 08 '20 at 11:18

1 Answers1

1

The purpose of HashMap is to store exactly one entry per key. What you want to achieve is impossible with the HashMap unless you use lists as value, e.g. HashMap<String,ArrayList<String>>.

There are several libraries available which provide a MultiMap, that does what you want. See https://www.baeldung.com/java-map-duplicate-keys

Stefan
  • 1,789
  • 1
  • 11
  • 16
  • The thing was it previously did started taking more values for each name that was when i placed it outside the eventhandler, the reason its inside the eventhandler of the submit button is that i needed to get the text into the name array. – Genesis_Kitty Mar 08 '20 at 10:41
  • @Genesis_Kitty then you changed something else along with placing it _outside the eventHandler_ .. plain Maps __never__ store multiple values per key, independent on where they are filled. – kleopatra Mar 08 '20 at 11:21