2 回答
TA贡献1828条经验 获得超3个赞
class UserData {
private final String username;
private final String password;
UserData(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
class Registration {
private final List<UserData> userData = new ArrayList<>();
public void regUser() {
String regName = JOptionPane.showInputDialog(null, "Enter User Name:");
String regPass = JOptionPane.showInputDialog(null, "Enter User Password:");
userData.add(new UserData(regName, regPass));
}
public void login() {
String name = JOptionPane.showInputDialog(null, "Enter User Name:");
String password = JOptionPane.showInputDialog(null, "Enter User Password:");
boolean isValidUser = userData.stream()
.anyMatch(user -> user.getUsername().equals(name) && user.getPassword().equals(password));
if (isValidUser) {
JOptionPane.showMessageDialog(null, "Welcome to Rent A Car\n All cars data here....");
} else {
JOptionPane.showMessageDialog(null, "Wrong login info please try again");
}
}
}
TA贡献1772条经验 获得超6个赞
除了代码质量差之外,问题还在于:
您的名称为“name1”,密码为“pass1”
,您检查它是否与每个注册用户相同。
您可能想使用 List 接口的 contains() 方法
,因此如果用户名和密码在列表中,则写入成功消息,否则失败。
添加回答
举报