-1

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);
}
}
  • The easiest way to do this that I am aware of is have a static id variable that is incremented by one every time an employee is made – Mitch Jun 19 '19 at 17:17
  • No, I set employee ID so if I put ID that is alreday created program need to show error message because that ID is taken. So I created form for add employees but idk how to enter the employees in the set collection and how to check if ID is taken or not. Edit: This is just example so user put employee ID and if user put ID that is already taken program need to show error message. Thats it. – Marc Estons Jun 19 '19 at 17:23
  • Why would a user enter an employee id? Id's should be generated using specific generators like if you are storing your data in a database, your database can generate a unique id every time. – Miss Chanandler Bong Jun 19 '19 at 17:24
  • This is just example man. As I said, I created a form for add employee and user who add employee put ID for them – Marc Estons Jun 19 '19 at 17:26

1 Answers1

0

First you need a new class called Employee for the employees you have. This class has the fields of an employee and the getter and setter to use them. It will look similar to this:

public class Employee {
    private int ID;
    private String username;
    // ...

    /* getter and setter here */
}

Then you need a Set to add your Employee objects. However, implementations of the Set interface usually use the equals() method to check if the Set already contains the object you want to add. But since your assignment says you should use one which uses the Comparable interface to compare the objects, you have to use the TreeSet class. But first your Employee class needs to implement the Comparable interface. Its only method compareTo() will use the ID field to order/sort two Employee objects. The implementation can look like this:

public class Employee implements Comparable<Employee> {
    //
    // ...
    //

    @Override
    public int compareTo(Employee o) {
        return this.ID - o.ID;
    } 
}

Now you can create a TreeSet object and add objects to it. You also use contains() to see if there is already an employee with the same id.

Employee e1 = new Employee();
e1.setID(1);
e1.setUsername("foobar");

Employee e2 = new Employee();
e2.setID(1);                    // same ID
e2.setUsername("blabli");

Employee e3 = new Employee();
e3.setID(1);                    // also same ID
e3.setUsername("johndoe");

Set<Employee> employees = new TreeSet<Employee>();
employees.add(e1);
employees.add(e2);
employees.add(e3);

Even though it looks like we have added three employees, the created Set will only contain one object. By using contains() you can check if there is already an Employee object inside the Set.

System.out.println(employees.size());         // 1

System.out.println(employees.contains(e1));   // true
System.out.println(employees.contains(e2));   // true
System.out.println(employees.contains(e3));   // true

Even as we have three different Employee objects with different usernames the TreeSet consider them all equal because of the way the compareTo() method is implemented.

Progman
  • 16,827
  • 6
  • 33
  • 48
  • Thank you for this explanation, but I have problem with Implementation. And also, as you can see I created form for add new employee, so I dont understand how to take inputs from form, you sent me example `e3.setID(1);` but I want to add employee throught frm – Marc Estons Jun 19 '19 at 21:36
  • @MarcEstons You can get the values from the inputs you have by calling the [`getText()`](https://docs.oracle.com/javafx/2/api/javafx/scene/control/TextInputControl.html#getText()) method on your controls. With these values you can fill the `Employee` object. – Progman Jun 19 '19 at 21:44
  • I can't make it, I just got getClass... `e1.getNameField;` – Marc Estons Jun 19 '19 at 21:50
  • @MarcEstons You use some code like `idField.getText()`, just like you have already written in your code, to get the text the user has added to the text fields. With these texts/strings you can build your `Employee` object and call the `set...();` methods. – Progman Jun 19 '19 at 21:52
  • Umm okey, just help me with implementation, because in that class program cant find symbol `id` – Marc Estons Jun 19 '19 at 21:54
  • @MarcEstons For the error "Can't find symbol" please check https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean. You might want to read some tutorials, documentations or bocks for the basics of java. – Progman Jun 19 '19 at 22:05
  • I know what that mean, its just weird because I have id – Marc Estons Jun 19 '19 at 22:06