图书馆管理系统——控制台打印版本

一:模式介绍:

1.采用MVC的模式

 

 补充:1.视图层是用来和用户打交道的,处理用户的输入,将一些从控制台得来的数据返回出去,以及提示用户的操作

           2.数据操作类:用来操作数据的增删改查,

           3.逻辑调度层:通过将调度视图层和数据层的数据关系,将整个系统串联起来,实现功能

二:代码

1.实体类:

1.1)就是表示图书属性的类

 1 import java.util.Objects;
 2 
 3 public class Book {
 4     private String name;
 5     private Double price;
 6     private Integer year;
 7     private Integer month;
 8 
 9     public Book() {};
10 
11     public String getName() {
12         return name;
13     }
14 
15     public void setName(String name) {
16         this.name = name;
17     }
18 
19     public Double getPrice() {
20         return price;
21     }
22 
23     public void setPrice(Double price) {
24         this.price = price;
25     }
26 
27     public Integer getYear() {
28         return year;
29     }
30 
31     public void setYear(Integer year) {
32         this.year = year;
33     }
34 
35     public Integer getMonth() {
36         return month;
37     }
38 
39     public void setMonth(Integer month) {
40         this.month = month;
41     }
42 
43     @Override
44     public String toString() {
45         return "Book{" +
46                 "name='" + name + '\'' +
47                 ", price=" + price +
48                 ", year=" + year +
49                 ", month=" + month +
50                 '}';
51     }
52 
53     @Override
54     public boolean equals(Object o) {
55         if (this == o) return true;
56         if (o == null || getClass() != o.getClass()) return false;
57         Book book = (Book) o;
58         return Objects.equals(name, book.name) &&
59                 Objects.equals(year, book.year) &&
60                 Objects.equals(month, book.month);
61     }
62 
63     @Override
64     public int hashCode() {
65         return Objects.hash(name, year, month);
66     }
67 }

1.2)图书管理系统有一个登录的界面,这里另外写一个登录的类

 1 import java.util.Objects;
 2 import java.util.Scanner;
 3 
 4 /**
 5  * 这是一个登录类
 6  */
 7 public class Login{
 8     //定义默认的密码和登录名
 9     private  String loginName = "白杨树";
10     private String password = "winterFlower";
11 
12     public Login() {
13     }
14 
15     public String getLoginName() {
16         return loginName;
17     }
18 
19     public void setLoginName(String loginName) {
20         this.loginName = loginName;
21     }
22 
23     public String getPassword() {
24         return password;
25     }
26 
27     public void setPassword(String password) {
28         this.password = password;
29     }
30 
31     @Override
32     public boolean equals(Object o) {
33         if (this == o) return true;
34         if (o == null || getClass() != o.getClass()) return false;
35         Login login = (Login) o;
36         return Objects.equals(loginName, login.loginName) &&
37                 Objects.equals(password, login.password);
38     }
39 
40     @Override
41     public int hashCode() {
42         return Objects.hash(loginName, password);
43     }
44 
45     public boolean register(Login login){
46             if(this.loginName.equals(login.getLoginName()) && this.password.equals(login.getPassword())){
47                 System.out.println("登录成功");
48             }else{
49                 System.out.println("你输入的登录名或者密码错误,请重新输入");
50                 return register(login);
51             }
52         return true;
53     }
54 }

 

2.视图层:

  1 import java.util.ArrayList;
  2 import java.util.Iterator;
  3 import java.util.Scanner;
  4 @SuppressWarnings("all")
  5 public class LibraryView {
  6     private static Scanner input = new Scanner(System.in);
  7 
  8     /**
  9      * 这是一个欢迎登录的界面
 10      * @return
 11      */
 12     public Login loginMenu(){
 13         System.out.println("欢迎登陆");
 14         System.out.println("请输入登录名和密码");
 15         String loginName = input.nextLine();
 16         String password = input.nextLine();
 17         //创建登录对象
 18         Login login = new Login();
 19         login.setLoginName(loginName);
 20         login.setPassword(password);
 21         return login;
 22     }
 23 
 24     /**
 25      * 这是一个功能选择视图
 26      * 1.添加图书
 27      * 2.修改图书信息
 28      * 3.删除图书
 29      * 4.根据图书名称模糊查找
 30      * 5.查看所有图书
 31      * 0.退出系统
 32      * @return
 33      */
 34     public int chooseFunction(){
 35         System.out.println("请根据提示选择你的操作功能");
 36         System.out.println("1-添加图书,2-修改图书信息,3-删除图书,4-根据图书名称模糊查找,5-查看所有图书,0-退出系统");
 37         String num = input.nextLine();
 38         int number = -1;
 39         try{
 40             number = Integer.parseInt(num);
 41         }catch (NumberFormatException e){
 42             System.out.println("请你重写输入一个数字");
 43             return chooseFunction();
 44         }
 45         return number;
 46     }
 47 
 48 
 49     /**
 50      * 这是一个添加图书的视图
 51      * @return
 52      */
 53     public Book addView(){
 54         System.out.println("请输入图示的名字和价格");
 55         String bookName = input.nextLine();
 56         String price = input.nextLine();
 57         double money = -1;
 58         try{
 59             money = Double.parseDouble(price);
 60         }catch (NumberFormatException e){
 61             System.out.println("请你输入一个数字");
 62             return addView();
 63         }
 64         //创建一个图书对象
 65         Book book = new Book();
 66         book.setName(bookName);
 67         book.setPrice(money);
 68         return book;
 69     }
 70 
 71     public Book updateView(){
 72         System.out.println("请输入新的图书名字和价格");
 73         String bookName = input.nextLine();
 74         String price = input.nextLine();
 75         double money = -1;
 76         try{
 77             money = Double.parseDouble(price);
 78         }catch (NumberFormatException e){
 79             System.out.println("请你输入一个数字");
 80            return updateView();
 81         }
 82         //创建图书对象
 83         Book book = new Book();
 84         book.setName(bookName);
 85         book.setPrice(money);
 86         return book;
 87     }
 88 
 89     /**
 90      * 这是提示用户输入他要修改图书的名字
 91      * @return
 92      */
 93     public String updateByName(){
 94         System.out.println("请输入你要修改的图书名字");
 95         String bookName = input.nextLine();
 96         return bookName;
 97     }
 98 
 99     /**
100      * 获取要修改图书的出版年份
101      * @return
102      */
103     public int updateByYear(){
104         System.out.println("请输入你要修改图书的出版年份");
105         String time = input.nextLine();
106         int year = -1;
107         try{
108             year = Integer.parseInt(time);
109         }catch (NumberFormatException e){
110             System.out.println("请输入一个年份");
111             return updateByYear();
112         }
113         if(year<1000 || year>9999){
114             System.out.println("请输入一个四位数的年份");
115             return updateByYear();
116         }
117         return year;
118     }
119 
120     /**
121      * 获取要修改图书的出版月份
122      * @return
123      */
124     public int updateByMoth(){
125         System.out.println("请输入你要修改图书的出版月份");
126         String time1 = input.nextLine();
127         int month = -1;
128         try{
129             month = Integer.parseInt(time1);
130         }catch (NumberFormatException e){
131             System.out.println("请输入一个年份");
132             return updateByMoth();
133         }
134         if(month<1 || month>12){
135             System.out.println("请输入一个四位数的年份");
136             return updateByYear();
137         }
138         return month;
139     }
140 
141     /**
142      * 键盘输入我们要修改的图书,并将修改后的图书返回出去
143      * @param book
144      * @return
145      */
146     public Book newInformation(Book book) {
147         System.out.println("请输入新的图书名字");
148         String bookName = input.nextLine();
149         System.out.println("请输入新的图书价格,年份和月份");
150         String money = input.nextLine();
151         String year = input.nextLine();
152         String month = input.nextLine();
153         double price = -1;
154         int year1 = -1;
155         int month1 = -1;
156         try {
157             price = Double.parseDouble(money);
158             year1 = Integer.parseInt(year);
159             month1 = Integer.parseInt(month);
160         } catch (NumberFormatException e) {
161             System.out.println("请输入正确的价格或者年份和月份");
162             return newInformation(book);
163         }
164         if ((year1 < 1000 || year1 > 9999) && (month1 < 1 || month1 > 12)) {
165             System.out.println("请输入正确的年份和月份");
166             return newInformation(book);
167         }
168         book.setName(bookName);
169         book.setPrice(price);
170         book.setMonth(month1);
171         book.setYear(year1);
172         return book;
173     }
174     /**
175      * 这是展示所有图书的界面
176      */
177     public void show(ArrayList<Book>list){
178         if(!list.isEmpty()){
179             Iterator<Book> iterator = list.iterator();
180             while(iterator.hasNext()){
181                 Book text = iterator.next();
182                 System.out.println(text);
183             }
184         }
185         else{
186             System.out.println("null");
187         }
188     }
189 
190 }

3.数据操作层:

  1 import java.util.ArrayList;
  2 import java.util.Calendar;
  3 import java.util.Iterator;
  4 
  5 public class LibraryDao {
  6     //创建一个集合来存储图书
  7     private static ArrayList<Book> list = new ArrayList<>(10000);
  8 
  9     //新增图书
 10     public boolean add(Book book) {
 11         if (list.contains(book)) {
 12             return false;
 13         }
 14         //年份
 15         int year = getYear();
 16         //月份
 17         int month = getMonth();
 18         //设置年份和月份
 19         book.setYear(year);
 20         book.setMonth(month);
 21         list.add(book);
 22         return true;
 23     }
 24 
 25     /**
 26      * 由系统获取年份
 27      *
 28      * @return
 29      */
 30     public int getYear() {
 31         //获取日期对象
 32         Calendar cl = Calendar.getInstance();
 33         //调用获取年份的方法
 34         Integer year = Integer.valueOf(cl.get(Calendar.YEAR));
 35         return year;
 36     }
 37 
 38     /**
 39      * 由系统获取月份
 40      *
 41      * @return
 42      */
 43     public int getMonth() {
 44         //获取日期对象
 45         Calendar cl = Calendar.getInstance();
 46         //调用获取年份的方法
 47         Integer month = Integer.valueOf(cl.get(Calendar.MONTH));
 48         return month;
 49     }
 50 
 51     /**
 52      * 查找要修改的图书
 53      * @param bookName
 54      * @param year
 55      * @param month
 56      * @return
 57      */
 58     public Book findBook(String bookName, int year, int month) {
 59         //遍历集合,查看集合中的图书是否有我们要修改的
 60         if (!list.isEmpty()) {
 61             Iterator<Book> iterator = list.iterator();
 62             while (iterator.hasNext()) {
 63                 Book text = iterator.next();
 64                 if (text.getName().equals(bookName) && text.getYear() == year && text.getMonth() == month) {
 65                     return text;
 66                 }
 67             }
 68         }
 69         return null;
 70     }
 71 
 72     /**
 73      * 删除图书
 74      * @param book
 75      */
 76     public void delete(Book book){
 77         list.remove(book);
 78     }
 79 
 80 
 81     /**
 82      * 根据图书名称模糊查找
 83      * @param
 84      * @return
 85      */
 86     public Book findByName(String name){
 87         //遍历集合中的每一本书
 88         if(!list.isEmpty()){
 89             Iterator<Book> iterator = list.iterator();
 90             while (iterator.hasNext()) {
 91                 Book text = iterator.next();
 92                 if(text.getName().equals(name)){
 93                     return text;
 94                 }
 95             }
 96         }
 97         return null;
 98     }
 99 
100     /**
101      查看所有图书
102      * @return
103      */
104     public ArrayList<Book> printAll() {
105         return list;
106     }
107 }

4.逻辑调度层:一般是在Main方法中,但是为了让代码结构更加简洁,将逻辑调度另在一个类中写一个方法,这样在Main()方法中只用调用这个方法就可以了。

4.1)

 1 import java.util.ArrayList;
 2 
 3 public class Administrator {
 4     static LibraryView view = new LibraryView();
 5     static LibraryDao dao = new LibraryDao();
 6     //管理员操作的动作
 7     public static void operateByAdministrator(){
 8         //定义一个数来控制循环跳出去
 9         int number = -1;
10         while(number!=0){
11             int num = view.chooseFunction();
12             switch (num){
13                 case 1:
14                     Book book = view.addView();
15                     dao.add(book);
16                     break;
17                 case 2:
18                     //修改图书
19                     //1.获取要修改的图书的信息
20                     String name = view.updateByName();
21                     int year = view.updateByYear();
22                     int month = view.updateByMoth();
23                     //2.获取要修改的图书
24                     Book book1 = dao.findBook(name, year, month);
25                     if(book1!=null) {
26                         Book book4 = view.newInformation(book1);
27                         //将修改的图书打印出来
28                         System.out.println(book4.toString());
29                     }else {
30                         System.out.println("你查找的图书并不存在");
31                     }
32                     break;
33                 case 3:
34                     //删除
35                     //1.获取要删除的图书的信息
36                     String name1 = view.updateByName();
37                     int year1 = view.updateByYear();
38                     int month1 = view.updateByMoth();
39                     //2.获取要修改的图书
40                     Book book2 = dao.findBook(name1, year1, month1);
41                     if(book2!=null) {
42                         dao.delete(book2);
43                     }else {
44                         System.out.println("你要删除的图书并不存在的图书并不存在");
45                     }
46                     break;
47                 case 4:
48                     //根据图书名字模糊查询图书
49                     String name3 = view.updateByName();
50                     Book book3 = dao.findByName(name3);
51                     if(book3!=null) {
52                         //将找到的图书打印出来
53                         System.out.println(book3.toString());
54                     }else{
55                         System.out.println("你要查找的图书并不存在");
56                     }
57                     break;
58                 case 5:
59                     //展示所有图书
60                     ArrayList<Book> books = dao.printAll();
61                     view.show(books);
62                     break;
63                 case 0:
64                     num = 0;
65                     return;
66             }
67         }
68     }
69 }

4.2)

 1 public class Test {
 2     public static void main(String[] args) {
 3         //创建视图对象
 4         LibraryView view = new LibraryView();
 5         //创建登录对象
 6         Login login = new Login();
 7         //登录视图,返回一个登录对象
 8         Login ll = view.loginMenu();//ll为返回的登录对象
 9         boolean flag = login.register(ll);
10         if(flag){
11             /**
12              * 如果登录成功,接下来就由管理自己操作,所以此处重新创建一个管理员类
13              */
14             //创建管理员类对象
15             Administrator.operateByAdministrator();
16 
17         }
18 
19     }
20 }

三:结语:

1.坚持就是胜利,让我们一起好好努力吧,绝对是不可以放弃的,宝贝加油!

2.引用老师的一句话:“早起的鸟儿有虫吃,乱写的代码有bug”。(深有体会,哈哈哈)

 

posted @ 2021-06-17 16:22  白杨树&  阅读(119)  评论(0编辑  收藏  举报