我最近才开始在我的大学的暑期课程中使用 Java 编码,所以我的代码知识和代码都低于标准。
这个作业要求我创建一个带有登录方法以及访问器和修改器的 User 类。一个具有动态大小的用户集合的 EmailSystem 类,一个将创建 User 对象并将其添加到用户列表的 registerUser 方法,以及一个将遍历用户列表的 listUser 方法。
EmailSystem 类的代码:
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");
}
}
用户类代码
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;
}
}
}
}
注册用户时,如何在创建的对象上运行 userLogin 方法?还是我应该完全做其他事情?
对于 registerUser 方法,将返回类型更改为 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;
}
对于用户名和密码的验证,您检查以下逻辑,或者您可以在 userValidate 方法中编写以下逻辑并在 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;
}
}
}
但是你仍然可以让这段代码更干净,你也可以编写 switch case 而不是 while 循环
推荐的方法在方法内部编写逻辑并在 main 中调用该方法
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句