如何将总价添加到最终输出中?

时间:2019-04-20 14:49:19

标签: java javafx

我正在使用JavaFX创建一个“ PizzaOrder”应用程序。我已经启动并运行了该窗口,并且按钮,单选按钮,下拉菜单和复选框可以正常工作,但无法弄清楚如何以含税价格调高最终价格。

我是一名试图学习编码的大一学生。我尝试了多种方法,但到目前为止没有任何效果,因此下面的代码为空/未完成。

package application;

import java.util.ArrayList;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;


public class PizzaOrderApp extends Application {

    private TextArea output;
    private Button order;
    private ChoiceBox<String> choiceBox;
    private ToggleGroup group;
    private TextField name;
    ArrayList<CheckBox> listCheckBoxes = new ArrayList<CheckBox>();

    public void start(Stage primaryStage) {

        Text text = new Text();

        text.setFont(Font.font("nfl minnesota vikings", FontWeight.EXTRA_BOLD, FontPosture.REGULAR, 100));
        text.setFill(Color.DARKVIOLET);
        text.setStrokeWidth(4);
        text.setStroke(Color.GOLD);
        text.setText("SKOL PIZZA!");

        FlowPane paneTop = new FlowPane(text);
        paneTop.setAlignment(Pos.CENTER);
        paneTop.setHgap(20);
        paneTop.setStyle("-fx-background-color: purple;");

        String crustType[] = {"Thick", "Thin", "Gluten Free" };

        choiceBox = new ChoiceBox<String>(); 
        choiceBox.getItems().addAll(crustType);
        choiceBox.getSelectionModel().selectFirst();

        VBox verticalBoxLeft = new VBox();
        verticalBoxLeft.getChildren().add(choiceBox);

        group = new ToggleGroup();

        RadioButton rb1 = new RadioButton("10\" Pizza");
        rb1.setToggleGroup(group);
        rb1.setSelected(true);

        RadioButton rb2 = new RadioButton("12\" Pizza");
        rb2.setToggleGroup(group);

        RadioButton rb3 = new RadioButton("14\" Pizza");
        rb3.setToggleGroup(group);

        RadioButton rb4 = new RadioButton("16\" Pizza");
        rb4.setToggleGroup(group);

        verticalBoxLeft.getChildren().add(rb1);
        verticalBoxLeft.getChildren().add(rb2);
        verticalBoxLeft.getChildren().add(rb3);
        verticalBoxLeft.getChildren().add(rb4);

        verticalBoxLeft.setSpacing(10);

        Pane paneLeft = new Pane(verticalBoxLeft);
        paneLeft.setStyle("-fx-background-color: gold;");
        paneLeft.setPrefSize(200, 600);

        output = new TextArea(); 
        output.setPrefColumnCount(30);
        output.setPrefRowCount(40);
        output.setEditable(false); 
        output.setText("Order");

        Pane paneRight = new Pane(output);
        paneRight.setStyle("-fx-background-color: gold;");
        paneRight.setPrefSize(200, 600);

        Font font = new Font(18);
        Label nameLabel = new Label("Name: ");
        nameLabel.setFont(font);

        name = new TextField();
       //name.setFont(font);
        name.setPrefWidth(50);
        name.setAlignment(Pos.CENTER);



        FlowPane paneCenter = new FlowPane(nameLabel, name);
        paneCenter.setHgap(20);
        paneCenter.setAlignment(Pos.CENTER);

        CheckBox topping1 = new CheckBox("Pepperoni");
        CheckBox topping2 = new CheckBox("Sausage");
        CheckBox topping3 = new CheckBox("Chicken");
        CheckBox topping4 = new CheckBox("Bacon");
        CheckBox topping5 = new CheckBox("Pineapple");

        listCheckBoxes.add(topping1);
        listCheckBoxes.add(topping2);
        listCheckBoxes.add(topping3);
        listCheckBoxes.add(topping4);
        listCheckBoxes.add(topping5);


        paneCenter.getChildren().add(topping1);
        paneCenter.getChildren().add(topping2);
        paneCenter.getChildren().add(topping3);
        paneCenter.getChildren().add(topping4);
        paneCenter.getChildren().add(topping5);
        paneCenter.setVgap(10);

        paneCenter.setStyle("-fx-background-color: gold;");

        order = new Button("Order");
        Button cancel = new Button("Cancel");


        order.setOnAction(this::processButtonPress);
        cancel.setOnAction(this::processButtonPress);


        FlowPane paneBottom = new FlowPane(order, cancel);

        paneBottom.setStyle("-fx-background-color: purple;");
        paneBottom.setPrefSize(400,100);
        paneBottom.setAlignment(Pos.CENTER);
        paneBottom.setHgap(50);


        BorderPane borderPane = new BorderPane();
        borderPane.setTop(paneTop);
        borderPane.setLeft(paneLeft);
        borderPane.setCenter(paneCenter);
        borderPane.setRight(paneRight);     
        borderPane.setBottom(paneBottom);

        Scene scene = new Scene(borderPane,600,400);
        primaryStage.setScene(scene);
        primaryStage.show();

    }


    public void processButtonPress(ActionEvent event)
    {
        if(event.getSource() == order) {
            output.appendText("\nOrder Button Pushed");
            String outputText = "";
            RadioButton selected = (RadioButton) group.getSelectedToggle();
            outputText += "\nSize: "+selected.getText();
            outputText += "\nCrust: "+choiceBox.getValue();
            outputText += "\nName: "+name.getText();




            for(CheckBox element : listCheckBoxes)
                if(element.isSelected())
                    outputText += "\nTopping: "+element.getText();

            output.setText(outputText);

    }
        else {
            output.appendText("\nCancel Button Pushed");

        }
    }

    public class PizzaOrder {

        private int size;
        private String crustType;
        private String name;
        //private double topping;
        ArrayList<String> toppings = new ArrayList<String>();


        public PizzaOrder(int size, String crustType, String name)
        {
            this.size = size;
            this.crustType = crustType;
            this.name = name;
        }

        public void addTopping(String topping)
        {


        }

        private double calculatePrice()
        {
            double price = 0;
            double totalPrice=0;
            double tax = .055;
            totalPrice = price + (price*tax);
            return totalPrice;
        }

        public String toString()
        {
            String str = toString()+ "Size: "+size+"\nCrust: "+crustType+"\nToppings: "+toppings+"\nName: "+name+"\nTax: "+(price*tax)+"\nFinal Price: "+totalPrice;
            return str;
        }
    }

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

选择用户想要的内容后,我需要显示最终输出:

  • 大小
  • 结皮
  • 浇头
  • 名称
  • 税收
  • 最终价格

分配说明: enter image description here

1 个答案:

答案 0 :(得分:0)

  

不知道如何调高含税的最终价格。

您根本没有按照指示使用PizzaOrder。处理order.setOnAction时,应创建此类的实例,并使用addTopping(String)将所有浇头添加到该实例中。

您还应该实现calculatePrice()方法,以便它使用类的字段来计算价格。最简单的方法是在switch上方size并基于此设置price,然后根据toppigns.size()添加浇头的价格。

然后在toString()文本区域中使用该实例的output。不要从控件中读取值。

注意:

  • 代替

    ArrayList<CheckBox> list = new ArrayList<CheckBox>();
    

    List<CheckBox> listCheckBoxes = new ArrayList<>();
    

    在这种情况下,最好对接口进行编码,并且您不需要在右侧使用通用类型,因为编译器会推断出这种类型。

  • 对每个按钮操作使用单独的方法。不要使用相同的方法,请检查源并基于此采取行动。