综合练习一
-
背景
某银行监管系统,需要设计并实现用户登入记录功能,每个用户登入系统时,系统自动记录登入用户的账户、登入时间、登入失败成功与否信息等,普通用户只能登入登出,管理员可以登入后查看日志及分析统计信息等。
-
用户账户信息存储设计
(5 分) 为了系统设计轻便,采用 XML 文件记录用户账户信息,要求可以记录有序号、用户账号、密码、身份 (管理员、普通) 等信息。
-
主要功能界面
(10 分) 主要功能界面要求参考如下:
--银行监管系统--
- 用户登录
- 查看日志
- 数据统计
- 退出系统
- 用户登录操作(选择 1)
=============================================
(25 分) 选择 1 时,进行用户登入操作:
>> 用户登录 >>>>>>>>>>
请输入账号 :
请输入密码 :
=============================================
输入完点击回车后,判断用户名密码是否正确,不论登入成功与否、都需要在控制台给予结果提示,并在日志文件中写入登入记录,以备查询。
=============================================
- 查看日志(选择 2)
(25 分) 选择 2 时,必须确保先用管理员账号登入成功。
>> 查 看 日 志 >>>>>>>>>> 抱歉,请先登录!
=============================================
>> 查 看 日 志 >>>>>>>>>> 抱歉,您不是管理员!
=============================================
>> 查 看 日 志 >>>>>>>>>>
您好,XXX, 系统全部登录日志如下:
序号 日期 用户 结果
- 2019-03-04 13:23:00 admin (管理员) 登入成功
- 2019-12-04 23:15:00 lisi (普通) 登入成功
- 2019-12-12 15:33:00 wangmu (普通) 登入成功
- 2019-12-13 09:27:00 zhaoliu(普通) 登入成功
- 2019-12-14 05:10:00 tianqi (普通) 登入失败
- 2019-12-14 05:11:00 tianqi (普通) 登入失败
- 2019-12-14 05:13:00 tianqi (普通) 登入失败
- 2019-12-14 05:14:00 tianqi (普通) 登入失败
=============================================
- 数据统计(选择 3)
(20 分) 选择 3 时,必须确保先用管理员账号登入成功 (否则,给出类似第 “五” 步中提示)。
>> 数 据 统 计 >>>>>>>>>>
本系统共8人次尝试登入,4人次登入成功,4人次登入失败,登入次数最多的用户是tianqi!
=============================================
- 退出系统(选择 4)
(10 分) 选择 4 时,退出系统:
>>系统已退出>>>>>>>>>> 谢谢使用
=============================================
- 附加分
(5 分) 附加分,项目结构设计合理,代码清晰,注释命名等规范,此项额外加分。
XML文件:user.xml
<?xml version="1.0" encoding="UTF-8"?>
<users><user type="管理员"><username>admin</username><password>123</password></user><user type="普通"><username>zhangsan</username><password>321</password></user><user type="普通"><username>lisi</username><password>321</password></user><user type="普通"><username>wangwu</username><password>321</password></user><user type="普通"><username>zhaoliu</username><password>321</password></user><user type="普通"><username>tianqi</username><password>321</password></user>
</users>
Entity:
User类
import java.io.Serializable;public class User implements Serializable {private String username;private String password;private String userType;public User() {}public User(String username, String password) {this.username=username;this.password=password;}public User(String username, String password, String userType) {this.username=username;this.password=password;this.userType=userType;}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 getUserType() {return userType;}public void setUserType(String userType) {this.userType=userType;}
}
LoginLog类
import java.io.*;
import java.util.ArrayList;
import java.util.List;public class LoginList implements Serializable {private List<LoginLog> loginLogs = new ArrayList<>();public LoginList() {}public LoginList(List<LoginLog> loginLogs) {this.loginLogs=loginLogs;}public List<LoginLog> getLoginLogs() {//从日志中提取对象try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/resources/log.txt"))) {loginLogs=(List<LoginLog>) ois.readObject();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}//将对象存入进新集合并返回该listList<LoginLog> list = loginLogs;return list;}public void setLoginLogs(List<LoginLog> loginLogs) {this.loginLogs=loginLogs;}public void LoginLogsRecord(LoginLog loginLog) throws IOException {//添加登录信息添加集合loginLogs.add(loginLog);//将集合对象写入日志try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src/resources/log.txt"))) {oos.writeObject(loginLogs);}}public void show() throws ClassNotFoundException, IOException {//将日志信息从日志中提取List<LoginLog> list = getLoginLogs();//循环打印for (int i = 0; i < list.size(); i++) {LoginLog log = list.get(i);System.out.println((i + 1) + "\t" + log);}}}
LoginList类
import java.io.*;
import java.util.ArrayList;
import java.util.List;public class LoginList implements Serializable {private List<LoginLog> loginLogs = new ArrayList<>();public LoginList() {}public LoginList(List<LoginLog> loginLogs) {this.loginLogs=loginLogs;}public List<LoginLog> getLoginLogs() {//从日志中提取对象try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/resources/log.txt"))) {loginLogs=(List<LoginLog>) ois.readObject();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}//将对象存入进新集合并返回该listList<LoginLog> list = loginLogs;return list;}public void setLoginLogs(List<LoginLog> loginLogs) {this.loginLogs=loginLogs;}public void LoginLogsRecord(LoginLog loginLog) throws IOException {//添加登录信息添加集合loginLogs.add(loginLog);//将集合对象写入日志try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src/resources/log.txt"))) {oos.writeObject(loginLogs);}}public void show() throws ClassNotFoundException, IOException {//将日志信息从日志中提取List<LoginLog> list = getLoginLogs();//循环打印for (int i = 0; i < list.size(); i++) {LoginLog log = list.get(i);System.out.println((i + 1) + "\t" + log);}}}
service:
UserService类
import entity.LoginList;
import entity.LoginLog;
import entity.User;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;import java.io.IOException;
import java.util.*;public class UserService {private LoginList loginList = new LoginList();//用户登录public User userLogin(String username, String password) throws DocumentException, IOException {SAXReader saxReader=new SAXReader();Document doc=saxReader.read("src/user.xml");Element root=doc.getRootElement();Iterator<Element> it=root.elementIterator();while (it.hasNext()) {Element user=it.next();//获取子元素的type属性String userType=user.attributeValue("type");//获取 <username> <password>元素Element userNameElement=user.element("username");Element passwordElement=user.element("password");//获取元素值String userNameText=userNameElement.getText();String passwordText=passwordElement.getText();//验证//创建当前时刻Date date = new Date();//创建一条登录信息LoginLog loginLog = new LoginLog();//赋值loginLog.setDate(date);loginLog.setUsername(username);loginLog.setUserType(userType);if (userNameText.equals(username) && passwordText.equals(password)) {//登录成功 设置为trueloginLog.setLogin(true);//将该登录信息记录进日志loginList.LoginLogsRecord(loginLog);//返回当前userUser userWhoLogin = new User(username, password, userType);return userWhoLogin;} else if (userNameText.equals(username) && !passwordText.equals(password)) {//登录成功 设置为falseloginLog.setLogin(false);//将该登录信息记录进日志loginList.LoginLogsRecord(loginLog);}}return null;}//查看日志public void show() throws IOException, ClassNotFoundException {loginList.show();}//数据统计public void countMember(){List<LoginLog> logs = loginList.getLoginLogs();int loginTimes=0;int loginSuccess=0;int loginFail=0;int loginMost = 0;String loginMostName="";Map<String,Integer> loginTimesPer = new HashMap<>();for (int i=0; i < logs.size(); i++) {LoginLog loginLog = logs.get(i);//循环一次登录次数+1loginTimes++;//登录成功与登录失败if (loginLog.isLogin() == true){loginSuccess++;}else {loginFail++;}//提取登录信息的用户名String loginName = loginLog.getUsername();//如果Map中包含该用户名,则键为用户名,值+1if (loginTimesPer.containsKey(loginName)){loginTimesPer.put(loginName, loginTimesPer.get(loginName)+1);}else {//如果不包含则新建一个用户名键,将值设置为1loginTimesPer.put(loginName,1);}}//遍历mapIterator it = loginTimesPer.keySet().iterator();while (it.hasNext()){String loginName =(String) it.next();//将次数设置为该键的值int count = loginTimesPer.get(loginName);//如果该值大于最大值,则最大值设为该值,次数最多用户设置为该值的键if (count>loginMost){loginMost = count;loginMostName = loginName;}}System.out.println("本系统共"+loginTimes+"人次尝试登入,"+loginSuccess+"人次登入成功,"+loginFail+"人次登入失败,登入次数最多的用户是"+loginMostName+"登录次数为"+loginMost+"次");}}
View:
Userview:
import entity.User;
import org.dom4j.DocumentException;
import service.UserService;import java.io.IOException;
import java.util.Scanner;public class UserView {public static void main(String[] args) throws DocumentException, IOException, ClassNotFoundException {UserView userView = new UserView();userView.show();}private Scanner input = new Scanner(System.in);//用户的业务逻辑类型private UserService userService = new UserService();//登陆成功之后获取的用户信息private User user;public void show() throws DocumentException, IOException, ClassNotFoundException {while (true) {System.out.println("--银行监管系统--");System.out.println("1.用户登录");System.out.println("2.查看日志");System.out.println("3.数据统计");System.out.println("4.退出系统");System.out.println("请选择:");int type = input.nextInt();switch (type) {case 1:login();break;case 2:loginAdmin();break;case 3:loginCount();break;case 4:System.out.println(">> 系统已退出 >>>>>>>>>>谢谢使用");return;}}}public void login() throws DocumentException, IOException {System.out.println(">> 用户登录 >>>>>>>>>>");System.out.println("请输入账号:");String username = input.next();System.out.println("请输入密码:");String password = input.next();user = userService.userLogin(username,password);if (user!=null){System.out.println("登录成功!");}else {System.out.println("用户密码不正确!");}}public void loginAdmin() throws IOException, ClassNotFoundException {System.out.println(">> 查看日志 >>>>>>>>>>");if (user==null){System.out.println("抱歉,请先登录!");}else if (!user.getUserType().equals("管理员")){System.out.println("抱歉,您不是管理员!");}else {System.out.println("序号\t日期\t\t\t\t\t\t用户\t\t\t结果");userService.show();}}public void loginCount(){if (user==null){System.out.println("抱歉,请先登录!");}else if (!user.getUserType().equals("管理员")){System.out.println("抱歉,您不是管理员!");}else {System.out.println(">> 数据统计 >>>>>>>>>>");userService.countMember();}}}