我用一些“预设”用户创建了一个登录系统。我想更改代码,以便能够在系统内部创建新用户并将其添加到我的用户列表中。我怎样才能做到这一点?我是Java的初学者,因此将不胜感激。
LoginSystem类:
package logpack;
import java.util.ArrayList;
import java.util.Scanner;
public class LoginSystem {
ArrayList<User> users = new ArrayList<User>();
public boolean run() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter username: ");
String inpUser = keyboard.nextLine();
System.out.println("Enter password: ");
String inpPass = keyboard.nextLine(); // gets input from user
boolean loginSuccess = false;
for (User temp : users) {
if (inpUser.equals(temp.getUsername()) && inpPass.equals(temp.getPassword())) {
System.out.println("Welcome " + temp.getName() + " to the System.");
loginSuccess = true;
break;
} else {
loginSuccess = false;
}
}
if (loginSuccess == false) {
System.out.println("Incorrect Input, Try again.");
}
return loginSuccess;
}
public void saveUser(User US) {
users.add(US);
}
public ArrayList<User> getUsers() {
return users;
}
}
用户类别:
package logpack;
public class User {
String name;
String username;
String password;
public User(String name, String username, String password) {
this.username = username;
this.password = password;
this.name = name;
}
protected String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
protected String getUsername() {
return username;
}
protected void setUsername (String username) {
this.username = username;
}
protected String getPassword() {
return password;
}
protected void setPassword (String password) {
this.password = password;
}
}
RunClass类(我必须更改以创建一种“注册”系统的类):
package logpack;
public class RunClass {
public static void main(String[] args) {
LoginSystem LGS = new LoginSystem();
System.out.println("Enter username: ");
////////
User u1 = new User("Daria", "Daria5", "12345");
User u2 = new User("Bob", "Bob8989 ", "Bob64748od7");
User u3 = new User("Samanta", "Sam764", "678303");
User u4 = new User("Dog", "Doggy998621", "C6574");
User u5 = new User("Doughnut", "DBghf", "pae102938");
LGS.saveUser(u1);
LGS.saveUser(u2);
LGS.saveUser(u3);
LGS.saveUser(u4);
LGS.saveUser(u5);
///////
/*Can you replace this section above with a loop and method that asks
* the user if they want to create a new user account? As many as they want?
* This is inefficient as we have to write many lines to create many users.
* It does mean that we always have users to start with to simulate
* having a save file.
*/
LGS.run();
//System.out.println("User list: "+LGS.users);
}
}
非常感谢您的帮助!
答案 0 :(得分:0)
在您的主类中,与其提示用户输入用户名,不如提示用户一系列选项,然后用户选择登录名或注册名,例如:
public static void main(String[] args) {
// create a scanner to read the command-line input
Scanner scanner = new Scanner(System.in);
int optionSelected;
do {
System.out.println("Select 1 for Login");
System.out.println("Select 2 for User Registration");
System.out.println("Select 3 to Exit");
System.out.print("Enter selected option: ");
optionSelected = scanner.nextInt();
if(optionSelected == 1){
// proceed with login
System.out.println("Login in progress\n");
} else if (optionSelected == 2){
// proceed with registration
System.out.println("Registration in progress\n");
}
} while (optionSelected != 3);
}
这不是世界上最好的代码,但是它将帮助您。目前只有3个选项,以后您可以添加更多选项。