我最近才开始在uni的暑期课程中使用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方法?还是我应该完全做其他事情?
答案 0 :(得分:0)
我认为 userLogin 方法在您的 EmailSystem 类中更为合理。并且userLogin方法应迭代userList以检查登录用户是否有效。代码可能像这样:
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);
}
}
和用户类别:
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;
}
}
答案 1 :(得分:0)
对于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;
}
}
}
但是您仍然可以使此代码更简洁,也可以编写开关大小写而不是while循环
推荐的方法是在方法内部编写逻辑并在main中调用该方法