A university has the following dormitories:
The university also offers the following meal plans:
Create an application with two combo boxes. One should hold the names of the dormitories, and the other should hold the meal plans. The user should select a dormitory and a meal plan, and the application should show the total charges for the semester. Answer with javaFX
// Java Program To Create A Dormitory And Meal Charges
Calculator
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.collections.*;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.text.Text.*;
import javafx.scene.paint.*;
import javafx.scene.text.*;
public class combo_box_2 extends Application {
// Launch the application
public void start(Stage stage)
{
// Set title for the stage
stage.setTitle("Dormitory And Meal
Charges Calculator");
// Dormitories
String dormitories[] =
{ "Allen Hall", "Pike Hall", "Farthing Hall",
"University Suites" };
// Creating a combo box to show
a list of dormitories
ComboBox combo_box_dormitories
=
new
ComboBox(FXCollections
.observableArrayList(dormitories));
// Meal Plans
String meal_plans[] =
{ "7 meals per week", "14 meals per week",
"Unlimited meals" };
// Creating a combo box to show
a list of meal plans
ComboBox combo_box_meal_plans
=
new
ComboBox(FXCollections
.observableArrayList(meal_plans));
// Label to display the total
charges for the semester
Label total_charges_label = new
Label("Total Charges = $0");
// Creating action event to
handle selection of different items in both lists
// and calculating charges according to the selections of the
user
EventHandler<ActionEvent>
event = new EventHandler<ActionEvent>() {
public void
handle(ActionEvent e)
{
int dormitory_charges = 0, meal_charges = 0, total_charges =
0;
//Calculating dormatory charges according to the current
selection
if(combo_box_dormitories.getValue()=="Allen Hall")
{
dormitory_charges = 1500;
}
else if(combo_box_dormitories.getValue()=="Pike Hall")
{
dormitory_charges = 1600;
}
else if(combo_box_dormitories.getValue()=="Farthing Hall")
{
dormitory_charges = 1200;
}
else if(combo_box_dormitories.getValue()=="University
Suites")
{
dormitory_charges = 1800;
}
//Calculating meal charges according to the current selection
if(combo_box_meal_plans.getValue()=="7 meals per week")
{
meal_charges = 560;
}
else if(combo_box_meal_plans.getValue()=="14 meals per week")
{
meal_charges = 1095;
}
else if(combo_box_meal_plans.getValue()=="Unlimited meals")
{
meal_charges = 1500;
}
//Calculating total charges as sum of both dormatory charges and
meal charges
total_charges = dormitory_charges + meal_charges;
//Displaying total charges on the "total_charges" label
total_charges_label.setText("Total Charges = $" +
Integer.toString(total_charges));
}
};
// Setting on action
combo_box_dormitories.setOnAction(event);
combo_box_meal_plans.setOnAction(event);
// Creating a tile pane
TilePane tile_pane = new
TilePane(combo_box_dormitories, combo_box_meal_plans,
total_charges_label);
// Creating a scene
Scene scene = new Scene(tile_pane,
350, 200);
// Setting the scene
stage.setScene(scene);
//Setting stage not to be resized
stage.setResizable(false);
stage.show();
}
public static void main(String args[])
{
// Launch the application
launch(args);
}
}
Output
A university has the following dormitories: Allen Hall: $1,500 per semester Pike Hall: $1,600 per semester...