目录
UserDaoImpl.java 继承于IUserDao接口
- 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 UserApplication {
- 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 "1":
- User user = inputUser(br);
- userDao.save(user);
- break;
- case "2":
- User user1 = loginUser(br);
- User bb = userDao.login(user1);
- if (bb != null) {
- UserApplication.user = bb;
- System.out.println("登录成功!");
- } else {
- System.out.println("登录失败!请重新登录");
- }
- break;
- case "3":
- System.out.println("====目前已注册的用户列表====");
- Object[] arr = userDao.getAll().getArray();
- for (Object temp : arr) {
- if (temp != null)
- System.out.println(temp);
- }
- System.out.println("====================");
- break;
- case "4":
- User user2 = mofidyUser(br);
- userDao.update(user2);
- UserApplication.user = null;
- System.out.println("修改完毕!请重新登录");
- break;
- case "5":
- UserApplication.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) {
- System.out.println("输入的数据格式错误!请重新输入");
- id = null;
- }
- }
- if (id.equals(UserApplication.user.getId()))
- UserApplication.user = null;
- userDao.delete(id);
- break;
-
- // default:
- // System.exit(0);
- // break;
-
- }
- if ("0".equals(ss)) {
- System.out.println("bye~~bye");
- break;
- }
- }
- br.close();
- }
-
- private static User mofidyUser(BufferedReader br) throws Exception {
- User res = UserApplication.user;
- System.out.println("请输入新的用户名称和接口和指令:");
- System.out.println("原始名称为:" + res.getUsername());
- System.out.println("请输入新的用户名称:(不修改则直接回车)");
- String username = br.readLine();
- if (username != null && username.trim().length() >= 6 && username.trim().length() <= 20)
- res.setUsername(username);
- System.out.println("请输入新用户口令:(不修改则直接回车)");
- String password = br.readLine();
- if (password != null && password.trim().length() >= 6 && password.trim().length() <= 20)
- res.setPassword(password);
- return res;
- }
-
- private static User loginUser(BufferedReader br) {
- System.out.println("请输入用户名称和口令:");
- String username = inputString(br, "用户名称", 6, 20);
- String password = inputString(br, "用户口令", 6, 20);
- User user = new User();
- user.setUsername(username);
- user.setPassword(password);
- return user;
- }
-
- 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("请选择...");
- }
-
- 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, "确认口令");
- if (password.equals(repassword))
- break;
- System.out.println("您输入的口令和确认口令不一致,请重新输入");
- }
- user.setUsername(username);
- user.setPassword(password);
- return user;
- }
-
- // int...如果有参数,则参数1为最小长度,参数2为最大长度
-
- 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 && 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;
- }
-
- }
- class Constants {
- public static final String FILE_NAME = "data/users.data";
- public static final String TEMP_NAME = "data/users_tmp.data";
- }
- 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);
-
- boolean delete(Long id);
-
- boolean update(User user2);
-
- ArrayList getAll();
-
- }
- 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;
- import com.yan.util.DaoFactory;
-
- public class UserDaoImpl implements IUserDao {
- public boolean save(User user) {
- boolean res = false;
- File oldFile = new File(Constants.FILE_NAME);
- File tmpFile = new File(Constants.TEMP_NAME);
- //判断oIdFile是否存在。exists存在的
- if (oldFile.exists()) {
- // FileInput
- 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 {
- // read读取
- Object tmp = ois.readObject();
- if (tmp != null)
- // write写入
- oos.writeObject(tmp);
- } catch (EOFException e) {
- break;
- }
- }
- oos.writeObject(user);
- res = true;
- } catch (Exception e) {
- // 在命令行打印异常信息在程序中出错的位置及原因
- e.printStackTrace();
- res = false;
- }
- } else {
- try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(oldFile));) {
- user.setId(1L);
- oos.writeLong(user.getId());
- oos.writeObject(user);
- res = true;
- } catch (Exception e) {
- e.printStackTrace();
- res = false;
- }
- }
- if (oldFile.exists() && tmpFile.exists()) {
- oldFile.delete();
- tmpFile.renameTo(oldFile);
- }
- return res;
-
- }
-
- public static void main(String[] args) {
- IUserDao userDao = DaoFactory.getUserDao();
- User user = new User();
- user.setId(2L);
- user.setUsername("zhangsan");
- user.setPassword("666666");
- boolean res = userDao.update(user);
- System.out.println(res ? "成功" : "失败");
- }
-
- @Override
- public User login(User user1) {
- User res = null;
- File oIdFile = new File(Constants.FILE_NAME);
- if (oIdFile.exists()) {
- try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oIdFile))) {
- ois.skip(8);
- while (true) {
- Object obj = ois.readObject();
- if (obj != null && obj instanceof User) {
- User tmp = (User) obj;
- if (user1.equals(tmp)) {
- res = tmp;
- break;
- }
- }
- }
- } catch (EOFException e) {
- System.out.println("文件结束...");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return res;
- }
-
- @Override
- public ArrayList getAll() {
- ArrayList res = 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();
- res.add(obj);
- }
- } catch (EOFException e) {
- System.out.println("数据加载完毕!");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return res;
- }
-
- @Override
- public boolean update(User user) {
- boolean res = 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 tmp = ois.readObject();
- System.out.println(user);
- if (tmp != null) {
- if (!tmp.equals(user)) {
- System.out.println(tmp);
- oos.writeObject(tmp);
- } else {
- oos.writeObject(user);
- res = true;
- }
- }
- } catch (EOFException e) {
- break;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- res = false;
- }
- }
- if (oldFile.exists() && tmpFile.exists()) {
- oldFile.delete();
- tmpFile.renameTo(oldFile);
- }
- return res;
- }
-
- @Override
- public boolean delete(Long id) {
- boolean res = 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 tmp = ois.readObject();
- if (tmp != null) {
- if (tmp instanceof User) {
- User temp = (User) tmp;
- if (!id.equals(temp.getId())) {
- oos.writeObject(tmp);
- } else {
- res = true;
- }
- }
- }
- } catch (EOFException e) {
- break;
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- res = false;
- }
- }
- if (oldFile.exists() && tmpFile.exists()) {
- oldFile.delete();
- tmpFile.renameTo(oldFile);
- }
- return res;
-
- }
-
- }
- package com.yan.entity;
-
- import java.io.Serializable;
- import java.util.Objects;
-
- public class User implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private Long id;
- private String username;
- private String password;
- private String repassword;
-
- @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(this.id, other.id);
- }
-
- // =============================
- 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 String getShowPassword() {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < password.length(); i++)
- sb.append("*");
- return sb.toString();
- }
-
- 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() + "]";
- }
-
- }
- 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) {
- resize();
- }
- }
-
- public Object[] getArray() {
- return arr;
- }
-
- private void resize() {
- Object[] res = new Object[arr.length * 2];
- System.arraycopy(arr, 0, res, 0, arr.length);
- this.arr = res;
- }
- }
-
- 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;
- }
-
- }
