简易日志管理系统
0.注册(需要验证用户名的可用性)
1.登录
2.写日志(标题,内容,时间)
3.查看日志信息
4.修改日志信息
5.修改日志的部分属性
import java.text.SimpleDateFormat;
import java.util.Date;
//(标题,内容,时间)
public class LogInfo {
private String title;
private String content;
private Date createTime;
public LogInfo() {
// TODO Auto-generated constructor stub
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
return title + ":" + content + " {" + sdf.format(createTime) + "}";
}
}
//用户类
`public class User {
private String nickName;
private String password;
public User() {
super();
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
//操作类
public class LogSystem {
private Scanner scanner;
// 保存已经注册了的用户信息
private User[] users;
// 当前用户数组的下标
private int index;
//定义一个空间保存用户添加的日志
private LogInfo[] logInfos;
private int logIndex;
/*
* 初始化属性
*/
public LogSystem() {
// TODO Auto-generated constructor stub
// 初始化输入流
this.scanner = new Scanner(System.in);
this.users = new User[10];
this.logInfos = new LogInfo[100];
// 启动项目
this.startLogSystem();
}
// 启动项目
public void startLogSystem() {
// 1.登录注册的选择菜单
int select = menuOne();
// 根据用户的菜单选项执行相应的代码
switch (select) {
case 1:
login();
startLogSystem();
break;
case 2:
reg();
startLogSystem();
break;
case 3:
break;
}
}
// 登录选择菜单
private int menuOne() {
System.out.println("1.登录");
System.out.println("2.注册");
System.out.println("3.退出");
System.out.print(">");
int r = scanner.nextInt();
// 取出缓存中多余的 /n
scanner.nextLine();
if (r < 0 || r > 3) {
System.out.println("您输入的选项有误,请重新输入!");
// 外层递归需要接受内层递归的返回值
r = menuOne();
}
return r;
}
// 登录选择菜单
private void logMenu() {
System.out.println("1.写日志");
System.out.println("2.查看日志信息");
System.out.println("3.修改日志信息");
System.out.println("4.修改日志的部分属性");
System.out.println("5.退出");
System.out.print(">");
int r = scanner.nextInt();
// 取出缓存中多余的 /n
scanner.nextLine();
if (r < 0 || r > 5) {
System.out.println("您输入的选项有误,请重新输入!");
// 外层递归需要接受内层递归的返回值
r = menuOne();
}
switch (r) {
case 1: //1.写日志
addLogInfo();
logMenu();
break;
case 2: //2.查看日志信息
showLogInfoByTitle();
logMenu();
break;
case 3: //修改日志信息
updateLogInfo();
logMenu();
break;
case 4:
try {
updateLogInfoAll();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
logMenu();
break;
case 5: //强制结束当前程序!
System.exit(0);
}
}
//选择性修改内容和创建时间
private void updateLogInfoAll() throws ParseException {
//1.需要修改的日志标题
System.out.print("请输入标题:");
String title = scanner.nextLine();
//2.查找日志,打原日志
int i = getLogInfoByTitle(title);
LogInfo logInfo = i >= 0 ? logInfos[i] : null;
if(logInfo == null) {
System.out.println("没有找到标题为 " + title + "的日志。");
return;
}
System.out.println(logInfo);
//3.输入修改后的日志内容
System.out.print("请输入新的内容:");
String content = scanner.nextLine();
System.out.println("content:" + content);
if(content != null && !"".equals(content)) {
logInfo.setContent(content);
}
//4.输入修改后的日志创建时间
System.out.print("请输入新的时间:");
String createTime = scanner.nextLine();
System.out.println("createTime:" + createTime);
//2019/12/10 19:42:46 转 Date
if(createTime != null && !"".equals(createTime)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
logInfo.setCreateTime(sdf.parse(createTime));
}
//5.把修改后的内容持久化保存
logInfos[i] = logInfo;
System.out.println("修改成功!");
}
//修改日志信息
private void updateLogInfo() {
//1.需要修改的日志标题
System.out.print("请输入标题:");
String title = scanner.nextLine();
//2.查找日志,打原日志
int i = getLogInfoByTitle(title);
LogInfo logInfo = i >= 0 ? logInfos[i] : null;
if(logInfo == null) {
System.out.println("没有找到标题为 " + title + "的日志。");
return;
}
System.out.println(logInfo);
//3.输入修改后的日志内容
System.out.print("请输入新的内容:");
logInfo.setContent(scanner.nextLine());
//4.进行修改
logInfos[i] = logInfo;
System.out.println("修改成功!");
}
//添加日志
private void addLogInfo() {
LogInfo logInfo = new LogInfo();
//1.添加日志标题
System.out.print("请输入标题:");
logInfo.setTitle(scanner.nextLine());
//2.添加日志内容
System.out.print("请输入内容:");
logInfo.setContent(scanner.nextLine());
//3.添加日志创建时间
logInfo.setCreateTime(new Date());
//把用户添加的日志加入全局属性,进行长久保存
this.logInfos[logIndex++] = logInfo;
System.out.println("日志添加成功!");
}
//更具标题搜索日志
public void showLogInfoByTitle() {
//1.用混输入查询日志标题
System.out.print("请输入您要查询的日志标题:");
String title = scanner.nextLine();
//2.在日志信息中查找对应的日志信息
int i = getLogInfoByTitle(title);
LogInfo logInfo = i >= 0 ? logInfos[i] : null;
//3.显示查找到的日志信息
if(logInfo == null) {
System.out.println("没有标题为 " + title + " 的日志!");
}else {
//输出语句打印 对象 时会默认调当前对象的toString()方法
System.out.println(logInfo);
}
}
//更具标题查找系统中的日志
private int getLogInfoByTitle(String title) {
for (int i = 0; i < logInfos.length; i++) {
if(logInfos[i] == null) {
break;
}
if(logInfos[i].getTitle().equals(title)) {
return i;
}
}
return -1;
}
// 注册
private void reg() {
User user = new User();
System.out.print("请输入注册的昵称:");
// String nickName = scanner.nextLine();
user.setNickName(scanner.nextLine());
System.out.print("请输入密码:");
String pwd = scanner.nextLine();
System.out.print("请确认密码:");
String rePwd = scanner.nextLine();
if (pwd.equals(rePwd)) {
user.setPassword(pwd);
this.users[index++] = user;
System.out.println("注册成功!");
} else {
System.out.println("两次密码不相同,注册失败!");
}
}
private void login() {
System.out.print("请输入您的昵称:");
String nickName = scanner.nextLine();
// 接收从数据中查询的用户
User user = null;
for (int i = 0; i < users.length; i++) {
if (this.users[i] == null) {
break;
}
if (nickName.equals(this.users[i].getNickName())) {
user = this.users[i];
}
}
if (user == null) {
System.out.println("用户名不存在!");
return;
}
// 用户名输入成功!
System.out.print("请输入您的密码:");
String password = scanner.nextLine();
if (password.equals(user.getPassword())) {
// 跳转日志菜单(主页)
logMenu();
} else {
System.out.println("你输入的密码有误!请检查后再试!");
}
}
}
//验证类
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//启动系统
new LogSystem();
}
}