|
|
|
|
@ -1,299 +0,0 @@
|
|
|
|
|
package com.ssm.zy;
|
|
|
|
|
|
|
|
|
|
import org.w3c.dom.Document;
|
|
|
|
|
import org.w3c.dom.Element;
|
|
|
|
|
import org.w3c.dom.NodeList;
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
|
|
// ====================== Model 层 ======================
|
|
|
|
|
class User {
|
|
|
|
|
private int id;
|
|
|
|
|
private String name;
|
|
|
|
|
private String pwd;
|
|
|
|
|
public User(int id, String name, String pwd) {
|
|
|
|
|
this.id = id;
|
|
|
|
|
this.name = name;
|
|
|
|
|
this.pwd = pwd;
|
|
|
|
|
}
|
|
|
|
|
public int getId() {return id;}
|
|
|
|
|
public String getName() {return name;}
|
|
|
|
|
public String getPwd() {return pwd;}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Job {
|
|
|
|
|
private int id;
|
|
|
|
|
private String title;
|
|
|
|
|
private User publisher;
|
|
|
|
|
public Job(int id, String title, User publisher) {
|
|
|
|
|
this.id = id;
|
|
|
|
|
this.title = title;
|
|
|
|
|
this.publisher = publisher;
|
|
|
|
|
}
|
|
|
|
|
public void show() {
|
|
|
|
|
System.out.println("兼职[" + id + "]:" + title + " | 发布者:" + publisher.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Post {
|
|
|
|
|
private int id;
|
|
|
|
|
private String content;
|
|
|
|
|
private User author;
|
|
|
|
|
public Post(int id, String content, User author) {
|
|
|
|
|
this.id = id;
|
|
|
|
|
this.content = content;
|
|
|
|
|
this.author = author;
|
|
|
|
|
}
|
|
|
|
|
public void show() {
|
|
|
|
|
System.out.println("帖子[" + id + "]:" + content + " | 作者:" + author.getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ====================== Service 层(业务逻辑) ======================
|
|
|
|
|
class UserService {
|
|
|
|
|
List<User> users = new ArrayList<>();
|
|
|
|
|
int uid = 1;
|
|
|
|
|
|
|
|
|
|
public UserService() {
|
|
|
|
|
users.add(new User(uid++, "张三", "123"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public User login(String name, String pwd) {
|
|
|
|
|
for (User u : users) {
|
|
|
|
|
if (u.getName().equals(name) && u.getPwd().equals(pwd)) return u;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void register(String name, String pwd) {
|
|
|
|
|
users.add(new User(uid++, name, pwd));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class JobService {
|
|
|
|
|
List<Job> jobs = new ArrayList<>();
|
|
|
|
|
int jid = 1;
|
|
|
|
|
|
|
|
|
|
public void publish(String title, User publisher) {
|
|
|
|
|
jobs.add(new Job(jid++, title, publisher));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Job> getAll() {
|
|
|
|
|
return jobs;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class PostService {
|
|
|
|
|
List<Post> posts = new ArrayList<>();
|
|
|
|
|
int pid = 1;
|
|
|
|
|
|
|
|
|
|
public void publish(String content, User author) {
|
|
|
|
|
posts.add(new Post(pid++, content, author));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Post> getAll() {
|
|
|
|
|
return posts;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class AdService {
|
|
|
|
|
List<String> ads = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
public void addAd(String content) {
|
|
|
|
|
ads.add(content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<String> getAll() {
|
|
|
|
|
return ads;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class NoticeService {
|
|
|
|
|
List<String> notices = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
public void addNotice(String title, String content) {
|
|
|
|
|
notices.add("【" + title + "】" + content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<String> getAll() {
|
|
|
|
|
return notices;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ====================== View 层(菜单/输出) ======================
|
|
|
|
|
class View {
|
|
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
|
|
|
|
|
|
public void showMainMenu() {
|
|
|
|
|
System.out.println("\n========== 主菜单 ==========");
|
|
|
|
|
System.out.println("1. 用户管理 2. 兼职管理 3. 帖子管理 4. 广告管理 5. 公告管理 6. 退出");
|
|
|
|
|
System.out.print("请输入:");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void showCarouselEnd() {
|
|
|
|
|
System.out.println("========== 轮播播放完毕 ==========\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int inputInt() {
|
|
|
|
|
return sc.nextInt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String inputStr() {
|
|
|
|
|
return sc.nextLine();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ====================== Controller 层(调度) ======================
|
|
|
|
|
public class CampusSystemMVC {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
new MainController().start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MainController {
|
|
|
|
|
UserService userService = new UserService();
|
|
|
|
|
JobService jobService = new JobService();
|
|
|
|
|
PostService postService = new PostService();
|
|
|
|
|
AdService adService = new AdService();
|
|
|
|
|
NoticeService noticeService = new NoticeService();
|
|
|
|
|
View view = new View();
|
|
|
|
|
User loginUser = null;
|
|
|
|
|
|
|
|
|
|
List<String> carousel = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
public void start() {
|
|
|
|
|
loadXML();
|
|
|
|
|
startCarousel3Times();
|
|
|
|
|
mainLoop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void loadXML() {
|
|
|
|
|
try {
|
|
|
|
|
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
|
|
|
|
Document doc = builder.parse("src/campus.xml");
|
|
|
|
|
doc.getDocumentElement().normalize();
|
|
|
|
|
|
|
|
|
|
NodeList carNodes = doc.getElementsByTagName("item");
|
|
|
|
|
for (int i = 0; i < carNodes.getLength(); i++)
|
|
|
|
|
carousel.add(carNodes.item(i).getTextContent());
|
|
|
|
|
|
|
|
|
|
NodeList adNodes = doc.getElementsByTagName("ad");
|
|
|
|
|
for (int i = 0; i < adNodes.getLength(); i++)
|
|
|
|
|
adService.addAd(adNodes.item(i).getTextContent());
|
|
|
|
|
|
|
|
|
|
NodeList noticeNodes = doc.getElementsByTagName("notice");
|
|
|
|
|
for (int i = 0; i < noticeNodes.getLength(); i++) {
|
|
|
|
|
Element e = (Element) noticeNodes.item(i);
|
|
|
|
|
noticeService.addNotice(e.getAttribute("title"), e.getTextContent());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
carousel.add("系统默认轮播");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void startCarousel3Times() {
|
|
|
|
|
new Thread(() -> {
|
|
|
|
|
System.out.println("========== 校园兼职网轮播 ==========");
|
|
|
|
|
int count = 0;
|
|
|
|
|
int index = 0;
|
|
|
|
|
while (count < 3) {
|
|
|
|
|
System.out.println("轮播:" + carousel.get(index));
|
|
|
|
|
index++;
|
|
|
|
|
if (index >= carousel.size()) {
|
|
|
|
|
index = 0;
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
try {Thread.sleep(1500);} catch (Exception ignored) {}
|
|
|
|
|
}
|
|
|
|
|
view.showCarouselEnd();
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void mainLoop() {
|
|
|
|
|
while (true) {
|
|
|
|
|
view.showMainMenu();
|
|
|
|
|
int op = view.inputInt();
|
|
|
|
|
view.inputStr();
|
|
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
|
case 1: userMenu(); break;
|
|
|
|
|
case 2: jobMenu(); break;
|
|
|
|
|
case 3: postMenu(); break;
|
|
|
|
|
case 4: adMenu(); break;
|
|
|
|
|
case 5: noticeMenu(); break;
|
|
|
|
|
case 6: System.out.println("退出"); return;
|
|
|
|
|
default: System.out.println("输入错误");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void userMenu() {
|
|
|
|
|
System.out.println("1.登录 2.注册");
|
|
|
|
|
int op = view.inputInt(); view.inputStr();
|
|
|
|
|
if (op == 1) {
|
|
|
|
|
System.out.print("用户名:"); String name = view.inputStr();
|
|
|
|
|
System.out.print("密码:"); String pwd = view.inputStr();
|
|
|
|
|
loginUser = userService.login(name, pwd);
|
|
|
|
|
System.out.println(loginUser != null ? "登录成功" : "失败");
|
|
|
|
|
} else {
|
|
|
|
|
System.out.print("用户名:"); String name = view.inputStr();
|
|
|
|
|
System.out.print("密码:"); String pwd = view.inputStr();
|
|
|
|
|
userService.register(name, pwd);
|
|
|
|
|
System.out.println("注册成功");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void jobMenu() {
|
|
|
|
|
if (loginUser == null) {System.out.println("请登录"); return;}
|
|
|
|
|
System.out.println("1.发布 2.查看");
|
|
|
|
|
int op = view.inputInt(); view.inputStr();
|
|
|
|
|
if (op == 1) {
|
|
|
|
|
System.out.print("标题:");
|
|
|
|
|
jobService.publish(view.inputStr(), loginUser);
|
|
|
|
|
System.out.println("发布成功");
|
|
|
|
|
} else {
|
|
|
|
|
for (Job j : jobService.getAll()) j.show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void postMenu() {
|
|
|
|
|
if (loginUser == null) {System.out.println("请登录"); return;}
|
|
|
|
|
System.out.println("1.发布 2.查看");
|
|
|
|
|
int op = view.inputInt(); view.inputStr();
|
|
|
|
|
if (op == 1) {
|
|
|
|
|
System.out.print("内容:");
|
|
|
|
|
postService.publish(view.inputStr(), loginUser);
|
|
|
|
|
System.out.println("发布成功");
|
|
|
|
|
} else {
|
|
|
|
|
for (Post p : postService.getAll()) p.show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void adMenu() {
|
|
|
|
|
System.out.println("1.查看 2.发布");
|
|
|
|
|
int op = view.inputInt(); view.inputStr();
|
|
|
|
|
if (op == 2) {
|
|
|
|
|
System.out.print("广告内容:");
|
|
|
|
|
adService.addAd(view.inputStr());
|
|
|
|
|
System.out.println("发布成功");
|
|
|
|
|
} else {
|
|
|
|
|
for (String s : adService.getAll()) System.out.println(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void noticeMenu() {
|
|
|
|
|
System.out.println("1.查看 2.发布");
|
|
|
|
|
int op = view.inputInt(); view.inputStr();
|
|
|
|
|
if (op == 2) {
|
|
|
|
|
System.out.print("标题:"); String t = view.inputStr();
|
|
|
|
|
System.out.print("内容:"); String c = view.inputStr();
|
|
|
|
|
noticeService.addNotice(t, c);
|
|
|
|
|
System.out.println("发布成功");
|
|
|
|
|
} else {
|
|
|
|
|
for (String s : noticeService.getAll()) System.out.println(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|