123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- package com.songzh.librarymanager.controller;
- import com.songzh.librarymanager.error.*;
- import com.songzh.librarymanager.model.*;
- import com.songzh.librarymanager.viewer.Viewer;
- public class Controller {
- private final Viewer viewer;
- private final Library libary;
- private Book book;
- private Author author;
- private Status status;
- static Controller createWithInit() {
- var ctrl = new Controller();
- ctrl.init();
- return ctrl;
- }
- public Controller() {
- this.libary = new Library();
- this.viewer = new Viewer();
- this.status = Status.Library;
- }
- private void init() {
- var author = new Author("佳程");
- var book1 = new SCIBook(author, "如何成为顶级前端?", "20040101-xxx222yyz");
- var book2 = new ScoreBook(author, "前端保姆教程", 98);
- author.addBook(book1);
- author.addBook(book2);
- this.libary.addAuthor(author);
- }
- private void mainloop() {
- for (;;) {
- try {
- this.viewer.showInfo(this.header());
- this.viewer.showInfo(this.menu());
- this.viewer.showInfo(this.footer());
- var opt = this.viewer.getMenuInput();
- switch (this.status) {
- case Library -> {
- LIBARYOPT:
- switch (opt) {
- case 1 -> {
- try {
- this.book = this.libary.getBook(this.viewer.getBookName());
- this.status = Status.Book;
- } catch (BookNotFound bookNotFound) {
- this.viewer.showInfo(String.format("书本未找到:%s", bookNotFound.bookName()));
- }
- }
- case 2 -> {
- try {
- this.author = this.libary.getAuthor(this.viewer.getAuthorName());
- this.status = Status.Author;
- } catch (AuthorNotFound authorNotFound) {
- this.viewer.showInfo(String.format("作者未找到:%s", authorNotFound.authorName()));
- }
- }
- case 3 -> {
- Author author;
- try {
- author = this.libary.getAuthor(this.viewer.getAuthorName());
- } catch (AuthorNotFound authorNotFound) {
- this.viewer.showInfo(String.format("作者未找到:%s", authorNotFound.authorName()));
- break LIBARYOPT;
- }
- var bookName = this.viewer.getBookName();
- var bookType = this.viewer.getBookType();
- Book book;
- switch (bookType) {
- case "SCI" -> {
- var sciNumber = this.viewer.getSCINumber();
- book = new SCIBook(author, bookName, sciNumber);
- }
- case "SCORE" -> {
- int score;
- try {
- score = this.viewer.getScore();
- } catch (NumberFormatException e) {
- this.viewer.showInfo(String.format("评分分数解析错误:%s", e.getMessage()));
- break LIBARYOPT;
- }
- book = new ScoreBook(author, bookName, score);
- }
- default -> {
- this.viewer.showInfo(String.format("书本类型不支持:%s", bookType));
- break LIBARYOPT;
- }
- }
- libary.addBook(book);
- }
- case 5 -> {
- var author = this.libary.authors();
- author.forEach((b)->{
- this.viewer.showInfo(String.format("* %s", b));
- });
- }
- case 6 -> {
- var books = this.libary.books();
- books.forEach((b)->{
- this.viewer.showInfo(String.format("* %s", b));
- });
- }
- default -> {
- this.viewer.showInfo(String.format("不支持的操作:%d", opt));
- }
- }
- }
- case Author -> {
- AUTHOROPT:
- switch (opt) {
- case 1 -> {
- this.viewer.showInfo(this.author.name());
- var books = this.author.books();
- books.forEach((b)->{
- this.viewer.showInfo(String.format("* %s", b));
- });
- }
- case 2 -> {
- try {
- this.book = this.author.getBook(this.viewer.getBookName());
- this.status = Status.Book;
- } catch (BookNotFound bookNotFound) {
- this.viewer.showInfo(String.format("书本未找到:%s", bookNotFound.bookName()));
- }
- }
- case 3 -> {
- var bookName = this.viewer.getBookName();
- var bookType = this.viewer.getBookType();
- Book book;
- switch (bookType) {
- case "SCI" -> {
- var sciNumber = this.viewer.getSCINumber();
- book = new SCIBook(this.author, bookName, sciNumber);
- }
- case "SCORE" -> {
- int score;
- try {
- score = this.viewer.getScore();
- } catch (NumberFormatException e) {
- this.viewer.showInfo(String.format("评分分数解析错误:%s", e.getMessage()));
- break AUTHOROPT;
- }
- book = new ScoreBook(this.author, bookName, score);
- }
- default -> {
- this.viewer.showInfo(String.format("书本类型不支持:%s", bookType));
- break AUTHOROPT;
- }
- }
- this.author.addBook(book);
- }
- default -> {
- this.viewer.showInfo(String.format("不支持的操作:%d", opt));
- }
- }
- }
- case Book -> {
- BOOKOPT:
- switch (opt) {
- case 1 -> {
- this.viewer.showInfo(this.book.identity());
- }
- case 2 -> {
- this.author = this.book.author();
- this.status = Status.Author;
- }
- default -> {
- this.viewer.showInfo(String.format("不支持的操作:%d", opt));
- }
- }
- }
- }
- } catch (Exit ignored) {
- return;
- } catch (Cancel ignored) {
- this.viewer.showInfo("已取消执行");
- } catch (Back ignored) {
- this.status = Status.Library;
- }
- }
- }
- private String menu() {
- return switch (this.status) {
- case Library -> """
- 1) 查找图书
- 2) 查找作者
- 3) 新增图书
- 4) 新增作者
- 5) 查看图书馆作者列表
- 6) 查看图书馆图书列表""";
- case Book -> """
- 1) 查看图书信息
- 2) 查找作者""";
- case Author -> """
- 1) 查看作者信息
- 2) 查找图书
- 3) 新增图书""";
- };
- }
- private String header() {
- return switch (this.status) {
- case Library -> "图书馆模式";
- case Book -> String.format("图书:%s", this.book.identity());
- case Author -> String.format("作者:%s", this.book.identity());
- };
- }
- private String footer() {
- return """
- 输入 "q" 退出,输入 "n" 返回图书馆模式。""";
- }
- public static void main(String[] args) {
- System.out.println("""
- 欢迎使用 顶级前端 Java!!! 的图书馆管理系统。
- 本系统基于内存管理图书,不做任何持久化处理。
- 谢谢!
- """);
- Controller.createWithInit().mainloop();
- }
- }
|