0% found this document useful (0 votes)
45 views4 pages

Patel Arth Lab7

This document describes a JavaFX GUI program that allows a user to select a lunch item and drink from a menu and calculates the total cost. It includes a ComboBox for lunch selection, RadioButtons for drink selection grouped in a ToggleGroup, and a submit button to trigger the calculation method. The calculation method determines the prices for the selected lunch and drink and displays the total in a TextArea. The GUI uses a BorderPane layout with the ComboBox at top, TextArea in center, and buttons on bottom.

Uploaded by

Arth Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views4 pages

Patel Arth Lab7

This document describes a JavaFX GUI program that allows a user to select a lunch item and drink from a menu and calculates the total cost. It includes a ComboBox for lunch selection, RadioButtons for drink selection grouped in a ToggleGroup, and a submit button to trigger the calculation method. The calculation method determines the prices for the selected lunch and drink and displays the total in a TextArea. The GUI uses a BorderPane layout with the ComboBox at top, TextArea in center, and buttons on bottom.

Uploaded by

Arth Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 7

Course: COSC 1047 Introduction to Computer Science II


Name: Arth Patel
Section: COSC 1047 J
Student number: 239548040

Write a GUI program as shown below.

##Methodology:

First I setup GUI components that includes ComboBox, RadioButtons and Button.
The 'lunchcomboBox' is intializes with lunch items and creates radiobuttons for
drinks. and grouped them using ToggleGroup. Next, I arranged Borderpane layout
with combobox at top, Textarea in the centre and buttons on bottom. Provides
Calculation method for total cost. For display the result create a textarea. At last,
apply main method.
#code:
package application;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Lab7Lunch extends Application


{
private ComboBox<String> lunchComboBox;
private RadioButton coffeeRadioButton;
private RadioButton teaRadioButton;
private RadioButton popRadioButton;
private ToggleGroup drinkGroup;
private TextArea textArea;

@Override
public void start(Stage primaryStage)
{
primaryStage.setTitle("Menu For Lunch");

lunchComboBox = new ComboBox<>();


lunchComboBox.getItems().addAll("Select an item","Hot Dog",
"Sandwich", "Hamburger");
lunchComboBox.setValue("Select an item");

drinkGroup = new ToggleGroup();

coffeeRadioButton = new RadioButton("Coffee");


coffeeRadioButton.setToggleGroup(drinkGroup);
coffeeRadioButton.setSelected(true);

teaRadioButton = new RadioButton("Tea");


teaRadioButton.setToggleGroup(drinkGroup);

popRadioButton = new RadioButton("Pop");


popRadioButton.setToggleGroup(drinkGroup);

VBox radioButtonsVBox = new VBox(10, new Label("Drink"),


coffeeRadioButton, teaRadioButton, popRadioButton);
radioButtonsVBox.setAlignment(Pos.CENTER_LEFT);
radioButtonsVBox.setPadding(new Insets(8));

Button submitButton = new Button("Submit");


submitButton.setOnAction(e -> calculateTotal());
HBox submitButtonHBox = new HBox(submitButton);
submitButtonHBox.setAlignment(Pos.CENTER);

textArea = new TextArea();


textArea.setEditable(false);

BorderPane borderPane = new BorderPane();


borderPane.setTop(lunchComboBox);
borderPane.setCenter(textArea);
borderPane.setLeft(radioButtonsVBox);
borderPane.setBottom(submitButtonHBox);
Scene scene = new Scene(borderPane, 250, 240);

primaryStage.setScene(scene);
primaryStage.show();
}

private void calculateTotal()


{
String selectedLunchItem = lunchComboBox.getValue();
String selectedDrink = "";
double lunchPrice = 0.0;
double drinkPrice = 0.0;

if (selectedLunchItem != null)
{
if (selectedLunchItem.equals("Hot Dog"))
{
lunchPrice = 3.57;
}
else if (selectedLunchItem.equals("Sandwich"))
{
lunchPrice = 6.0;
}
else if (selectedLunchItem.equals("Hamburger"))
{
lunchPrice = 5.5;
}
}

RadioButton selectedRadioButton = (RadioButton)


drinkGroup.getSelectedToggle();
if (selectedRadioButton != null)
{
selectedDrink = selectedRadioButton.getText();
if (selectedDrink.equals("Coffee"))
{
drinkPrice = 1.44;
}
else if (selectedDrink.equals("Tea"))
{
drinkPrice = 1.20;
}
else if (selectedDrink.equals("Pop"))
{
drinkPrice = 1.77;
}
}

double total = lunchPrice + drinkPrice;


textArea.setText("Total: $" + total);
}

public static void main(String[] args)


{
launch(args);
}
}

You might also like