大二上学期学校开设过Java课程,但是我没怎么认真听,现在大二第二学期刚刚开始,想要自己慢慢学习Java,先复习上学期老师要求完成的任务,这篇文章就算是一个开始吧。

一、日志实体类的创建
实体类创建需要的属性
Java学习【1】——用户输入的日志数据信息采集以及获取当前系统时间的方法

import java.util.Date;
public class LogRec {
    // ID标识
    private int id;
    // 时间
    private Date time;
    // 地址
    private String address;
    // 状态
    private int type;
    // 登录用户名
    private String userName;
    // 登录IP
    private String ip;
    /**
     * 登录状态 包含LOG_IN,LOG_OUT
     */
    private int logType;
    // LOG_IN
    public static final int LOG_IN = 1;
    // LOG_OUT
    public static final int LOG_OUT = 0;

    // 状态常量

    public static final int GATHER = 1;
    public static final int MATCH = 2;
    public static final int RECORD = 3;
    public static final int SEND = 4;
    public static final int RECEIVE = 5;
    public static final int WRITE = 6;
    public static final int SAVE = 7;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public Date getTime() {
        return time;
    }
    public void setTime(Date time) {
        this.time = time;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public int getType() {
        return type;
    }
    public void setType(int type) {
        this.type = type;
    }
    public String getUser() {
        return userName;
    }
    public void setUser(String user) {
        this.userName = user;
    }
    public String getIp() {
        return ip;
    }
    public void setIp(String ip) {
        this.ip = ip;
    }
    public int getLogType() {
        return logType;
    }
    public void setLogType(int logType) {
        this.logType = logType;
    }
    public LogRec(int id, Date time, String address, int type, String user, String ip, int logType) {
        super();
        this.id = id;
        this.time = time;
        this.address = address;
        this.type = type;
        this.userName = user;
        this.ip = ip;
        this.logType = logType;
    }

    public LogRec() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public String toString() {
        return "LogRec [id=" + id + ", time=" + time + ", address=" + address + ", type=" + type + ", user=" 
    + userName+ ", ip=" + ip + ", logType=" + logType + "]";
    }
}

二、日志数据信息采集及显示

import java.util.Date;
import java.util.Scanner;


public class LogRecServe {

    public LogRec inputLog() {
        Scanner scanner = new Scanner(System.in);
        // 输入ID标识
        System.out.println("请输入用户ID标识");
        // 接受键盘输入的整数
        int id = scanner.nextInt();
        // 获取系统当前时间
        Date dateNow = new Date();
        // 输入地址
        System.out.println("请输入地址");
        // 接受键盘输入地址
        String address = scanner.next();
        // 设置数据状态为采集状态
        int type = LogRec.GATHER;
        // 用户名输入
        System.out.println("请输入用户名");
        // 接受用用户名输入
        String userName = scanner.next();
        // 主机ip输入
        System.out.println("请输入主机IP");
        // 接受用户输入的主机IP地址
        String ip = scanner.next();
        // 登录状态输入 :登录状态:1=>登录,0=>登出
        System.out.println("请输入登录状态:1=>登录,0=>登出");
        // 接受输入的登录状态
        int logType = scanner.nextInt();
        // 实例化logRec对象
        LogRec logRec = new LogRec(id, dateNow, address, type, userName, ip, logType);

        return logRec;

    }

    public void showLog(LogRec... logRecs) {
        // forEach循环
        for (LogRec e : logRecs) {
            if (e != null) {
                System.out.println(e.toString());
            }
        }
    }

}

三、创建测试类测试编写功能

public class LogRecTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // 创建一个记录日志业务对象
        LogRecServe logRecServe = new LogRecServe();
        // 只有一个对象时可用这种方式创建
        // LogRec logRec = new LogRec();
        // logRec = logRecServe.inputLog();
        // logRecServe.showLog(logRec);
        System.out.println("------------------------------------------------");

        // 当有多个日志信息时,采用数组方式创建对象
        // 创建一个日志对象数组,用于存放采集的日志信息
         LogRec[] logRecs = new LogRec[2];
         for (int i = 0; i < logRecs.length; i++) {
         System.out.println("第" + (i + 1) + "个日志数据采集");
         logRecs[i] = logRecServe.inputLog();
         }
         // 显示日志信息
         logRecServe.showLog(logRecs);
    }

}

测试了一下结果:Java学习【1】——用户输入的日志数据信息采集以及获取当前系统时间的方法
四、获取当前系统时间的办法
看《java核心教程》里有个打印当前系统时间的办法,就去度娘了一下相关的办法

import java.text.SimpleDateFormat;
import java.util.Date;


public class Time {
	public static void main(String[] args) {
		Date time=new Date();
		SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(dateformat.format(time));
	}
	
}

Java学习【1】——用户输入的日志数据信息采集以及获取当前系统时间的方法

相关文章:

  • 2022-12-23
  • 2021-07-20
  • 2021-10-05
  • 2021-08-02
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
  • 2021-12-25
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案