package com.huayu.demo.mb; import java.util.Objects; public class UserInfo { private String name; private String date; private String title; private String content; public UserInfo() { } public UserInfo(String name, String date, String title, String content) { this.name = name; this.date = date; this.title = title; this.content = content; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } 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; } @Override public String toString() { return "UserInfo{" + "name='" + name + '\'' + ", date='" + date + '\'' + ", title='" + title + '\'' + ", content='" + content + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; UserInfo userInfo = (UserInfo) o; return Objects.equals(name, userInfo.name) && Objects.equals(date, userInfo.date) && Objects.equals(title, userInfo.title) && Objects.equals(content, userInfo.content); } @Override public int hashCode() { return Objects.hash(name, date, title, content); } }
package com.huayu.demo.mb; import java.util.ArrayList; import java.util.Scanner; public class Test { public static final ArrayList<UserInfo> list=new ArrayList<>(); public static final Scanner input=new Scanner(System.in); public static final Message message=new MessageImpl(); public static void main(String[] args) { while (true){ System.out.println("---欢迎登录留言板系统---"); System.out.println("-------主菜单-------"); System.out.println("1.查询留言"); System.out.println("2.添加留言"); System.out.println("3.退出系统"); System.out.println("请选择"); int choice= input.nextInt(); switch (choice){ case 1: message.queryMessage(list); break; case 2: message.addMessage(list); break; case 3: message.exitSystem(); break; } } } }
package com.huayu.demo.mb; import java.util.ArrayList; public interface Message { void queryMessage(ArrayList<UserInfo>list); void addMessage(ArrayList<UserInfo> list); void exitSystem(); }
package com.huayu.demo.mb; import java.util.ArrayList; import java.util.Scanner; public class MessageImpl implements Message{ public static final Scanner input=new Scanner(System.in); @Override public void queryMessage(ArrayList<UserInfo> list) { if(list.size()==0){ System.out.println("无信息"); } else { for (int i = 0; i < list.size(); i++) { UserInfo userInfo=list.get(i); System.out.println("留言人:"+userInfo.getName()+"留言时间:"+"\n"+userInfo.getDate()+"\n"+"留言标题:"+userInfo.getTitle()+"\n"+"留言内容:"+userInfo.getContent()); } } } @Override public void addMessage(ArrayList<UserInfo> list) { System.out.println("留言人:"); String name=input.nextLine(); System.out.println("留言时间:"); String date=input.nextLine(); System.out.println("留言标题:"); String title=input.nextLine(); System.out.println("留言内容:"); String content=input.nextLine(); UserInfo userInfo = new UserInfo(name,date,title,content); list.add(userInfo); System.out.println("添加成功"); } @Override public void exitSystem() { System.out.println("谢谢使用"); System.exit(0); } }