Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the code for a GUI application that allows the user to purchase tickets

ID: 3705189 • Letter: C

Question

Complete the code for a GUI application that allows the user to purchase tickets for a movie. There are 4 movies to choose from. A ticket to a 2D movie costs $12, while a 3D ticket costs $17.

This is what the application looks like when it loads PURCHASE MOVIE TICKETS Select a movie: Number of tickets: PURCHASE CANCEL The user can select a movie from the dropdown list and enter a value for the number of tickets in the text box PURCHASE MOVIE TICKETS Select a movie: Batman V Superman (3D) Number of tickets:4 PURCHASE CANCEL

Explanation / Answer

----------------------Lab11.java---------------------

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.geometry.*;

public class Lab11 extends Application {

// Declare the class properties
private Stage stage;
private VBox sceneBox;
private HBox movieListingBox;
private HBox numTicketsBox;
private HBox buttonBox;
private Label movieListingLabel;
private Label numTicketsLabel;
private ChoiceBox<String> movieChoiceBox;
private TextField numTicketsTextField;
private Button purchaseButton;
private Button cancelButton;

// Use these constants when calculating total cost
private static double TICKET_PRICE_3D = 17.0;
private static double TICKET_PRICE_2D = 12.0;

@Override
public void start(Stage primaryStage) {
stage = primaryStage;
stage.setTitle("PURCHASE MOVIE TICKETS");

// TODO: Initialize movieListingLabel and set its minimum width to 75
movieListingLabel = new Label("Select a movie:");
movieListingLabel.setMinWidth(75);

// TODO: Initialize movieChoiceBox and add the following movie titles to it:
String movie1 = "Batman V Superman (3D)";
String movie2 = "Kung Fu Panda 3 (2D)";
String movie3 = "My Big Fat Greek Wedding 2 (3D)";
String movie4 = "Rush Hour (2D)";
movieChoiceBox = new ChoiceBox();
movieChoiceBox.getItems().add(movie1);
movieChoiceBox.getItems().add(movie2);
movieChoiceBox.getItems().add(movie3);
movieChoiceBox.getItems().add(movie4);

// Place movieListingLabel and movieChoiceBox into the movieListingBox container
movieListingBox = new HBox(20, movieListingLabel, movieChoiceBox);

// TODO: Initialize numTicketsLabel and set its minimum width to 75
numTicketsLabel = new Label("Number of Tickets:");
numTicketsLabel.setMinWidth(75);

// TODO: Initialize numTicketsTextField and set its preferred column width to 4
numTicketsTextField = new TextField();
numTicketsTextField.prefWidth(4);

// Place numTicketsLabel and numTicketsTextField into the numTicketsBox container
numTicketsBox = new HBox(20, numTicketsLabel, numTicketsTextField);

// TODO: Initialize purchaseButton and cancelButton
purchaseButton = new Button("PURCHASE");
cancelButton = new Button("CANCEL");

// Set the actions for the buttons
cancelButton.setOnAction(e -> close());
purchaseButton.setOnAction(e -> displayResults());

// TODO: Place the two buttons into buttonBox with 60 pixels between them
buttonBox = new HBox(60,purchaseButton,cancelButton);

// Center the buttons in buttonBox
buttonBox.setAlignment(Pos.CENTER);

// Add the sub-containers to the sceneBox and put some padding around each one
sceneBox = new VBox(60, movieListingBox, numTicketsBox, buttonBox);
sceneBox.setPadding(new Insets(30,20,30,20));

// Create a new Scene object with the sceneBox in it
Scene scene = new Scene(sceneBox, 440, 280);

// TODO: Set scene to be the scene for stage and show the stage
stage.setScene(scene);
stage.show();

}

// This method will close and application
public void close() {
stage.close();
}

// TODO: Code the displayResults method. This method is called when the user clicks on the
// purchase button.
//
// Call MessageBox.show to generate dialog boxes.
//
// Here is some pseudo code for this method:
//
// Initialize numTickets (int) to 0 and totalCost (double) to 0.0
// Set movieSelection to the value that is currently selected in movieChoiceBox
//
// If movieSelection is null:
// Show an error dialog box that says, "Please select a movie!"
// Else:
// Try:
// Set numTickets to be the parsed value of numTicketsTextField
// If movieSelection ends with "(2D)":
// Set totalCost = numTickets * TICKET_PRICE_2D
// Else:
// Set totalCost = numTickets * TICKET_PRICE_3D
// Display a dialog box that shows the total cost
// Catch (NumberFormatException e):
// Display an error dialog box that says, "Please enter a number for the number of tickets!"
//
public void displayResults() {
// System.out.println("displayResults");
String movieSelection = (String) movieChoiceBox.getValue();
if(movieSelection==null){
MessageBox.show("Please select a movie!","ERROR");
return;
}

Integer numTickets = null;
try{
numTickets = Integer.parseInt(numTicketsTextField.getText());
}catch(Exception e){
MessageBox.show("Please enter a number for the number of tickets!","ERROR");
return;
}
double totalCost = 0;
if(movieSelection.endsWith("(2D)")){
totalCost = numTickets * TICKET_PRICE_2D;
}else{
totalCost = numTickets * TICKET_PRICE_3D;
}
MessageBox.show("TOTAL IS: $"+totalCost,"THANK YOU!");
}

// Start the application in the main method
public static void main(String[] args) {
Application.launch(args);
}

}