我正在尝试在单独的屏幕上打印收据

时间:2018-10-16 09:28:23

标签: java

我正在尝试制作一个收银员程序,以便在单独的屏幕上打印带有价格的所选产品。我快到了,但无法弄清楚最后一部分。

当我选择我的产品时,我得到以下回报:

[Ljava.lang.String;@3774e19

代替:

Pizza Margherita - (eur)6.-

这是我的代码:

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]);
    }
}

}

1 个答案:

答案 0 :(得分:1)

您每次在数组ObservableList data上添加String[] producten,以便显示该对象。我想您想做的是:

 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));

            }
        });

但是我认为您应该做一些Pojo之类的事情:

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 +
                '}';
    }
}

在通用对象上也有一个强类型:ObservableList<Pizza> data = FXCollections.observableArrayList();