0

I've only started coding in java recently for a summer course at my uni so my code knowledge and code are subpar.

This assignment requires me take make a User class with a login method and accessors and mutators. A EmailSystem class that has a dynamic-sized collection of users, a registerUser method that will create a User object and add it to the list of users, and a listUser method that will iterate through the list of users.

Code for EmailSystem class:

public class EmailSystem {
   public static ArrayList<User> userList = new ArrayList<>();

   public static void registerUser(String firstname, String lastname, String username, String password) {
      User myUser = new User(firstname, lastname, username, password);
      userList.add(myUser);
      }

   public static void listUsers () {
      for (int i=0; i<userList.size(); i++)
      {
         System.out.println(userList.get(i));
      }
   }

   public static void main(String[] args) {
      registerUser("John", "Doe", "Username", "Password");
   }
}

Code for User class

import javax.swing.JOptionPane;
import java.util.*;

public class EmailSystem {
   public static ArrayList<User> userList = new ArrayList<>();

   public static void registerUser(String firstname, String lastname, String username, String password) {
      User myUser = new User(firstname, lastname, username, password);
      userList.add(myUser);
      }


   public static void listUsers () {
      for (int i=0; i<userList.size(); i++)
      {
         System.out.println(userList.get(i));
      }
   }

   public static void main(String[] args) {
      registerUser("John", "Doe", "Username", "Password");
      boolean hasExit = false;
      while (hasExit == false)
      {
         String options = "";
         options = JOptionPane.showInputDialog("The following actions are supported. Enter the code: \nA: Login\nB: Print Users\nC: Exit");
         if (options.equalsIgnoreCase ("A"))
         {
            String username = JOptionPane.showInputDialog("Username");
            String password = JOptionPane.showInputDialog("Password");
            // I would like to check whether the username and password submitted here are the same as the one used to register  
            // Use userLogin method here to compare the username and password that is inputted here and the username and password that was created when registering.
         }
         else if (options.equalsIgnoreCase ("B"))  
         {
            listUsers();
         }      
         else if (options.equalsIgnoreCase ("C"))
         {
            hasExit = true;
         }
      }  

   }
}

When registering a user how would I be able to run the the userLogin method on the created object? Or should I be doing something else entirely?

  • During registration, from where would you get `_username` and `_password`? Also, strings must be compared using `equals` and not `==` – Thiyagu Jul 05 '18 at 04:07
  • should registerUser method responsible to create a new user or add already created user into the list – noone Jul 05 '18 at 04:08
  • so you want to validate userLogin before the creation of User object? or you want to validate that User object after creation? @User – Ryuzaki L Jul 05 '18 at 04:11
  • I don't understand why this is marked as a duplicate. This question has absolutely nothing to do with comparing Strings... That may be a separate issue with the code presented, but the OP didn't ask anything about that. – Zephyr Jul 05 '18 at 04:11
  • yes, if i understand correctly this is about validating username and password, after the creation of object or at the time of object creation – Ryuzaki L Jul 05 '18 at 04:13
  • Yes, that's what I want to do, after creating the object I want to be able to validate in, if it were the main method I could do myUser.userLogin("...", "..."), but since it's not i'm not sure what to do. – IAteYourCat Jul 05 '18 at 04:17
  • Clearly not a duplicate. I'm reopening this. But the string comparison thing is still an issue, and I strongly recommend that IAteYourCat should read about string comparison in Java. – Dawood ibn Kareem Jul 05 '18 at 04:18
  • https://stackoverflow.com/q/513832 – Dawood ibn Kareem Jul 05 '18 at 04:19
  • okay let me get clear, what do you want to check exactly? username, password null check? or you want to call userLogin method in EmailSystem class by passing username and password @IAteYourCat – Ryuzaki L Jul 05 '18 at 04:28
  • if i understand correctly, after creating user object and want to check username, password using userLogin method, and if that returns true add that object to userList. am i right? @IAteYourCat – Ryuzaki L Jul 05 '18 at 04:34
  • @Deadpool, Partially, I want to create a user object, and then add that user object to userList. The userLogin method is to check whether or not an inputted username and password is equal to the one used to registered, I've updated my user Class so you can see what I'm trying to do – IAteYourCat Jul 05 '18 at 04:54
  • let me know still if it is not working – Ryuzaki L Jul 05 '18 at 05:42

2 Answers2

0

I think the userLogin method is more reasonable when it's in your EmailSystem class. And the userLogin method should iterate the userList to check if the login user is valid. Here is the code may like:

public class EmailSystem {
    public static ArrayList<User> userList = new ArrayList<>();

    public static void registerUser(String firstname, String lastname, String username, String password) {
        User myUser = new User(firstname, lastname, username, password);
        userList.add(myUser);
    }
    public static boolean userLogin(String _username, String _password) {
        boolean loginValid;
        User user = userList.stream().filter(o -> {
            if (o.getUser().equals(_username) && o.getPassword().equals(_password)) {
                return true;
            }
            return false;
        }).findFirst().orElse(null);
        if (user!=null)
            loginValid = true;
        else
            loginValid = false;
        return loginValid;
    }

    public static void listUsers () {
        for (int i=0; i<userList.size(); i++)
        {
            System.out.println(userList.get(i));
        }
    }

    public static void main(String[] args) {
        registerUser("John", "Doe", "Username", "Password");
        boolean b = userLogin("Username", "Password");
        System.out.println(b);
    }
}

and the user class:

public class User {
    private String firstname;
    private String lastname;
    private String username;
    private String password;

    public User(String _firstname, String _lastname, String _username, String _password) {
        firstname = _firstname;
        lastname = _lastname;
        username = _username;
        password = _password;
    }
    public String getUser() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String printName() {
        return firstname;
    }
    public void setUser(String sUser, String sPass) {
        username = sUser;
        password = sPass;
    }
    public void setName(String fName, String lName) {
        firstname = fName;
        lastname = lName;
    }
}
Jswq
  • 758
  • 1
  • 7
  • 23
0

For the registerUser method change return type to User

 public static User registerUser(String firstname, String lastname, String 
  username, String password) {
      User myUser = new User(firstname, lastname, username, password);
      userList.add(myUser);
      return myUser;
      }

And for validation of username and password you check below logic, or you can write below logic in userValidate method and call that method if options = "A"

public static void main(String[] args) {
     User myuser= registerUser("John", "Doe", "Username", "Password");
      boolean hasExit = true;
      while (hasExit)
      {
         String options = "";
         options = JOptionPane.showInputDialog("The following actions are 
   supported. Enter the code: \nA: Login\nB: Print Users\nC: Exit");
         if (options.equalsIgnoreCase ("A"))
         {
            String username = JOptionPane.showInputDialog("Username");
            String password = JOptionPane.showInputDialog("Password");
            // I would like to check whether the username and password 
      // submitted here are the same as the one used to register  
            // Use userLogin method here to compare the username and password 
    //that is inputted here and the username and password that was created 
    //when registering
            if(myuser.getUsername().equals(username) && 
      myuser.getPassword.equals(password)) { 
                // this is for only created user object by using registerUser 
        // method
                // if you want to check in complete list need to iterate list 
            // here, by using for each or streams 
                System.out.println("user existed or success ");
                hasExit = false;
            }else {
                System.out.println("failed");
                hasExit = false;
            }
         }
         else if (options.equalsIgnoreCase ("B"))  
         {
            listUsers();
           hasExit = false;
         }      
         else if (options.equalsIgnoreCase ("C"))
         {
            hasExit = false;
         }
      }  

   }

But still you can make this code more clean, and also you can write switch case instead of while loop

Recommended approach writing logic inside of method and calling that method in main

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98