On the form create a list box, to allow a user to select between three different cities. Also create a combo box for the user to select the year. Finally add a submit button. Once the user has selected a city, year and clicked the submit button display the petrol price per litre for that year.
Use the following table for the city and yearly petrol prices per litre:
2017 2018 2019
Johannesburg 10.72 10.35 10.20
Durban 12.75 12.32 12.22
Cape Town 13.70 13.31 13.23
You are also required to create a menu system which will allow the user to exit the application under the file menu. Under the tools menu allow the form submission and option to display the average yearly petrol price report. The layout of the form is left to your discretion. Marks will be allocated to the presentation and effectiveness of the layout, but the following layout is displayed for your convenience
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
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.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
double[][] prices = {{10.72, 12.75, 13.70}, {10.35, 12.32, 13.31}, {10.20, 12.22, 13.23}};
ListView<String> citiesListView = new ListView<>(FXCollections.observableArrayList("Johannesburg", "Durban", "Cape Town"));
citiesListView.setMaxSize(200, 100);
ComboBox<String> yearsComboBox = new ComboBox<>(FXCollections.observableArrayList("2017", "2018", "2019"));
TextField priceTextField = new TextField();
priceTextField.setMaxSize(200, 20);
priceTextField.setEditable(false);
Button submitButton = new Button("Submit");
submitButton.setOnMouseReleased((MouseEvent me) -> {
if (me.getButton() == MouseButton.PRIMARY) {
int cityIndex = citiesListView.getSelectionModel().getSelectedIndex();
int yearIndex = yearsComboBox.getSelectionModel().getSelectedIndex();
if (cityIndex != -1 && yearIndex != -1) {
priceTextField.setText(Double.toString(prices[cityIndex][yearIndex]));
}
}
});
MenuItem exitMenuItem = new MenuItem("Exit");
exitMenuItem.setOnAction((ActionEvent event) -> {
System.exit(0);
});
MenuItem submissionMenuItem = new MenuItem("Submission");
submissionMenuItem.setOnAction((ActionEvent event) -> {
});
MenuItem priceReportMenuItem = new MenuItem("Price Report");
priceReportMenuItem.setOnAction((ActionEvent event) -> {
StringBuilder builder = new StringBuilder();
String[] years = {"2017", "2018", "2019"};
double[] average = new double[3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
average[i] += prices[i][j];
}
average[i] /= 3;
builder.append(years[i]).append(" ").append(String.format("%.2f", average[i])).append('\n');
}
new Alert(Alert.AlertType.INFORMATION, builder.toString(), ButtonType.OK).show();
});
Menu fileMenu = new Menu("File");
fileMenu.getItems().add(exitMenuItem);
Menu toolsMenu = new Menu("Tools");
toolsMenu.getItems().addAll(submissionMenuItem, priceReportMenuItem);
MenuBar menuBar = new MenuBar(fileMenu, toolsMenu);
VBox comboBoxesVBox = new VBox(10, citiesListView, yearsComboBox, priceTextField, submitButton);
comboBoxesVBox.setAlignment(Pos.CENTER);
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(comboBoxesVBox);
primaryStage.setScene(new Scene(borderPane, 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Comments
Leave a comment