分析
简单实现后分析问题:
1、数据重复,要求id不允许重复。引入算法:max(id)+1是新用户的id值
2、用户注册信息中用户名称和口令不能为空,不再输入验证.所以操作之前应该对传入数据进行验证,如果验证失败则通过抛出异常阻止程序执行public void regist(UserBean user)throws Exception
3、显示所有用户信息:为了测试方便而添加的方法
1、传入参数:void
2、返回值:void
public void showAll()throws Exception
4、用户登录login
1、传入参数: UserBean其中包含的是用户输入的登录信息,应该包含用户名称和口令
2、返回值:boolean 登录成功则返回true,否则false
3、操作文件过程中可能会有异常IOException
4、具体的处理逻辑:遍历文件中所保存的用户信息,如果有对应的用户名和口令的对象存在,则立即中断并返回true;如果遍历整个文件都没有符合要求的对象,则返回false
public boolean login(UserBean user)throws Exception
package com.yan.app;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.yan.dao.IUserDao;
import com.yan.entity.User;
import com.yan.util.DaoFactory;
//整体应用操作界面
public class UserAppliction {
private static User user = null;
public static void main(String[] args) throws Exception {
IUserDao userDao = DaoFactory.getUserDao();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
menu1();
String ss = br.readLine();
switch (ss) {
case "0":
System.out.println("bye~~~bye~~~");
System.exit(0);
case "1":
User user = inputUser(br);
userDao.save(user);
break;
case "2":
User user1 = loginUser(br);
User bb = userDao.login(user1);
if (bb != null) {
UserAppliction.user = bb;
System.out.println("登陆成功");
} else {
System.out.println("登录失败");
}
break;
case "3":// 显示所有用户
System.out.println("=========已注册的用户信息表=========");
Object[] arr = userDao.getAll().getArr();
for (Object tmp : arr) {
if (tmp != null)
System.out.println(tmp);
}
System.out.println("=============================");
break;
case "4":// 修改注册信息
User user2 = mofidyUser(br);
userDao.upDate(user2);
UserAppliction.user = null;
System.out.println("修改完了");
break;
case "5":// 退出登录
UserAppliction.user = null;
System.out.println("退出登录");
break;
case "6":// 删除用户
System.out.println("请输入要删除的编号!");
Long id = null;
while (true) {
String ss1 = br.readLine();
try {
id = Long.parseLong(ss1);
break;
} catch (Exception e) {
e.printStackTrace();
id = null;
}
}
if (id.equals(UserAppliction.user.getId())) {
UserAppliction.user = null;
}
userDao.delete(id);
break;
//
// default:
// break;
}
}
}
public static void menu1() {
System.out.println("============欢迎登陆本系统============");
System.out.println("1---新增用户 2---登陆系统");
if (user != null) {
System.out.println("3---显示所有用户 4---修改注册信息");
System.out.println("5---退出登录 6---删除用户");
}
System.out.println("0---退出系统");
System.out.println("=================================");
System.out.println("请选择……");
}
private static User mofidyUser(BufferedReader br) throws Exception {
User us = UserAppliction.user;
System.out.println("请输入用户名称和口令");
System.out.println("原始名称" + us.getUsername());
System.out.println("请输入新用户名称");
String username = br.readLine();
if (username != null || username.trim().length() >= 6 && username.trim().length() <= 20) {
us.setUsername(username);
}
System.out.println("请输入新用户口令");
String password = br.readLine();
if (password != null || password.trim().length() >= 6 && password.trim().length() <= 20) {
us.setPassword(password);
}
return us;
}
private static User loginUser(BufferedReader br) {
System.out.println("请输入用户名称和用户口令");
String username = inputString(br, "用户名称", 6);
String password = inputString(br, "用户口令", 6, 20);
User user = new User();
user.setUsername(username);
user.setPassword(password);
return user;
}
public static User inputUser(BufferedReader br) {
User user = new User();
String username = inputString(br, "用户名称", 6);
String password = null;
while (true) {
password = inputString(br, "用户口令", 6, 20);
String repassword = inputString(br, "确认口令", 6, 20);
if (password.equals(repassword)) {
break;
} else
System.out.println("您确认的口令和输入的口令不一致,请重新输入");
}
user.setUsername(username);
user.setPassword(password);
return user;
}
public static String inputString(BufferedReader br, String name, int... params) {
String res = null;
boolean flag = false;
while (true) {
System.out.println("请输入" + name + ":");
try {
res = br.readLine();
if (res != null) {
if (params != null) {
if (params.length > 0) {
flag = res.trim().length() >= params[0];
if (flag) {
if (params.length > 1) {
flag = res.trim().length() <= params[1];
}
}
}
} else {
flag = true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (flag) {
break;
} else {
System.out.println("您输入的数据有误!请重新输入");
}
}
return res;
}
}
package com.yan.dao;
import com.yan.entity.User;
import com.yan.util.ArrayList;
public interface IUserDao {
boolean save(User user);
User login(User user);
ArrayList getAll();
boolean upDate(User user2);
boolean delete(Long id);
}
package com.yan.dao;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.yan.entity.User;
import com.yan.util.ArrayList;
//dao是数据访问接口的简称
public class UserDaoImpl implements IUserDao {
@Override
public boolean save(User user) {
File oldFile = new File(Constants.FILE_NAME);
File tmpFile = new File(Constants.TEMP_NAME);
boolean flag = false;
if (oldFile.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oldFile));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tmpFile));) {
long maxId = ois.readLong();
user.setId(maxId + 1);
oos.writeLong(user.getId());
while (true) {
try {
Object obj = ois.readObject();
if (obj != null)
oos.writeObject(obj);
} catch (EOFException e) {
break;
}
}
oos.writeObject(user);
flag = true;
} catch (Exception e) {
e.printStackTrace();
flag = false;
}
} else {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(oldFile))) {
user.setId(1L);
oos.writeLong(user.getId());
oos.writeObject(user);
flag = true;
} catch (Exception e) {
e.printStackTrace();
flag = false;
}
}
if (oldFile.exists() && tmpFile.exists()) {
oldFile.delete();
tmpFile.renameTo(oldFile);
}
return flag;
}
@Override
public User login(User user) {
User us = null;
File oldfile = new File(Constants.FILE_NAME);
if (oldfile.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oldfile))) {
ois.skip(8);
while (true) {
Object obj = ois.readObject();
if (obj != null && obj instanceof User) {
User tmp = (User) obj;
if (user.equals(obj)) {
us = tmp;
break;
}
}
}
} catch (EOFException e) {
System.out.println("文件读取完了");
} catch (Exception e) {
e.printStackTrace();
}
}
return us;
}
@Override
public ArrayList getAll() {
ArrayList al = new ArrayList();
File oldFile = new File(Constants.FILE_NAME);
if (oldFile.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oldFile))) {
ois.skip(8);
while (true) {
Object obj = ois.readObject();
al.add(obj);
}
} catch (EOFException e) {
System.out.println("文件读取完了");
} catch (Exception e) {
e.printStackTrace();
}
}
return al;
}
@Override
public boolean upDate(User user) {
boolean flag = false;
File oldFile = new File(Constants.FILE_NAME);
File tmpFile = new File(Constants.TEMP_NAME);
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oldFile));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tmpFile));) {
oos.writeLong(ois.readLong());
while (true) {
try {
Object obj = ois.readObject();
System.out.println(user);
if (obj != null) {
if (!obj.equals(user)) {
oos.writeObject(obj);
System.out.println(obj);
} else {
oos.writeObject(user);
flag = true;
}
}
} catch (EOFException e) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
flag = false;
}
if (oldFile.exists() || tmpFile.exists()) {
oldFile.delete();
tmpFile.renameTo(oldFile);
}
return flag;
}
@Override
public boolean delete(Long id) {
boolean flag = false;
File oldFile = new File(Constants.FILE_NAME);
File tmpFile = new File(Constants.TEMP_NAME);
if (oldFile.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oldFile));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tmpFile));) {
oos.writeLong(ois.readLong());
while (true) {
try {
Object obj = ois.readObject();
if (obj != null) {
if (obj instanceof User) {
User tmp = (User) obj;
if (!id.equals(tmp.getId())) {
oos.writeObject(obj);
} else {
flag = true;
}
}
}
} catch (EOFException e) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
flag = false;
}
}
if (oldFile.exists() || tmpFile.exists()) {
oldFile.delete();
tmpFile.renameTo(oldFile);
}
return flag;
}
}
package com.yan.entity;
import java.io.Serializable;
import java.util.Objects;
//定义值been
public class User implements Serializable {
private static final long serialVersionUID = 975753826485206653L;
private Long id;
private String username;
private String password;
private String repassword;
public User(Long id, String username, String password, String repassword) {
super();
this.id = id;
this.username = username;
this.password = password;
this.repassword = repassword;
}
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + this.getShowPassword() + "]";
}
private String getShowPassword() {
StringBuilder ss = new StringBuilder();
for (int i = 0; i < password.length(); i++) {
ss.append("*");
}
return ss.toString();
}
@Override
public int hashCode() {
if (this.id == null)
return Objects.hash(password, username);
return Objects.hash(id);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (this.id == null || other.id == null)
return Objects.equals(password, other.password) && Objects.equals(username, other.username);
else
return Objects.equals(id, other.id);
}
}
package com.yan.util;
import com.yan.dao.IUserDao;
import com.yan.dao.UserDaoImpl;
//工具类
public class DaoFactory {
private DaoFactory() {
}
private static IUserDao userDao = new UserDaoImpl();
public static IUserDao getUserDao() {
return userDao;
}
}
package com.yan.util;
public class ArrayList {
private Object[] arr;
private int count;
public ArrayList() {
this(10);
}
public ArrayList(int length) {
arr = new Object[length];
}
public void add(Object obj) {
arr[count++] = obj;
if (count >= arr.length)
extend();
}
private void extend() {
Object[] brr = new Object[arr.length * 3 / 2];
System.arraycopy(arr, 0, brr, 0, arr.length);
this.arr = brr;
}
public Object[] getArr() {
return arr;
}
}