-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLunchMenu.java
More file actions
79 lines (64 loc) · 2.5 KB
/
LunchMenu.java
File metadata and controls
79 lines (64 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class LunchMenu extends Application {
private TextArea resultTextArea;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
ComboBox<String> lunchMenu = new ComboBox<>();
lunchMenu.getItems().addAll("Hot Dog", "Sandwich", "Hamburger");
root.setTop(lunchMenu);
resultTextArea = new TextArea();
root.setCenter(resultTextArea);
ToggleGroup group = new ToggleGroup();
RadioButton coffee = new RadioButton("Coffee");
RadioButton tea = new RadioButton("Tea");
RadioButton pop = new RadioButton("Pop");
coffee.setToggleGroup(group);
tea.setToggleGroup(group);
pop.setToggleGroup(group);
VBox radioButtonBox = new VBox(10, coffee, tea, pop);
radioButtonBox.setPadding(new Insets(10));
root.setLeft(radioButtonBox);
Button submit = new Button("Submit");
root.setBottom(submit);
BorderPane.setAlignment(submit, Pos.CENTER);
submit.setOnAction(event -> calculateTotal(lunchMenu.getValue(), group));
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("Lunch Menu");
primaryStage.setScene(scene);
primaryStage.show();
}
private void calculateTotal(String food, ToggleGroup group) {
double foodPrice = 0;
double drinkPrice = 0;
switch (food) {
case "Hot Dog": foodPrice = 1.50; break;
case "Sandwich": foodPrice = 2.00; break;
case "Hamburger": foodPrice = 2.50; break;
}
RadioButton selectedDrink = (RadioButton) group.getSelectedToggle();
if (selectedDrink != null) {
switch (selectedDrink.getText()) {
case "Coffee": drinkPrice = 1.00; break;
case "Tea": drinkPrice = 0.75; break;
case "Pop": drinkPrice = 1.25; break;
}
}
double total = foodPrice + drinkPrice;
resultTextArea.setText("Total is $" + total);
}
}