I need to create class employees with attributes: id, username, lastname, age, rank. Then use the JavaFX control to create a form for employee entry to one of the set collections
Then use the JavaFX to create form for employee entry to one of the set collections. The Set should not be added employees with the same id value. This check should be made through Comparable interface. So if user try to put employee with same id like one from already employee program need to show error message or if that is first user with that id program should show message that user is created succesfully.
So, I created form with JavaFX for add employees but I dont know how to put them in set collections and how to check Does anyone already have an id assigned to a new employee?
This is just code for form and some errors if user didnt put employee info. I dont know how to add employee in set collections and check does anyone alreday have an id assigned to a new employee
public void start(Stage primaryStage) throws Exception {
GridPane gridPane = createRegistrationFormPane();
addUIControls(gridPane);
Scene scene = new Scene(gridPane, 800, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
private GridPane createRegistrationFormPane() {
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
gridPane.setPadding(new Insets(40, 40, 40, 40));
gridPane.setHgap(10);
gridPane.setVgap(10);
ColumnConstraints columnOneConstraints = new ColumnConstraints(100, 100, Double.MAX_VALUE);
columnOneConstraints.setHalignment(HPos.RIGHT);
ColumnConstraints columnTwoConstrains = new ColumnConstraints(200, 200, Double.MAX_VALUE);
columnTwoConstrains.setHgrow(Priority.ALWAYS);
gridPane.getColumnConstraints().addAll(columnOneConstraints, columnTwoConstrains);
return gridPane;
}
private void addUIControls(GridPane gridPane) {
Label headerLabel = new Label("Dodavanje zaposlenog");
headerLabel.setFont(Font.font("Arial", FontWeight.BOLD, 24));
gridPane.add(headerLabel, 0, 0, 2, 1);
GridPane.setHalignment(headerLabel, HPos.CENTER);
GridPane.setMargin(headerLabel, new Insets(20, 0, 20, 0));
Label idLabel = new Label("ID: ");
gridPane.add(idLabel, 0, 1);
TextField idField = new TextField();
idField.setPrefHeight(40);
gridPane.add(idField, 1, 1);
Label imeLabel = new Label("Ime: ");
gridPane.add(imeLabel, 0, 2);
TextField imeField = new TextField();
imeField.setPrefHeight(40);
gridPane.add(imeField, 1, 2);
Label prezimeLabel = new Label("Prezime: ");
gridPane.add(prezimeLabel, 0, 3);
TextField prezimeField = new TextField();
prezimeField.setPrefHeight(40);
gridPane.add(prezimeField, 1, 3);
Label godineLabel = new Label("Godine: ");
gridPane.add(godineLabel, 0, 4);
TextField godineField = new TextField();
godineField.setPrefHeight(40);
gridPane.add(godineField, 1, 4);
Label zvanjeLabel = new Label("Zvanje: ");
gridPane.add(zvanjeLabel, 0, 5);
TextField zvanjeField = new TextField();
zvanjeField.setPrefHeight(40);
gridPane.add(zvanjeField, 1, 5);
Button submitButton = new Button("Dodaj");
submitButton.setPrefHeight(40);
submitButton.setDefaultButton(true);
submitButton.setPrefWidth(100);
gridPane.add(submitButton, 0, 6, 2, 1);
GridPane.setHalignment(submitButton, HPos.CENTER);
GridPane.setMargin(submitButton, new Insets(20, 0, 20, 0));
submitButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (idField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Greska", "Unesite ID zaposlenog");
return;
}
if (imeField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Greska", "Unesite ime zaposlenog");
return;
}
if (prezimeField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Greska", "Unesite prezime zaposlenog");
return;
}
if (godineField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Greska", "Unesite godine zaposlenog");
return;
}
if (zvanjeField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Greska", "Unesite zvanje zaposlenog");
return;
}
showAlert(Alert.AlertType.CONFIRMATION, gridPane.getScene().getWindow(), "Uspesno ste dodali zaposlenog!", "Njegov ID je: " + idField.getText());
}
});
}
private void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.initOwner(owner);
alert.show();
}
public static void main(String[] args) {
launch(args);
}
}