Reputation: 25
MY TASK : Write a JavaFX GUI application that allows the user to pick a set of pizza toppings using a set of check boxes. Assuming each topping cost 50 cents, and a plain pizza costs $10, display the cost of the pizza. Note that, once a topping is checked or unchecked, the cost of pizza should update automatically.
FIX REQUEST : my pizza cost calculator fails to add cost for topping properly
For example, Extracheese checked and unchecked resulted in below base price $10 (ie; $7)
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
import javafx.scene.text.Text;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
class PizzaCost extends VBox {
private CheckBox ExtraCheeseCheckBox;
private CheckBox GreenPepperCheckBox;
private Label TotalPizzaCost;
public double pizzaCost = 10.0;
public PizzaCost(){
Font font = new Font(13);
ExtraCheeseCheckBox = new CheckBox("Extra Cheese");
ExtraCheeseCheckBox.setOnAction(this::processCheckBoxAction);
GreenPepperCheckBox = new CheckBox("GreenPepper");
GreenPepperCheckBox.setOnAction(this::processCheckBoxAction);
TotalPizzaCost = new Label(pizzaCost +"");
TotalPizzaCost.setFont(font);
HBox options = new HBox(ExtraCheeseCheckBox,GreenPepperCheckBox);
getChildren().addAll(options,TotalPizzaCost);
}
public void processCheckBoxAction (ActionEvent event){
if (ExtraCheeseCheckBox.isSelected()){
pizzaCost += 0.5;
}
else {
pizzaCost -= 0.5;
}
TotalPizzaCost.setText(pizzaCost+"");
}
}
Upvotes: 0
Views: 86
Reputation: 20924
The following code works for me. The pizza cost never goes below $10.
import java.text.NumberFormat;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class PizzaCost extends Application {
private static final double TOPPING_COST = 0.5d;
private static final NumberFormat COST_FORMAT = NumberFormat.getCurrencyInstance();
private static final String PIZZA_COST = "Pizza Cost: ";
private double pizzaCost = 10.0d;
private Label totalPizzaCostLabel;
@Override
public void start(Stage primaryStage) throws Exception {
CheckBox extraCheesCheckBox = getCheckBox("Extra Cheese");
CheckBox greenPepperCheckBox = getCheckBox("Green Pepper");
HBox hBox = new HBox(20.0d, extraCheesCheckBox, greenPepperCheckBox);
totalPizzaCostLabel = new Label(PIZZA_COST + COST_FORMAT.format(pizzaCost));
totalPizzaCostLabel.setFont(new Font(13.0d));
VBox root = new VBox(20.0d, hBox, totalPizzaCostLabel);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(40.0d));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setTitle("Pizza Cost");
primaryStage.show();
}
private CheckBox getCheckBox(String text) {
CheckBox cb = new CheckBox(text);
cb.setOnAction(this::processCheckBoxAction);
return cb;
}
private void processCheckBoxAction(ActionEvent event) {
CheckBox cb = (CheckBox) event.getSource();
if (cb.isSelected()) {
pizzaCost += TOPPING_COST;
}
else {
pizzaCost -= TOPPING_COST;
}
totalPizzaCostLabel.setText(PIZZA_COST + COST_FORMAT.format(pizzaCost));
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 2