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?