gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:27:52 26 4
gpt4 key购买 nike

我正在使用 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

最佳答案

can't figure out how to bring up the final price with tax included.

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

您还应该实现 calculatePrice() 方法,以便它使用类的字段来计算价格。最简单的方法是切换尺寸并据此设置价格,然后根据toppins添加配料的价格.size().

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

注释:

  • 代替

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

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

    在这种情况下,最好对接口(interface)进行编码,并且您不需要右侧的泛型类型,因为编译器会推断出这一点。

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

关于java - 如何将总价添加到最终输出中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55774638/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com