• Java基础项目~用户管理系统


    目录

    1,整体应用操作界面

    UserApplication.java

    2,数据访问对象,用于封装数据的访问逻辑细节

     package-info.java

    接口类:IUserDao

    UserDaoImpl.java  继承于IUserDao接口

    3,定义值bean

    User.java

    4,工具类

    ArrayList.java

    DaoFactory.java

    代码运行截图:


    1,整体应用操作界面

    UserApplication.java

    1. package com.yan.app;
    2. import java.io.BufferedReader;
    3. import java.io.IOException;
    4. import java.io.InputStreamReader;
    5. import com.yan.dao.IUserDao;
    6. import com.yan.entity.User;
    7. import com.yan.util.DaoFactory;
    8. public class UserApplication {
    9. private static User user = null;
    10. public static void main(String[] args) throws Exception {
    11. IUserDao userDao = DaoFactory.getUserDao();
    12. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    13. while (true) {
    14. menu1();
    15. String ss = br.readLine();
    16. switch (ss) {
    17. case "1":
    18. User user = inputUser(br);
    19. userDao.save(user);
    20. break;
    21. case "2":
    22. User user1 = loginUser(br);
    23. User bb = userDao.login(user1);
    24. if (bb != null) {
    25. UserApplication.user = bb;
    26. System.out.println("登录成功!");
    27. } else {
    28. System.out.println("登录失败!请重新登录");
    29. }
    30. break;
    31. case "3":
    32. System.out.println("====目前已注册的用户列表====");
    33. Object[] arr = userDao.getAll().getArray();
    34. for (Object temp : arr) {
    35. if (temp != null)
    36. System.out.println(temp);
    37. }
    38. System.out.println("====================");
    39. break;
    40. case "4":
    41. User user2 = mofidyUser(br);
    42. userDao.update(user2);
    43. UserApplication.user = null;
    44. System.out.println("修改完毕!请重新登录");
    45. break;
    46. case "5":
    47. UserApplication.user = null;
    48. System.out.println("退出登录");
    49. break;
    50. case "6":
    51. System.out.println("输入要删除的用户编号:");
    52. Long id = null;
    53. while (true) {
    54. String ss1 = br.readLine();
    55. try {
    56. id = Long.parseLong(ss1);
    57. break;
    58. } catch (Exception e) {
    59. System.out.println("输入的数据格式错误!请重新输入");
    60. id = null;
    61. }
    62. }
    63. if (id.equals(UserApplication.user.getId()))
    64. UserApplication.user = null;
    65. userDao.delete(id);
    66. break;
    67. // default:
    68. // System.exit(0);
    69. // break;
    70. }
    71. if ("0".equals(ss)) {
    72. System.out.println("bye~~bye");
    73. break;
    74. }
    75. }
    76. br.close();
    77. }
    78. private static User mofidyUser(BufferedReader br) throws Exception {
    79. User res = UserApplication.user;
    80. System.out.println("请输入新的用户名称和接口和指令:");
    81. System.out.println("原始名称为:" + res.getUsername());
    82. System.out.println("请输入新的用户名称:(不修改则直接回车)");
    83. String username = br.readLine();
    84. if (username != null && username.trim().length() >= 6 && username.trim().length() <= 20)
    85. res.setUsername(username);
    86. System.out.println("请输入新用户口令:(不修改则直接回车)");
    87. String password = br.readLine();
    88. if (password != null && password.trim().length() >= 6 && password.trim().length() <= 20)
    89. res.setPassword(password);
    90. return res;
    91. }
    92. private static User loginUser(BufferedReader br) {
    93. System.out.println("请输入用户名称和口令:");
    94. String username = inputString(br, "用户名称", 6, 20);
    95. String password = inputString(br, "用户口令", 6, 20);
    96. User user = new User();
    97. user.setUsername(username);
    98. user.setPassword(password);
    99. return user;
    100. }
    101. public static void menu1() {
    102. System.out.println("============欢迎使用本系统============");
    103. System.out.println("1---新增用户 2---登录系统");
    104. if (user != null) {
    105. System.out.println("3---显示所有用户 4---修改注册信息");
    106. System.out.println("5---退出登录 6---删除用户");
    107. }
    108. System.out.println("0---退出系统");
    109. System.out.println("=================================");
    110. System.out.println("请选择...");
    111. }
    112. public static User inputUser(BufferedReader br) {
    113. User user = new User();
    114. String username = inputString(br, "用户名称", 6);
    115. String password = null;
    116. while (true) {
    117. password = inputString(br, "用户口令", 6, 20);
    118. String repassword = inputString(br, "确认口令");
    119. if (password.equals(repassword))
    120. break;
    121. System.out.println("您输入的口令和确认口令不一致,请重新输入");
    122. }
    123. user.setUsername(username);
    124. user.setPassword(password);
    125. return user;
    126. }
    127. // int...如果有参数,则参数1为最小长度,参数2为最大长度
    128. public static String inputString(BufferedReader br, String name, int... params) {
    129. String res = null;
    130. boolean flag = false;
    131. while (true) {
    132. System.out.println("请输入" + name + ":");
    133. try {
    134. res = br.readLine();
    135. if (res != null) {
    136. if (params != null && params.length > 0) {
    137. flag = res.trim().length() >= params[0];
    138. if (flag) {
    139. if (params.length > 1)
    140. flag = res.trim().length() <= params[1];
    141. }
    142. } else
    143. flag = true;
    144. }
    145. } catch (IOException e) {
    146. e.printStackTrace();
    147. }
    148. if (flag)
    149. break;
    150. else
    151. System.out.println("你输入的数据有误!请重新输入");
    152. }
    153. return res;
    154. }
    155. }

    2,数据访问对象,用于封装数据的访问逻辑细节

     package-info.java

    1. class Constants {
    2. public static final String FILE_NAME = "data/users.data";
    3. public static final String TEMP_NAME = "data/users_tmp.data";
    4. }

    接口类:IUserDao

    1. package com.yan.dao;
    2. import com.yan.entity.User;
    3. import com.yan.util.ArrayList;
    4. public interface IUserDao {
    5. boolean save(User user);
    6. User login(User user);
    7. boolean delete(Long id);
    8. boolean update(User user2);
    9. ArrayList getAll();
    10. }

    UserDaoImpl.java  继承于IUserDao接口

    1. package com.yan.dao;
    2. import java.io.EOFException;
    3. import java.io.File;
    4. import java.io.FileInputStream;
    5. import java.io.FileOutputStream;
    6. import java.io.ObjectInputStream;
    7. import java.io.ObjectOutputStream;
    8. import com.yan.entity.User;
    9. import com.yan.util.ArrayList;
    10. import com.yan.util.DaoFactory;
    11. public class UserDaoImpl implements IUserDao {
    12. public boolean save(User user) {
    13. boolean res = false;
    14. File oldFile = new File(Constants.FILE_NAME);
    15. File tmpFile = new File(Constants.TEMP_NAME);
    16. //判断oIdFile是否存在。exists存在的
    17. if (oldFile.exists()) {
    18. // FileInput
    19. try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oldFile));
    20. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tmpFile));) {
    21. long maxId = ois.readLong();
    22. user.setId(maxId + 1);
    23. oos.writeLong(user.getId());
    24. while (true) {
    25. try {
    26. // read读取
    27. Object tmp = ois.readObject();
    28. if (tmp != null)
    29. // write写入
    30. oos.writeObject(tmp);
    31. } catch (EOFException e) {
    32. break;
    33. }
    34. }
    35. oos.writeObject(user);
    36. res = true;
    37. } catch (Exception e) {
    38. // 在命令行打印异常信息在程序中出错的位置及原因
    39. e.printStackTrace();
    40. res = false;
    41. }
    42. } else {
    43. try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(oldFile));) {
    44. user.setId(1L);
    45. oos.writeLong(user.getId());
    46. oos.writeObject(user);
    47. res = true;
    48. } catch (Exception e) {
    49. e.printStackTrace();
    50. res = false;
    51. }
    52. }
    53. if (oldFile.exists() && tmpFile.exists()) {
    54. oldFile.delete();
    55. tmpFile.renameTo(oldFile);
    56. }
    57. return res;
    58. }
    59. public static void main(String[] args) {
    60. IUserDao userDao = DaoFactory.getUserDao();
    61. User user = new User();
    62. user.setId(2L);
    63. user.setUsername("zhangsan");
    64. user.setPassword("666666");
    65. boolean res = userDao.update(user);
    66. System.out.println(res ? "成功" : "失败");
    67. }
    68. @Override
    69. public User login(User user1) {
    70. User res = null;
    71. File oIdFile = new File(Constants.FILE_NAME);
    72. if (oIdFile.exists()) {
    73. try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oIdFile))) {
    74. ois.skip(8);
    75. while (true) {
    76. Object obj = ois.readObject();
    77. if (obj != null && obj instanceof User) {
    78. User tmp = (User) obj;
    79. if (user1.equals(tmp)) {
    80. res = tmp;
    81. break;
    82. }
    83. }
    84. }
    85. } catch (EOFException e) {
    86. System.out.println("文件结束...");
    87. } catch (Exception e) {
    88. e.printStackTrace();
    89. }
    90. }
    91. return res;
    92. }
    93. @Override
    94. public ArrayList getAll() {
    95. ArrayList res = new ArrayList();
    96. File oldFile = new File(Constants.FILE_NAME);
    97. if (oldFile.exists()) {
    98. try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oldFile))) {
    99. ois.skip(8);
    100. while (true) {
    101. Object obj = ois.readObject();
    102. res.add(obj);
    103. }
    104. } catch (EOFException e) {
    105. System.out.println("数据加载完毕!");
    106. } catch (Exception e) {
    107. e.printStackTrace();
    108. }
    109. }
    110. return res;
    111. }
    112. @Override
    113. public boolean update(User user) {
    114. boolean res = false;
    115. File oldFile = new File(Constants.FILE_NAME);
    116. File tmpFile = new File(Constants.TEMP_NAME);
    117. if (oldFile.exists()) {
    118. try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oldFile));
    119. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tmpFile));) {
    120. oos.writeLong(ois.readLong());
    121. while (true) {
    122. try {
    123. Object tmp = ois.readObject();
    124. System.out.println(user);
    125. if (tmp != null) {
    126. if (!tmp.equals(user)) {
    127. System.out.println(tmp);
    128. oos.writeObject(tmp);
    129. } else {
    130. oos.writeObject(user);
    131. res = true;
    132. }
    133. }
    134. } catch (EOFException e) {
    135. break;
    136. }
    137. }
    138. } catch (Exception e) {
    139. e.printStackTrace();
    140. res = false;
    141. }
    142. }
    143. if (oldFile.exists() && tmpFile.exists()) {
    144. oldFile.delete();
    145. tmpFile.renameTo(oldFile);
    146. }
    147. return res;
    148. }
    149. @Override
    150. public boolean delete(Long id) {
    151. boolean res = false;
    152. File oldFile = new File(Constants.FILE_NAME);
    153. File tmpFile = new File(Constants.TEMP_NAME);
    154. if (oldFile.exists()) {
    155. try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(oldFile));
    156. ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(tmpFile));) {
    157. oos.writeLong(ois.readLong());
    158. while (true) {
    159. try {
    160. Object tmp = ois.readObject();
    161. if (tmp != null) {
    162. if (tmp instanceof User) {
    163. User temp = (User) tmp;
    164. if (!id.equals(temp.getId())) {
    165. oos.writeObject(tmp);
    166. } else {
    167. res = true;
    168. }
    169. }
    170. }
    171. } catch (EOFException e) {
    172. break;
    173. }
    174. }
    175. } catch (Exception e) {
    176. e.printStackTrace();
    177. res = false;
    178. }
    179. }
    180. if (oldFile.exists() && tmpFile.exists()) {
    181. oldFile.delete();
    182. tmpFile.renameTo(oldFile);
    183. }
    184. return res;
    185. }
    186. }

    3,定义值bean

    User.java

    1. package com.yan.entity;
    2. import java.io.Serializable;
    3. import java.util.Objects;
    4. public class User implements Serializable {
    5. /**
    6. *
    7. */
    8. private static final long serialVersionUID = 1L;
    9. private Long id;
    10. private String username;
    11. private String password;
    12. private String repassword;
    13. @Override
    14. public int hashCode() {
    15. if (this.id == null)
    16. return Objects.hash(password, username);
    17. return Objects.hash(id);
    18. }
    19. @Override
    20. public boolean equals(Object obj) {
    21. if (this == obj)
    22. return true;
    23. if (obj == null)
    24. return false;
    25. if (getClass() != obj.getClass())
    26. return false;
    27. User other = (User) obj;
    28. if (this.id == null || other.id == null)
    29. return Objects.equals(password, other.password) && Objects.equals(username, other.username);
    30. else
    31. return Objects.equals(this.id, other.id);
    32. }
    33. // =============================
    34. public Long getId() {
    35. return id;
    36. }
    37. public void setId(Long id) {
    38. this.id = id;
    39. }
    40. public String getUsername() {
    41. return username;
    42. }
    43. public void setUsername(String username) {
    44. this.username = username;
    45. }
    46. public String getPassword() {
    47. return password;
    48. }
    49. public String getShowPassword() {
    50. StringBuilder sb = new StringBuilder();
    51. for (int i = 0; i < password.length(); i++)
    52. sb.append("*");
    53. return sb.toString();
    54. }
    55. public void setPassword(String password) {
    56. this.password = password;
    57. }
    58. public String getRepassword() {
    59. return repassword;
    60. }
    61. public void setRepassword(String repassword) {
    62. this.repassword = repassword;
    63. }
    64. @Override
    65. public String toString() {
    66. return "User [id=" + id + ", username=" + username + ", password=" + this.getShowPassword() + "]";
    67. }
    68. }

    4,工具类

    ArrayList.java

    1. package com.yan.util;
    2. public class ArrayList {
    3. private Object[] arr;
    4. private int count;
    5. public ArrayList() {
    6. this(10);
    7. }
    8. public ArrayList(int length) {
    9. arr = new Object[length];
    10. }
    11. public void add(Object obj) {
    12. arr[count++] = obj;
    13. if (count >= arr.length) {
    14. resize();
    15. }
    16. }
    17. public Object[] getArray() {
    18. return arr;
    19. }
    20. private void resize() {
    21. Object[] res = new Object[arr.length * 2];
    22. System.arraycopy(arr, 0, res, 0, arr.length);
    23. this.arr = res;
    24. }
    25. }

    DaoFactory.java

    1. package com.yan.util;
    2. import com.yan.dao.IUserDao;
    3. import com.yan.dao.UserDaoImpl;
    4. public class DaoFactory {
    5. private DaoFactory() {
    6. }
    7. private static IUserDao userDao = new UserDaoImpl();
    8. public static IUserDao getUserDao() {
    9. return userDao;
    10. }
    11. }

    代码运行截图:

     

  • 相关阅读:
    工程课 SpringBoot-2.1. 笔记
    C++ 函数
    基于Spring Boot的宠物猫店管理系统的设计与实现毕业设计源码140909
    小白备战大厂算法笔试(二)——数组、链表、列表
    输入回车换行,div标签可编写
    休眠和睡眠有哪些区别?如何让电脑一键休眠?
    VS + QT 封装带UI界面的DLL
    算法刷题日志——回溯算法
    【Redis笔记】发布与订阅
    2021 华数杯全国大学生数学建模竞赛C题-基于神经网络预测电动汽车目标客户销售策略(三)(附带赛题解析&获奖论文及MATLAB代码)
  • 原文地址:https://blog.csdn.net/qq_51222096/article/details/126321328