-1

I am trying to make a cashier program that prints the chosen products with the price on a separate screen. I'm almost there but can't figure out how to do the last part.

When i choose my products i get these returns:

[Ljava.lang.String;@3774e19

instead of like:

Pizza Margherita - (eur)6.-

This is my code:

public class Main extends Application {

int screenWidth = 500;
int screenHeight = 300;

int pizzaCounter = 0;
int totalPrice = 0;
double korting = 1.0;

int buttonCounter = 0;
int count = 0;
Text pizzaCounterText = new Text();
final StackPane root = new StackPane();
final StackPane root2 = new StackPane();

public static final ObservableList data = FXCollections.observableArrayList();
ListView<String> receipt = new ListView<>(data);

Button[] buttons = new Button[8];
String[] producten = new String[8];
String[] totaal = new String[200];

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Kassa");

    TextArea textArea = new TextArea();
    VBox vbox = new VBox(textArea);
    receipt.setEditable(false);
    receipt.setStyle("-fx-font-size:20.0;");
    root2.getChildren().add(receipt);

    HBox hbox = new HBox();
    Button discount = new Button("10% korting");
    Button afrekenen = new Button("Afrekenen!");

    discount.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            if (korting == 1) {
                korting = 0.9;
            } else {
                korting = 1;
            }

        }
    });

    afrekenen.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            primaryStage.setScene(new Scene(root2, screenWidth, screenHeight));
            primaryStage.show();
        }
    });

    hbox.getChildren().addAll(discount, afrekenen);

    Scene scene = new Scene(vbox, -310, -40);
    primaryStage.setScene(scene);
    primaryStage.show();

    pizzaCounterText.setText("Items bought: " + Integer.toString(pizzaCounter) + "\nTotal: " + Integer.toString(totalPrice) + ",--");
    producten[0] = "Pizza Margherita-6";
    producten[1] = "Pizza Funghi-10";
    producten[2] = "Pizza Prociutto-8";
    producten[3] = "Pizza Hawaii-11";
    producten[4] = "Pizza Shoarma-12";
    producten[5] = "Pizza Salami-10";
    producten[6] = "Cola Regular-2";
    producten[7] = "Cola Zero-2";

    // maakt de buttons
    for (int i = 0; i < 8; i++) {
        String[] parts = producten[i].split("-");
        buttons[i] = createButton(i, parts[0], Integer.parseInt(parts[1]));
    }

    root.setAlignment(TOP_LEFT);

    pizzaCounterText.setTranslateX(screenWidth - 300);
    pizzaCounterText.setTranslateY(screenHeight - 40);
    vbox.setTranslateX(screenWidth - 500);
    vbox.setTranslateY(screenHeight - 40);
    hbox.setTranslateX(screenWidth - 350);
    hbox.setTranslateY(screenHeight - 90);
    root.getChildren().add(hbox);
    root.getChildren().add(vbox);
    root.getChildren().add(pizzaCounterText);

    primaryStage.setScene(new Scene(root, screenWidth, screenHeight));
    primaryStage.show();
}

public Button createButton(int index, String name, int price) {
    Button btn = new Button();
    btn.setText(name);
    btn.setMinWidth(100);

    int column = buttonCounter % 3;
    int row = buttonCounter / 3;

    btn.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            totalPrice += price;
            pizzaCounter++;
            do {
                totaal[count] = name;
                count++;

                data.add(producten);
            } while (bonActive());
            pizzaCounterText.setText("Items bought: " + Integer.toString(pizzaCounter) + "\nTotal: " + Double.toString(totalPrice * korting));
            System.out.println(Arrays.deepToString(totaal));

        }
    });

    btn.setTranslateX(50 + 150 * column);
    btn.setTranslateY(35 + 50 * row);

    root.getChildren().add(btn);

    buttonCounter++;

    return btn;
}

public boolean bonActive() {
    return false;
}

public void bon() {
    for (int i = 0; i < totaal.length; i++) {
        System.out.println(totaal[i]);
    }
}

}

Sreeram TP
  • 11,346
  • 7
  • 54
  • 108
Tobias
  • 3
  • 1
  • 1
    Btw, korting means discount, and afrekenen means checkout – Tobias Oct 16 '18 at 09:29
  • Possible duplicate of [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Jaroslaw Pawlak Oct 16 '18 at 09:59
  • 1
    Next time before you post a question, you should remove as much code as you can, until you get [mcve]. 90% of code you posted has nothing to do with your problem. – Jaroslaw Pawlak Oct 16 '18 at 10:00

1 Answers1

0

You are adding on the ObservableList data every time the array String[] producten so it's displaying this object. I think what you want to do is something like :

 btn.setOnAction(new EventHandler<ActionEvent>() {
       @Override
       public void handle(ActionEvent event) {
            totalPrice += price;
            pizzaCounter++;
            do {
               totaal[count] = name;
               count++;
               data.add(name + " " + price);
            } while (bonActive());

            pizzaCounterText.setText("Items bought: " + Integer.toString(pizzaCounter) + "\nTotal: " + Double.toString(totalPrice * korting));
                System.out.println(Arrays.deepToString(totaal));

            }
        });

But I think you should do some Pojo like :

static class Pizza {
    String name;
    int price;

    public Pizza(String name, int price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Pizza{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

And also have a strong type on your generic object : ObservableList<Pizza> data = FXCollections.observableArrayList();

Matthieu Gabin
  • 830
  • 9
  • 26