综合练习一

使用到的知识点:XML,写入file文件,读取file文件

  • 背景

某银行监管系统,需要设计并实现用户登入记录功能,每个用户登入系统时,系统自动记录登入用户的账户、登入时间、登入失败成功与否信息等,普通用户只能登入登出,管理员可以登入后查看日志及分析统计信息等。

  • 用户账户信息存储设计

(5 分) 为了系统设计轻便,采用 XML 文件记录用户账户信息,要求可以记录有序号、用户账号、密码、身份 (管理员、普通) 等信息。

  • 主要功能界面

(10 分) 主要功能界面要求参考如下:

--银行监管系统--

  1. 用户登录
  2. 查看日志
  3. 数据统计
  4. 退出系统
  • 用户登录操作(选择 1)

============================================= 

(25 分) 选择 1 时,进行用户登入操作:

>> 用户登录 >>>>>>>>>>

请输入账号 :

请输入密码 :

============================================= 

输入完点击回车后,判断用户名密码是否正确,不论登入成功与否、都需要在控制台给予结果提示,并在日志文件中写入登入记录,以备查询。

============================================= 

  • 查看日志(选择 2)

(25 分) 选择 2 时,必须确保先用管理员账号登入成功。

>> 查 看 日 志 >>>>>>>>>> 抱歉,请先登录!

============================================= 

>> 查 看 日 志 >>>>>>>>>>  抱歉,您不是管理员!

============================================= 

>> 查 看 日 志 >>>>>>>>>>

您好,XXX, 系统全部登录日志如下:

序号     日期                用户        结果

  1. 2019-03-04 13:23:00  admin (管理员)  登入成功
  2. 2019-12-04 23:15:00  lisi     (普通)  登入成功
  3. 2019-12-12 15:33:00  wangmu (普通)  登入成功
  4. 2019-12-13 09:27:00  zhaoliu(普通)  登入成功
  5. 2019-12-14 05:10:00  tianqi (普通)  登入失败
  6. 2019-12-14 05:11:00  tianqi (普通)  登入失败
  7. 2019-12-14 05:13:00  tianqi (普通)  登入失败
  8. 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();
        }

        //将对象存入进新集合并返回该list
        List<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();
        }

        //将对象存入进新集合并返回该list
        List<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)) {
                //登录成功 设置为true
                loginLog.setLogin(true);
                //将该登录信息记录进日志
                loginList.LoginLogsRecord(loginLog);
                //返回当前user
                User userWhoLogin = new User(username, password, userType);
                return userWhoLogin;
            } else if (userNameText.equals(username) && !passwordText.equals(password)) {
                //登录成功 设置为false
                loginLog.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);
            //循环一次登录次数+1
            loginTimes++;

            //登录成功与登录失败
            if (loginLog.isLogin() == true){
                loginSuccess++;
            }else {
                loginFail++;
            }

            //提取登录信息的用户名
            String loginName = loginLog.getUsername();
            //如果Map中包含该用户名,则键为用户名,值+1
            if (loginTimesPer.containsKey(loginName)){
                loginTimesPer.put(loginName, loginTimesPer.get(loginName)+1);
            }else {
                //如果不包含则新建一个用户名键,将值设置为1
                loginTimesPer.put(loginName,1);
            }
        }

        //遍历map
        Iterator 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();
        }

    }


}

附录:

dom4j

https://download.csdn.net/download/m0_62696265/90707510?spm=1001.2014.3001.5503

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值