0

I'm trying to make a login function in java swing with functional programming style, below is my code

String username = txtUsername.getText();
        String passwordStr = String.valueOf(txtPassword.getPassword());
        User login = new User(username, passwordStr);

        Optional<User> adminUser = Optional.of(new User("a", "a"));
        Optional<User> user = Optional.ofNullable(login);

        adminUser.filter(u -> u.equals(login))
                .ifPresentOrElse(
                        u -> {
                            this.setVisible(false);
                            HomePageAdmin hma = new HomePageAdmin();
                            hma.setVisible(true);
                        },
                        () -> user.filter(u -> u.login(passwordStr))
                                .ifPresentOrElse(
                                        u -> {
                                            this.setVisible(false);
                                            HomePagePeople hmp = new HomePagePeople(username);
                                            hmp.setVisible(true);
                                        },
                                        () -> JOptionPane.showMessageDialog(null, "Wrong Credential")
                                )
                );

everything is working fine, if I try to login with credentials other than "a" as username and "a" as password, however when I try to log in to the system with those credentials, it gives the "wrong credential" Pane as seen below

enter image description here

I also tried various ways with the filter function above with this alternatives

        User login = new User(username, passwordStr);
        User loginAdmin = new User("a", "a");

        Optional<User> adminUser = Optional.of(loginAdmin);
        Optional<User> user = Optional.ofNullable(login);

        adminUser.filter(u -> u.equals(login))

the code above give the same error and won't accept the logic. However, if I try to change the adminUser.filter(u -> u.equals(login)) to adminUser.filter(u -> u.equals(loginAdmin)), it works just fine, however this logic is wrong as the adminUser will always be equal to loginAdmin thus inputting anything into the textbox will allow user to log in to the admin page as seen in example below

enter image description here

enter image description here

any help is greatly Appreciated

Virist
  • 1
  • 5
  • 2
    Please show your `User` class. In particular, what does `equals` do? – tgdavies Mar 20 '23 at 20:50
  • equals is esentially `==`, well I also tried using `==` but still gives the same result oh and this is actually one of my assignment in uni to change the code to functional/declarative and it's working just fine before, so I can certainly say that there's nothing wrong in my `User` class – Virist Mar 20 '23 at 20:59
  • See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java/513839#513839 – tgdavies Mar 20 '23 at 21:01
  • I tried to use the `==` already and it still gives error btw, oh and I used the `equals` for this `adminUser.filter(u -> u.equals(loginAdmin))` and it works just fine as seen in the explanation above, but the logic is just incorrect – Virist Mar 20 '23 at 21:06
  • Please edit your question to show your User class. In particular, what does equals do? – tgdavies Mar 20 '23 at 21:07
  • equals is esentially ==, did you not see this comment? – Virist Mar 20 '23 at 21:10
  • If the `User` class doesn't override `equals()` (i.e. it is the same as `==`) then it is no wonder that your code doesn't work for the admin user. `new User(username, passwordStr)` and `new User("a", "a")` are two different user objects and according to `==` they are **never** the same object. In Java there is no way that `new User(username, passwordStr) == new User("a", "a")` will be true. – Thomas Kläger Mar 20 '23 at 21:32
  • yes, I just found out that it is not the same object as it will keep on creating new object, I tried to print out the `adminUser` and get this result `Optional[vaccinationsystem.User@7d1dd2ca]` meanwhile the `loginAdmin` give out this result `vaccinationsystem.User@47ffd8e4` are there any way to fix this problem? or any other way to get the `"a", "a"` string – Virist Mar 20 '23 at 21:43
  • 1
    The solution is simple: properly implement an `equals()` method in your `User` class – Thomas Kläger Mar 20 '23 at 22:10
  • and how can you do that? here's the link to my User class for you to check [User](https://github.com/richardsnj/User-class.git) @tgdavies – Virist Mar 21 '23 at 04:25
  • Your class has no equals method, so it is using `Object.equals()` which uses object identity. You need to implement `equals` and compare the username and the password fields. – tgdavies Mar 21 '23 at 04:54

1 Answers1

0

I fixed my problem by implementing the same logic with the user.filter(u -> u.login(passwordStr)) code by creating a new method in my User class called loginAdmin as seen below

public boolean loginAdmin(String name, String pw) {

        //file to be scanned 
        String Temp = "a";
        String Temp2 = "a";
        //returns true if there is another line to read  

        if (Temp.equals(Username) && Temp2.equals(Password)) {
            return true;
        } else {
            return false;

        }
    }

this fix the problem and is implemented to the login class and here's the updated code

String Username = txtUsername.getText();
        String Password = String.valueOf(txtPassword.getPassword());
        User login = new User(Username, Password);

        Optional<User> adminUser = Optional.of(login);
        Optional<User> user = Optional.ofNullable(login);

        adminUser.filter(u -> u.loginAdmin(Username, Password))
                .ifPresentOrElse(
                        u -> {
                            this.setVisible(false);
                            HomePageAdmin hma = new HomePageAdmin();
                            hma.setVisible(true);
                        },
                        () -> user.filter(u -> u.login(Password))
                                .ifPresentOrElse(
                                        u -> {
                                            this.setVisible(false);
                                            HomePagePeople hmp = new HomePagePeople(Username);
                                            hmp.setVisible(true);
                                        },
                                        ()
                                        -> JOptionPane.showMessageDialog(null, "Wrong Credential")
                                )
                );

Thanks everyone to help me solve this issue

Virist
  • 1
  • 5