Instructions
You will need to complete the following objectives as a Java Application: create a GUI interface
1. When the program starts the user needs to be asked if they want to make a new entry or to
view a previous entry
2. If the user wants to make a new entry, the first question will be how many meters they
travelled (this will then need to be converted into kilometers)
3. The second question will give the user 3 options to choose from, and each option will have a
value. The options are as follows:
a. Hatchback = 3
b. SUV = 3.5
c. Sports car = 4.
When the user has selected an option that options value needs to be multiplied by the
distance they travelled in kilometers
4. The third question will allow the user to enter a description, of where they travel to and why
did they travel there.
5. You need to make use of error handling to ensure that the user only types in items that are
asked and not anything else
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
private Scene mainScene;
private String previousResult = "none";
private String previousDescription = "none";
private StackPane getFirstPane() {
Button newEntryButton = new Button("New Entry");
newEntryButton.setOnMouseReleased((MouseEvent mouseEvent) -> {
if (mouseEvent.getButton() == MouseButton.PRIMARY) {
mainScene.setRoot(getSecondPane());
}
});
Button previousEntryButton = new Button("Previous Entry");
previousEntryButton.setOnMouseReleased((MouseEvent mouseEvent) -> {
if (mouseEvent.getButton() == MouseButton.PRIMARY) {
mainScene.setRoot(getFifthPane());
}
});
VBox vBox = new VBox(30, previousEntryButton, newEntryButton);
vBox.setAlignment(Pos.CENTER);
return new StackPane(vBox);
}
private StackPane getSecondPane() {
TextField metersTextField = new TextField();
metersTextField.setMaxWidth(100);
Button convertButton = new Button("Convert");
convertButton.setOnMouseReleased((MouseEvent mouseEvent) -> {
if (mouseEvent.getButton() == MouseButton.PRIMARY) {
try {
mainScene.setRoot(getThirdPane(Double.parseDouble(metersTextField.getText()) / 1000));
} catch (NumberFormatException e) {
}
}
});
VBox vBox = new VBox(30, metersTextField, convertButton);
vBox.setAlignment(Pos.CENTER);
return new StackPane(vBox);
}
private StackPane getThirdPane(double kilometers) {
Label kilometersLabel = new Label("Kilometers: " + kilometers);
ToggleGroup toggleGroup = new ToggleGroup();
RadioButton hatchbackRadioButton = new RadioButton("a. Hatchback = 3");
hatchbackRadioButton.setToggleGroup(toggleGroup);
hatchbackRadioButton.setSelected(true);
RadioButton suvRadioButton = new RadioButton("b. Hatchback = 3.5");
suvRadioButton.setToggleGroup(toggleGroup);
RadioButton sportsCarRadioButton = new RadioButton("c. Hatchback = 4");
sportsCarRadioButton.setToggleGroup(toggleGroup);
Button calculateButton = new Button("Calculate");
calculateButton.setOnMouseReleased((MouseEvent mouseEvent) -> {
if (mouseEvent.getButton() == MouseButton.PRIMARY) {
double multiplier;
if (toggleGroup.getSelectedToggle().equals(hatchbackRadioButton)) {
multiplier = 3;
} else if (toggleGroup.getSelectedToggle().equals(suvRadioButton)) {
multiplier = 3.5;
} else {
multiplier = 4;
}
mainScene.setRoot(getFourthPane(kilometers * multiplier));
}
});
VBox vBox = new VBox(10, kilometersLabel, hatchbackRadioButton, suvRadioButton,
sportsCarRadioButton, calculateButton);
vBox.setAlignment(Pos.CENTER);
return new StackPane(vBox);
}
private StackPane getFourthPane(double result) {
Label resultLabel = new Label("Result: " + result);
TextArea descriptionTextArea = new TextArea();
descriptionTextArea.setWrapText(true);
Button saveButton = new Button("Save");
saveButton.setOnMouseReleased((MouseEvent mouseEvent) -> {
if (mouseEvent.getButton() == MouseButton.PRIMARY) {
previousResult = Double.toString(result);
previousDescription = descriptionTextArea.getText();
}
});
Button backButton = new Button("Back");
backButton.setOnMouseReleased((MouseEvent mouseEvent) -> {
if (mouseEvent.getButton() == MouseButton.PRIMARY) {
mainScene.setRoot(getFirstPane());
}
});
HBox hBox = new HBox(20, saveButton, backButton);
hBox.setAlignment(Pos.CENTER);
VBox vBox = new VBox(20, resultLabel, descriptionTextArea, hBox);
vBox.setAlignment(Pos.CENTER);
return new StackPane(vBox);
}
private StackPane getFifthPane() {
Label infoLabel = new Label("Previous saved data:");
Label resultLabel = new Label("Result: " + previousResult);
Label descriptionLabel = new Label("Description:\n" + previousDescription);
Button backButton = new Button("Back");
backButton.setOnMouseReleased((MouseEvent mouseEvent) -> {
if (mouseEvent.getButton() == MouseButton.PRIMARY) {
mainScene.setRoot(getFirstPane());
}
});
VBox vBox = new VBox(15, infoLabel, resultLabel, descriptionLabel, backButton);
vBox.setAlignment(Pos.CENTER);
return new StackPane(vBox);
}
@Override
public void start(Stage primaryStage) throws Exception {
mainScene = new Scene(getFirstPane());
primaryStage.setScene(mainScene);
primaryStage.setWidth(400);
primaryStage.setHeight(300);
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Comments
Leave a comment