Controller.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package com.songzh.librarymanager.controller;
  2. import com.songzh.librarymanager.error.*;
  3. import com.songzh.librarymanager.model.*;
  4. import com.songzh.librarymanager.viewer.Viewer;
  5. public class Controller {
  6. private final Viewer viewer;
  7. private final Library libary;
  8. private Book book;
  9. private Author author;
  10. private Status status;
  11. static Controller createWithInit() {
  12. var ctrl = new Controller();
  13. ctrl.init();
  14. return ctrl;
  15. }
  16. public Controller() {
  17. this.libary = new Library();
  18. this.viewer = new Viewer();
  19. this.status = Status.Library;
  20. }
  21. private void init() {
  22. var author = new Author("佳程");
  23. var book1 = new SCIBook(author, "如何成为顶级前端?", "20040101-xxx222yyz");
  24. var book2 = new ScoreBook(author, "前端保姆教程", 98);
  25. author.addBook(book1);
  26. author.addBook(book2);
  27. this.libary.addAuthor(author);
  28. }
  29. private void mainloop() {
  30. for (;;) {
  31. try {
  32. this.viewer.showInfo(this.header());
  33. this.viewer.showInfo(this.menu());
  34. this.viewer.showInfo(this.footer());
  35. var opt = this.viewer.getMenuInput();
  36. switch (this.status) {
  37. case Library -> {
  38. LIBARYOPT:
  39. switch (opt) {
  40. case 1 -> {
  41. try {
  42. this.book = this.libary.getBook(this.viewer.getBookName());
  43. this.status = Status.Book;
  44. } catch (BookNotFound bookNotFound) {
  45. this.viewer.showInfo(String.format("书本未找到:%s", bookNotFound.bookName()));
  46. }
  47. }
  48. case 2 -> {
  49. try {
  50. this.author = this.libary.getAuthor(this.viewer.getAuthorName());
  51. this.status = Status.Author;
  52. } catch (AuthorNotFound authorNotFound) {
  53. this.viewer.showInfo(String.format("作者未找到:%s", authorNotFound.authorName()));
  54. }
  55. }
  56. case 3 -> {
  57. Author author;
  58. try {
  59. author = this.libary.getAuthor(this.viewer.getAuthorName());
  60. } catch (AuthorNotFound authorNotFound) {
  61. this.viewer.showInfo(String.format("作者未找到:%s", authorNotFound.authorName()));
  62. break LIBARYOPT;
  63. }
  64. var bookName = this.viewer.getBookName();
  65. var bookType = this.viewer.getBookType();
  66. Book book;
  67. switch (bookType) {
  68. case "SCI" -> {
  69. var sciNumber = this.viewer.getSCINumber();
  70. book = new SCIBook(author, bookName, sciNumber);
  71. }
  72. case "SCORE" -> {
  73. int score;
  74. try {
  75. score = this.viewer.getScore();
  76. } catch (NumberFormatException e) {
  77. this.viewer.showInfo(String.format("评分分数解析错误:%s", e.getMessage()));
  78. break LIBARYOPT;
  79. }
  80. book = new ScoreBook(author, bookName, score);
  81. }
  82. default -> {
  83. this.viewer.showInfo(String.format("书本类型不支持:%s", bookType));
  84. break LIBARYOPT;
  85. }
  86. }
  87. libary.addBook(book);
  88. }
  89. case 5 -> {
  90. var author = this.libary.authors();
  91. author.forEach((b)->{
  92. this.viewer.showInfo(String.format("* %s", b));
  93. });
  94. }
  95. case 6 -> {
  96. var books = this.libary.books();
  97. books.forEach((b)->{
  98. this.viewer.showInfo(String.format("* %s", b));
  99. });
  100. }
  101. default -> {
  102. this.viewer.showInfo(String.format("不支持的操作:%d", opt));
  103. }
  104. }
  105. }
  106. case Author -> {
  107. AUTHOROPT:
  108. switch (opt) {
  109. case 1 -> {
  110. this.viewer.showInfo(this.author.name());
  111. var books = this.author.books();
  112. books.forEach((b)->{
  113. this.viewer.showInfo(String.format("* %s", b));
  114. });
  115. }
  116. case 2 -> {
  117. try {
  118. this.book = this.author.getBook(this.viewer.getBookName());
  119. this.status = Status.Book;
  120. } catch (BookNotFound bookNotFound) {
  121. this.viewer.showInfo(String.format("书本未找到:%s", bookNotFound.bookName()));
  122. }
  123. }
  124. case 3 -> {
  125. var bookName = this.viewer.getBookName();
  126. var bookType = this.viewer.getBookType();
  127. Book book;
  128. switch (bookType) {
  129. case "SCI" -> {
  130. var sciNumber = this.viewer.getSCINumber();
  131. book = new SCIBook(this.author, bookName, sciNumber);
  132. }
  133. case "SCORE" -> {
  134. int score;
  135. try {
  136. score = this.viewer.getScore();
  137. } catch (NumberFormatException e) {
  138. this.viewer.showInfo(String.format("评分分数解析错误:%s", e.getMessage()));
  139. break AUTHOROPT;
  140. }
  141. book = new ScoreBook(this.author, bookName, score);
  142. }
  143. default -> {
  144. this.viewer.showInfo(String.format("书本类型不支持:%s", bookType));
  145. break AUTHOROPT;
  146. }
  147. }
  148. this.author.addBook(book);
  149. }
  150. default -> {
  151. this.viewer.showInfo(String.format("不支持的操作:%d", opt));
  152. }
  153. }
  154. }
  155. case Book -> {
  156. BOOKOPT:
  157. switch (opt) {
  158. case 1 -> {
  159. this.viewer.showInfo(this.book.identity());
  160. }
  161. case 2 -> {
  162. this.author = this.book.author();
  163. this.status = Status.Author;
  164. }
  165. default -> {
  166. this.viewer.showInfo(String.format("不支持的操作:%d", opt));
  167. }
  168. }
  169. }
  170. }
  171. } catch (Exit ignored) {
  172. return;
  173. } catch (Cancel ignored) {
  174. this.viewer.showInfo("已取消执行");
  175. } catch (Back ignored) {
  176. this.status = Status.Library;
  177. }
  178. }
  179. }
  180. private String menu() {
  181. return switch (this.status) {
  182. case Library -> """
  183. 1) 查找图书
  184. 2) 查找作者
  185. 3) 新增图书
  186. 4) 新增作者
  187. 5) 查看图书馆作者列表
  188. 6) 查看图书馆图书列表""";
  189. case Book -> """
  190. 1) 查看图书信息
  191. 2) 查找作者""";
  192. case Author -> """
  193. 1) 查看作者信息
  194. 2) 查找图书
  195. 3) 新增图书""";
  196. };
  197. }
  198. private String header() {
  199. return switch (this.status) {
  200. case Library -> "图书馆模式";
  201. case Book -> String.format("图书:%s", this.book.identity());
  202. case Author -> String.format("作者:%s", this.book.identity());
  203. };
  204. }
  205. private String footer() {
  206. return """
  207. 输入 "q" 退出,输入 "n" 返回图书馆模式。""";
  208. }
  209. public static void main(String[] args) {
  210. System.out.println("""
  211. 欢迎使用 顶级前端 Java!!! 的图书馆管理系统。
  212. 本系统基于内存管理图书,不做任何持久化处理。
  213. 谢谢!
  214. """);
  215. Controller.createWithInit().mainloop();
  216. }
  217. }