用户管理系统

目录

用户管理系统

一、mycode.app包

        下的UserApplication.java文件

二、mycode.dao包

package-info.java文件

IUserDao.java文件

UserDaoImpl.java文件

三、mycode.entity包

User.java文件

四、mycode.util包

ArrayList.java文件

DaoFactory.java文件

五、mycode.trst包

Test.java文件

六、data文件夹

users.data文件

项目包,文件布局

实现样式


一、mycode.app包

        下的UserApplication.java文件

package mycode.app;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import mycode.dao.IUserDao;
import mycode.entity.User;
import mycode.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().toArray();
				for (Object temp : arr) {
					if (temp != null)
						System.out.println(temp);
				}
				System.out.println("==============================");
				break;
			case "5":
				UserApplication.user=null;
				System.out.println("退出登录");
				break;
			case "4":
				User user2 = mofidyUser(br);
				userDao.update(user2);
				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;

			}
			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;
		System.out.println("请输入" + name + ":");
		while (true) {
			try {
				res = br.readLine();
				if (res != null && res.trim().length() > 0) {
					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;
	}
}

二、mycode.dao包

package-info.java文件

package mycode.dao;

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

IUserDao.java文件

package mycode.dao;

import java.util.ArrayList;

import mycode.entity.User;

public interface IUserDao {//接口

	//抽象方法
	boolean save(User user);

	User login(User user);

	ArrayList getAll();

	boolean update(User user);

	boolean delete(Long id);

}

UserDaoImpl.java文件

package mycode.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 java.util.ArrayList;

import mycode.entity.User;
import mycode.util.DaoFactory;

public class UserDaoImpl implements IUserDao {

	@Override
	public boolean save(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));) {
				long maxId = ois.readLong();
				user.setId(maxId + 1);
				oos.writeLong(user.getId());
				while (true) {
					try {
						Object tmp = ois.readObject();
						if (tmp != null)
							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);
			} 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.setUsername("lisi");
//		user.setPassword("4444");
//		boolean res=userDao.save(user);
//		System.out.println(res?"成功":"失败");
//		System.out.println(user.getId());
		
//		User user=new User();
//		user.setUsername("lisi");
//		user.setPassword("5555");
//		boolean bb=userDao.login(user);
//		System.out.println(bb);
		
//		ArrayList list = userDao.getAll();
//		Object[] arr = list.getArray();
//		for (Object temp : arr)
//			if (temp != null)
//				System.out.println(temp);

		User user = new User();
		user.setId(2L);
		user.setUsername("lisi666");
		user.setPassword("666666");
		boolean res = userDao.update(user);
		System.out.println(res ? "成功" : "失败");
	}
	
	 @Override
	 public User login(User user) {
			User res = 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(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;
		}
}

三、mycode.entity包

User.java文件

package mycode.entity;

import java.io.Serializable;
import java.util.Objects;
public class User implements Serializable {
	private static final long serialVersionUID = -5842697108263364883L;
	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 String getShowPassword() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < password.length(); i++)
			sb.append("*");
		return sb.toString();
	}
	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() + "]";
	}
}

四、mycode.util包

ArrayList.java文件

package mycode.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;
	}
}

DaoFactory.java文件

package mycode.util;

import mycode.dao.IUserDao;
import mycode.dao.UserDaoImpl;

public class DaoFactory {
	private DaoFactory() {
	}

	private static IUserDao userDao = new UserDaoImpl();

	public static IUserDao getUserDao() {
		return userDao;
	}
}

五、mycode.trst包

Test.java文件

package test;

import java.sql.Date;
import java.util.Arrays;

public class Test1 {
	public static void main(String[] args) {
		// 简单类型
		int[] arr1 = { 1, 2, 3, 4, 5, 6 };
		int[] brr1 = new int[arr1.length * 2];
		System.arraycopy(arr1, 1, brr1, 2, arr1.length - 1);
		System.out.println(Arrays.toString(brr1));
		// 引用类型
		Object[] arr2 = new Object[3];
		for (int i = 0; i < arr2.length; i++) {
			arr2[i] = new Date(1659684098321L+i*1000);
		}
		Date[] brr2=new Date[arr2.length*2];
		System.arraycopy(arr2, 0, brr2, 0, arr2.length);
		((Date)arr2[0]).setYear(9000);
		System.out.println(Arrays.toString(brr2));
	}
}

六、data文件夹

users.data文件

Readme.md说明文件

# 用户管理系统

简单实现后分析问题:

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`

项目包,文件布局

Java用户管理系统说明 java编写用户管理系统_java

实现样式

Java用户管理系统说明 java编写用户管理系统_System_02

 

Java用户管理系统说明 java编写用户管理系统_Java用户管理系统说明_03