-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovieTicketBookingSystem.java
More file actions
170 lines (139 loc) · 5.53 KB
/
MovieTicketBookingSystem.java
File metadata and controls
170 lines (139 loc) · 5.53 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class MovieTicketBookingSystem extends Application {
// Movie prices
private static final double REGULAR_MOVIE_PRICE = 10.0;
private static final double PREMIUM_MOVIE_PRICE = 15.0;
// UI components
private ComboBox<String> movieComboBox;
private ComboBox<String> showtimeComboBox;
private Spinner<Integer> ticketSpinner;
private Label totalCostLabel;
@Override
public void start(Stage primaryStage) {
// Set the title
primaryStage.setTitle("Movie Ticket Booking System");
// Create the main grid
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
// Add title
Text sceneTitle = new Text("Movie Ticket Booking");
sceneTitle.setFont(Font.font("Tahoma", FontWeight.BOLD, 20));
grid.add(sceneTitle, 0, 0, 2, 1);
// Movie selection
Label movieLabel = new Label("Select Movie:");
grid.add(movieLabel, 0, 1);
movieComboBox = new ComboBox<>();
movieComboBox.getItems().addAll(
"The Avengers (Regular)",
"Jurassic World (Regular)",
"Star Wars (Premium)",
"The Dark Knight (Premium)"
);
movieComboBox.setPromptText("Select a movie");
movieComboBox.setOnAction(e -> calculateTotal());
grid.add(movieComboBox, 1, 1);
// Showtime selection
Label showtimeLabel = new Label("Select Showtime:");
grid.add(showtimeLabel, 0, 2);
showtimeComboBox = new ComboBox<>();
showtimeComboBox.getItems().addAll(
"10:00 AM",
"1:00 PM",
"4:00 PM",
"7:00 PM",
"10:00 PM"
);
showtimeComboBox.setPromptText("Select a showtime");
grid.add(showtimeComboBox, 1, 2);
// Number of tickets
Label ticketsLabel = new Label("Number of Tickets:");
grid.add(ticketsLabel, 0, 3);
ticketSpinner = new Spinner<>(1, 10, 1);
ticketSpinner.setEditable(true);
ticketSpinner.valueProperty().addListener((obs, oldValue, newValue) -> calculateTotal());
grid.add(ticketSpinner, 1, 3);
// Total cost
Label costLabel = new Label("Total Cost:");
grid.add(costLabel, 0, 4);
totalCostLabel = new Label("$0.00");
grid.add(totalCostLabel, 1, 4);
// Button box
HBox buttonBox = new HBox(10);
buttonBox.setAlignment(Pos.CENTER);
Button confirmButton = new Button("Confirm Booking");
confirmButton.setOnAction(e -> confirmBooking());
Button resetButton = new Button("Reset");
resetButton.setOnAction(e -> resetForm());
Button exitButton = new Button("Exit");
exitButton.setOnAction(e -> primaryStage.close());
buttonBox.getChildren().addAll(confirmButton, resetButton, exitButton);
grid.add(buttonBox, 0, 5, 2, 1);
// Create scene and show
Scene scene = new Scene(grid, 400, 350);
primaryStage.setScene(scene);
primaryStage.show();
}
// Calculate the total cost
private void calculateTotal() {
if (movieComboBox.getValue() == null) {
return;
}
double price = 0.0;
if (movieComboBox.getValue().contains("Regular")) {
price = REGULAR_MOVIE_PRICE;
} else if (movieComboBox.getValue().contains("Premium")) {
price = PREMIUM_MOVIE_PRICE;
}
int numTickets = ticketSpinner.getValue();
double total = price * numTickets;
totalCostLabel.setText(String.format("$%.2f", total));
}
// Confirm booking
private void confirmBooking() {
if (movieComboBox.getValue() == null || showtimeComboBox.getValue() == null) {
showAlert("Error", "Please select a movie and showtime.");
return;
}
String movie = movieComboBox.getValue();
String showtime = showtimeComboBox.getValue();
int tickets = ticketSpinner.getValue();
double total = Double.parseDouble(totalCostLabel.getText().substring(1));
String message = String.format(
"Booking Confirmed!\n\nMovie: %s\nShowtime: %s\nTickets: %d\nTotal Cost: $%.2f",
movie, showtime, tickets, total
);
showAlert("Booking Confirmed", message);
}
// Reset the form
private void resetForm() {
movieComboBox.setValue(null);
showtimeComboBox.setValue(null);
ticketSpinner.getValueFactory().setValue(1);
totalCostLabel.setText("$0.00");
}
// Show an alert
private void showAlert(String title, String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
public static void main(String[] args) {
launch(args);
}
}