完成一个学员日志管理系统,实现以下功能:学员登录,学员添加,发表日志,查看所有学员日志,查看指定学员日志,以及查讯所有学员信息。
//学员类
public class Student {
private String name; //姓名
private String password; //密码
private String sex; //性别
private ArrayList<Daily> list; //日志集合
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String name, String password, String sex) {
super();
this.name = name;
this.password = password;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String toString(){
return name+"/*****/"+sex;
}
}
//日志类
public class Daily {
private long id; //标志位
private String content;//内容
private Student stu; //学员
public Daily() {
// TODO Auto-generated constructor stub
}
public Daily(long id, String content, Student stu) {
super();
this.id = id;
this.content = content;
this.stu = stu;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
this.stu = stu;
}
public String toString()
{
return id+"\t"+stu.getName()+"\t"+content;
}
}
工具类,封装系统输出
public class Tools {
public static void p(Object obj){
System.out.println(obj);
}
}
public class OAMain {
//存储登录的学生
private static Student loginStu;
//存储所有学员的容器
private static ArrayList<Student> students = new ArrayList<Student>();
//存储所有日志的容器
private static ArrayList<Daily> dailies = new ArrayList<Daily>();
public void start(){
Tools.p("**********欢迎使用SOFTEEM学员日志管理系统V1.0**********");
Tools.p("[1]学员登录");
Tools.p("[2]学员添加");
Tools.p("[3]日志发布");
Tools.p("[4]查看所有日志");
Tools.p("[5]查看指定学员的日志");
Tools.p("[6]学员列表");
Tools.p("[0]系统退出");
Tools.p("****************************************************");
Tools.p("请输入指令:");
enter();
}
public void enter(){
//读取控制台的输入
Scanner sc = new Scanner(System.in);
int comm = sc.nextInt();
switch(comm){
case 1:
login();
break;
case 2:
addStudent();
break;
case 3:
submitDaily();
break;
case 4:
listDaily();
break;
case 5:
break;
case 6:
listStudent();
break;
case 0:
Tools.p("谢谢使用,再见!");
System.exit(0);
break;
default:
Tools.p("请输入正确指令");
enter();
break;
}
}
//日志列表
private void listDaily() {
Tools.p("********日志号\t发布人\t内容**********");
for (Daily d : dailies) {
Tools.p(d);
}
enter();
}
//登录
private void login() {
if(checkLogin()){
Tools.p("已登录,请直接输入操作指令!");
enter();
}else{
Tools.p("请输入账号密码(如:admin/123)");
Scanner sc = new Scanner(System.in);
String[] info = sc.nextLine().split("/");
boolean isSuccess = false;//假设登录失败
for(Student s:students){
if(s.getName().equals(info[0]) && s.getPassword().equals(info[1])){
isSuccess = true;
//保存当前登录的学员对象
loginStu = s;
break;
}
}
if(isSuccess){
Tools.p("登录成功!");
enter();
}else{
Tools.p("登录失败");
login();
}
}
}
//学员列表
private void listStudent() {
Tools.p("姓名/密码/性别");
for (Student s : students) {
Tools.p(s);
}
enter();
}
//添加学生
private void addStudent() {
Tools.p("请输入学生信息(如:张三/123456/男)");
Scanner sc = new Scanner(System.in);
String info = sc.nextLine();
//使用“/”截取字符串内部的数据
String[] s = info.split("/");
//创建学员对象
Student stu = new Student(s[0], s[1], s[2]);
//将学员对象存储到容器中
students.add(stu);
Tools.p("添加成功!");
enter();
}
//日志提交
private void submitDaily() {
//1.检查登录与否
if(checkLogin()){
Tools.p("请输入日志内容");
Scanner sc = new Scanner(System.in);
String content = sc.nextLine();
Daily daily = new Daily(System.currentTimeMillis(),content,loginStu);
dailies.add(daily);
Tools.p("日志发表成功!");
}else{
Tools.p("请登录后再试!");
}
enter();
}
//检测学员是否登录
public boolean checkLogin(){
if(loginStu == null){
return false;
}
return true;
}
public static void main(String[] args) {
new OAMain().start();
}
}
不早了,洗洗睡了。。。