Introduction
The DormMealPlanCalculator is a JavaFX project that allows students to select a dormitory and meal plan to calculate their total charges. The project uses JavaFX properties, ComboBoxes, and bindings to achieve its functionality.
About the Code
Firstly, the program creates JavaFX properties to hold the selected dormitory, meal plan, and total charges. These properties are then bound to the corresponding ComboBoxes, which allow the user to select a dormitory and meal plan from a pre-defined list.
// create JavaFX properties to hold the selected dormitory, meal plan, and total charges
StringProperty selectedDormitory = new SimpleStringProperty();
StringProperty selectedMealPlan = new SimpleStringProperty();
DoubleProperty totalCharges = new SimpleDoubleProperty();
ComboBox<String> dormitoryComboBox = new ComboBox<>();
dormitoryComboBox.getItems().addAll("Allen Hall $1,800", "Pike Hall $2,200", "Farthing Hall $2,800", "University Suites $3,000"); dormitoryComboBox.valueProperty().bindBidirectional(selectedDormitory);
Next, the program creates bindings to calculate the total charges based on the selected dormitory and meal plan. The bindings use a series of when-otherwise statements to determine the charge for each dormitory and meal plan option. The charges are then added together to calculate the total charges.
....
DoubleBinding mealPlanCharge = (DoubleBinding) Bindings.when(selectedMealPlan.isEqualTo("7 meals per week $600"))
.then(600.0)
.otherwise(Bindings.when(selectedMealPlan.isEqualTo("14 meals per week $1,100"))
.then(1100.0)
.otherwise(Bindings.when(selectedMealPlan.isEqualTo("Unlimited meals $1,800"))
.then(1800.0)
.otherwise(0)));
totalCharges.bind(dormitoryCharge.add(mealPlanCharge));
Finally, the program creates a label to display the total charges and a VBox layout to organize the ComboBoxes and Labels. The program also sets a scene with the layout and applies a stylesheet for styling purposes.
// create a layout for the controls and labels
VBox root = new VBox(15, headingLabel, headingLabel2, dormitoryComboBox, mealPlanComboBox, totalChargesLabel);
See my Codes
See the App in Action
Summary
In summary, the DormMealPlanCalculator is a JavaFX project that allows students to easily calculate their dormitory and meal plan charges. The project uses JavaFX properties, ComboBoxes, and bindings to achieve its functionality, and provides a user-friendly interface with a clean and organized layout.
Reference
T. Gaddis and G. Muganda, Starting Out with Java – From Control Structures through Data Structures, 4th Edition, 330 Hudson Street, NY NY 10013: Pearson, 2019.