Images of Output
Code
/* ----------------------------------------------------------------------------
* This program accepts user selections of a data plan, phone, and extra
* options and return the prices of the items selected.
* ----------------------------------------------------------------------------
* Inputs: Selected items.
* Processing: Get selected items, assign value to each.
* Outputs: Displayed prices.
*/
package jennings_smartphonepackages;
/*-----------------------
* Anna Jennings *
* COSC 2336-101CL *
* February 21, 2019 *
*-----------------------
*/
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.MenuBar;
import javafx.scene.control.Menu;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
public class Jennings_SmartphonePackages extends Application
{
private MenuBar menuBar;
private Menu phoneMenu;
private Menu dataMenu;
private Menu extrasMenu;
private RadioMenuItem model100;
private RadioMenuItem model110;
private RadioMenuItem model200;
private RadioMenuItem gig8;
private RadioMenuItem gig16;
private RadioMenuItem gig20;
private CheckMenuItem insurance;
private CheckMenuItem hotspot;
private Label dataCost;
private Label phoneCost;
private Label extrasCost;
private double insuranceCost;
private double hotspotCost;
private double totalExtrasCost;
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage appStage)
{
Label instructionLabel = new Label("Select a phone, data plan, and "
+ "extras from the menus above.");
instructionLabel.setId("instructionLabel");
MenuBar menuBar = new MenuBar();
Menu phoneMenu = new Menu("Phones");
RadioMenuItem model100 = new RadioMenuItem("Model 100");
RadioMenuItem model110 = new RadioMenuItem("Model 110");
RadioMenuItem model200 = new RadioMenuItem("Model 200");
ToggleGroup phoneItems = new ToggleGroup();
model100.setToggleGroup(phoneItems);
model110.setToggleGroup(phoneItems);
model200.setToggleGroup(phoneItems);
phoneMenu.getItems().addAll(model100, model110, model200);
Menu dataMenu = new Menu("Data Plans");
RadioMenuItem gig8 = new RadioMenuItem("8 GB/month");
RadioMenuItem gig16 = new RadioMenuItem("16 GB/month");
RadioMenuItem gig20 = new RadioMenuItem("20 GB/month");
ToggleGroup dataItems = new ToggleGroup();
gig8.setToggleGroup(dataItems);
gig16.setToggleGroup(dataItems);
gig20.setToggleGroup(dataItems);
dataMenu.getItems().addAll(gig8, gig16, gig20);
Menu extrasMenu = new Menu("Extras");
CheckMenuItem insurance = new CheckMenuItem("Phone Replacement Insurance");
CheckMenuItem hotspot = new CheckMenuItem("WiFi Hotspot Capability");
extrasMenu.getItems().addAll(insurance, hotspot);
menuBar.getMenus().addAll(phoneMenu, dataMenu, extrasMenu);
Label phoneCostLabel = new Label("Phone Cost:");
Label dataCostLabel = new Label("Data Cost:");
Label extrasCostLabel = new Label("Extras Cost:");
VBox itemizedLabels = new VBox(phoneCostLabel, dataCostLabel, extrasCostLabel);
itemizedLabels.setPrefWidth(150);
itemizedLabels.setStyle("-fx-font-weight: bold; -fx-text-fill: white;");
Label phoneCost = new Label("$ 0.00 per month");
Label dataCost = new Label("$ 0.00 per month");
Label extrasCost = new Label("$ 0.00 per month");
VBox itemizedCosts = new VBox(phoneCost, dataCost, extrasCost);
itemizedCosts.setStyle("-fx-text-fill: white;");
HBox hbox = new HBox(itemizedLabels, itemizedCosts);
hbox.setPadding(new Insets(10, 0, 0, 0));
VBox mainArea = new VBox(instructionLabel, hbox);
mainArea.setStyle("-fx-background-color: azure;");
mainArea.setPadding(new Insets(15, 0, 15, 20));
model100.setOnAction(event ->
{
phoneCost.setText("$ 299.95");
});
model110.setOnAction(event ->
{
phoneCost.setText("$ 399.95");
});
model200.setOnAction(event ->
{
phoneCost.setText("$ 499.95");
});
gig8.setOnAction(event ->
{
dataCost.setText("$ 45.00 per month");
});
gig16.setOnAction(event ->
{
dataCost.setText("$ 65.00 per month");
});
gig20.setOnAction(event ->
{
dataCost.setText("$ 99.00 per month");
});
totalExtrasCost = 0.00;
insurance.setOnAction(event ->
{
if (insurance.isSelected())
{
insuranceCost = 5.00;
totalExtrasCost += insuranceCost;
extrasCost.setText(String.format("$ %.2f",totalExtrasCost) + " per month");
} else {
totalExtrasCost -= 5.00;
extrasCost.setText(String.format("$ %.2f",totalExtrasCost) + " per month");
}
});
hotspot.setOnAction(event ->
{
if (hotspot.isSelected())
{
hotspotCost = 10.00;
totalExtrasCost += hotspotCost;
extrasCost.setText(String.format("$ %.2f",totalExtrasCost) +
" per month");
} else {
totalExtrasCost -= 10.00;
extrasCost.setText(String.format("$ %.2f",totalExtrasCost) +
" per month");
}
});
Stage window = appStage;
VBox vbox = new VBox(menuBar, mainArea);
vbox.setStyle("-fx-background-color: rgb(225,237,225)");
vbox.setAlignment(Pos.BOTTOM_CENTER);
Scene scene = new Scene(vbox);
scene.getStylesheets().add("smartphonepackages.css");
appStage.setMinHeight(182);
appStage.setMaxHeight(182);
appStage.setMinWidth(420);
appStage.setMaxWidth(420);
appStage.setScene(scene);
appStage.setTitle("Cell Solutions");
appStage.show();
}
}
/* smartphonepackages.css */
.root {
-fx-font-family: Tahoma;
}
.vbox {
-fx-alignment: center;
-fx-spacing: 10px;
-fx-background-color:#e0ffff;
}
.menu-bar {
-fx-background-color: rgb(225,237,225);
-fx-border-style: none none solid none;
-fx-border-width: 0px 0px 1px 0px;
-fx-border-color: mediumaquamarine;
}
.menu {
-fx-background-color: white;
-fx-border-color: mediumaquamarine;
-fx-font-size: 9pt;
-fx-border-width: 1px 1px 0px 1px;
}
.menu:hover {
-fx-background-color: mediumaquamarine;
-fx-border-color: mediumaquamarine;
-fx-font-size: 9pt
}
.menu-item:hover, .menu-item:focused{
-fx-background-color: mediumaquamarine;
}
.menu:showing {
-fx-background-color: mediumaquamarine;
}
.label {
-fx-font-size: 10pt;
}
#instructionLabel {
-fx-font-weight: bold;
-fx-text-fill: teal;
-fx-font-size: 9pt;
}