创建普通文本文件存储登录名、姓名、密码以及注册时间,实现用户的登录、注册功能。提示:注意使用面向对象编程,设计合理的类来解决。1)用户登录,总共有3次登录机会,如果都失败则自动退出系统2)登录成功3)用户注册
public class Test {
public static void main(String[] args) {
UserSystem system = new UserSystem();
while (true) {
system.printChoice();
}
}
}
class UserSystem {
Scanner scanner;
public UserSystem() {
scanner = new Scanner(System.in);
}
public void printChoice() {
System.out.println("**********用户管理系统**********");
System.out.println("* 1、用户登录 *");
System.out.println("* 2、用户注册 *");
System.out.println("* 3、退出系统 *");
System.out.print("请选择:");
switch (scanner.nextInt()) {
case 1:
if (login() == null) System.exit(0);
break;
case 2:
register();
break;
case 3:
System.exit(0);
break;
default:
System.out.println("错误选项!");
}
}
private User login() {
int loginTimes = 0;
String username, pwd;
do {
System.out.print("请输入用户名:");
username = scanner.next();
System.out.print("请输入密码:");
pwd = scanner.next();
UserManager.LoginResult loginResult = UserManager.canLogin(username, pwd);
switch (loginResult.loginResult) {
case UserManager.SUCCESS:
loginResult.user.printInfo();
return loginResult.user;
case UserManager.ERROR_CODE_USERNAME:
System.out.println("登录失败!用户名不存在,请重写登录");
break;
case UserManager.ERROR_CODE_PWD:
System.out.println("登录失败!密码不正确,请重写登录");
break;
}
loginTimes++;
} while (loginTimes < 3);
return null;
}
private void register() {
String username, pwd, nickname;
System.out.print("请输入用户名:");
username = scanner.next();
System.out.print("请输入密码:");
pwd = scanner.next();
System.out.print("请输入确认密码:");
if (!pwd.equals(scanner.next())) {
System.out.println("密码与确认密码不符!");
return;
}
System.out.print("请输入姓名:");
nickname = scanner.next();
UserManager.LoginResult loginResult = UserManager.canLogin(username, pwd);
if (loginResult.loginResult != UserManager.ERROR_CODE_USERNAME) {
System.out.println("注册失败!该用户已存在,请重新填写!");
return;
}
UserManager.saveToDb(username, pwd, nickname);
}
}
class UserManager {
private static final String DB_NAME = "d:/user.txt";
public static final int SUCCESS = 1;
public static final int ERROR_CODE_USERNAME = -1;
public static final int ERROR_CODE_PWD = -2;
public static LoginResult canLogin(String username, String pwd) {
try {
Scanner scanner = new Scanner(new File(DB_NAME));
while (scanner.hasNextLine()) {
String userRecordString = scanner.nextLine();
if (userRecordString.equals("")) continue;
User userRecord = new User(userRecordString);
int result = userRecord.canLogin(username, pwd);
if (result == SUCCESS) {
return new LoginResult(SUCCESS, userRecord);
} else if (result == ERROR_CODE_PWD) {
return new LoginResult(ERROR_CODE_PWD);
}
}
} catch (Exception e) {
// System.out.println("登录失败!用户名不存在,请重新填写");
}
return new LoginResult(ERROR_CODE_USERNAME);
}
public static void saveToDb(String username, String pwd, String nickname) {
File file = new File(DB_NAME);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
FileWriter fileWriter = new FileWriter(file, true);
fileWriter.append('\n');
fileWriter.append(new User(username, pwd, nickname).getRecordString());
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static class LoginResult {
public int loginResult;
public User user;
public LoginResult(int loginResult) {
this.loginResult = loginResult;
}
public LoginResult(int loginResult, User user) {
this.loginResult = loginResult;
this.user = user;
}
}
}
class User {
private String username;
private String pwd;
private String nickname;
private String registerTime;
public User(String username, String pwd, String nickname) {
this.username = username;
this.pwd = pwd;
this.nickname = nickname;
this.registerTime = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(System.currentTimeMillis());
}
public User(String recordLine) {
String[] userItem = recordLine.split(" ");
username = userItem[0];
pwd = userItem[1];
nickname = userItem[2];
registerTime = userItem[3] + " " + userItem[4];
}
public int canLogin(String username, String pwd) {
if (!username.equals(this.username)) {
return UserManager.ERROR_CODE_USERNAME;
}
if (!pwd.equals(this.pwd)) {
return UserManager.ERROR_CODE_PWD;
}
return UserManager.SUCCESS;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public void printInfo() {
System.out.println(String.format("登录名:%s,姓名:%s,密码:%s,注册日期:%s", username, nickname, pwd, registerTime));
}
public String getRecordString() {
return String.format("%s %s %s %s", username, pwd, nickname, registerTime);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2018-05-13
帮你用Java写了一个
import java.io.*;import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyClass extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
//声明控件
JLabel nameLabel = new JLabel("用户名");
JLabel passwordLabel = new JLabel("密码");
JTextField name = new JTextField();
JPasswordField password = new JPasswordField();
JButton register = new JButton("注册");
JButton login = new JButton("登录");
JPanel form = new JPanel();
int count = 3;//登录失败的次数
public static void main(String[] args) {
new MyClass();
}
//布局
public MyClass() {
setTitle("登录界面");
setBounds(100, 100, 300, 210);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
form.setLayout(null);
form.setBackground(Color.white);
nameLabel.setBounds(60,40,100,20);
name.setBounds(120,40,100,20);
passwordLabel.setBounds(60,70,100,20);
password.setBounds(120,70,100,20);
register.setBounds(60,100,60,20);
login.setBounds(160,100,60,20);
form.add(nameLabel);
form.add(name);
form.add(passwordLabel);
form.add(password);
form.add(register);
form.add(login);
add(form);
register.setActionCommand("register");
login.setActionCommand("login");
register.addActionListener(this);
login.addActionListener(this);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
File userFile = new File("用户信息.txt");
ArrayList<User> userList = new ArrayList<>();
//取出文件
try {
FileInputStream in = new FileInputStream(userFile);
InputStreamReader r = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(r);
String line = "";
while((line = reader.readLine()) != null) {
User user = new User();
user.setName(line);
user.setPassword(reader.readLine());
user.setTime(reader.readLine());
userList.add(user);
}
reader.close();
}
catch(IOException exception) {
System.out.println("注册失败");
}
//获取用户信息
User userEdit = new User();
userEdit.setName(name.getText());
userEdit.setPassword(new String(password.getPassword()));
userEdit.setTime(String.format("%tF %tT", new Date(), new Date()));
//判断是否为空
if(userEdit.getName().equals("")) {
JOptionPane.showMessageDialog(null, "请输入用户名");
return;
}
if(userEdit.getPassword().equals("")) {
JOptionPane.showMessageDialog(null, "请输入密码");
return;
}
switch(e.getActionCommand()) {
//注册
case "register":
userList.add(userEdit);
//存入文件
try {
FileOutputStream out = new FileOutputStream(userFile);
OutputStreamWriter w = new OutputStreamWriter(out);
BufferedWriter writer = new BufferedWriter(w);
for(User user: userList) {
writer.write(user.getName() + "\r\n");
writer.write(user.getPassword() + "\r\n");
writer.write(user.getTime() + "\r\n");
}
writer.close();
JOptionPane.showMessageDialog(null, "注册成功");
name.setText("");
password.setText("");
}
catch(IOException exception) {
System.out.println("注册失败");
}
break;
//登录
case "login":
boolean state = false;//登录状态
for(User user: userList) {
if(user.getName().equals(userEdit.getName()) && user.getPassword().equals(userEdit.getPassword())) {
JOptionPane.showMessageDialog(null, "登录成功\n用户名:" + user.getName() + "\n密码:" + user.getPassword() + "\n注册时间:" + user.getTime());
state = true;
}
}
if(count == 1) {
JOptionPane.showMessageDialog(null, "登录失败,即将退出程序");
System.exit(0);
}
else if(!state) {
JOptionPane.showMessageDialog(null, "登录失败,您还有" + (--count) + "次机会");
}
break;
}
}
}
class User {
private String name;
private String password;
private String time;
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public void setTime(String time) {
this.time = time;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public String getTime() {
return time;
}
}本回答被提问者采纳
第2个回答 推荐于2018-05-13
/ /你看到自己专门为你烫一下注册和登录系统 1新用户注册 2登录 3退出 1 注册帐号: 请输入您的姓名: KA西瓜 请输入您的密码:! 注册成功 注册和登录系统 1新用户注册 2登录 3退出 2 登录帐号: 请输入用户名: KA西瓜 请输入密码: 确定登录...本回答被网友采纳