> 文档中心 > 《Java-第十一章》之图书管理系统

《Java-第十一章》之图书管理系统

前言

学习完java面向对象语法后,做一个图书系统来熟悉语法。

博客主页:KC老衲爱尼姑的博客主页

博主的github,平常所写代码皆在于此

共勉:talk is cheap, show me the code

作者是爪哇岛的新手,水平很有限,如果发现错误,一定要及时告知作者哦!感谢感谢!


文章目录

  • 🚀1.需求分析
    • 🚀2.具体实现
      • 🚀3.项目文件划分
      • 🚀4.book类
      • 🚀5.BookList类
      • 🚀6.IOperations接口
        • 🚀1.新增图书功能
        • 🚀2.借阅图书功能
        • 🚀3.删除图书功能
        • 🚀4.退出系统功能
        • 🚀5.归还图书功能
        • 🚀6.查找图书功能
        • 🚀7.显示图书功能
      • 🚀8.User
        • 🚀8.1RootUser类
        • 🚀8.2NormalUser类
      • 🚀9.系统启动类

🚀1.需求分析

在这里插入图片描述

通过上图可知图书管理系统的使用者分为图书管理员以及普通用户,并且管理员与普通用户对图书系统的使用权限有所差异。但是无论是图书管理员还是普通用户都是对书籍进行操作,为此需要一个书架来存储书籍,然后使书架具备CURD功能即可,最后可以给使用者使用了。

🚀2.具体实现

🚀3.项目文件划分

创建项目后,在scr下分别创建book包,booklist,operation以及user包对所需的类进行管理,book包下为书籍类,booklist下为书架,operation下为书架的具体功能实现,user下用户所在处。

🚀4.book类

给予书籍类一些基本属性如书名,作者,价格,类型,是否被借阅等属性。

package book;/** * truth:talk is cheap, show me the code * * @author KC萧寒 * @description * @createDate: 2022-05-19 12:57 */public class Book {    private String name;    private String author;    private int price;    private String type;    private boolean borrow;    public Book(String name, String author, int price, String type) { this.name = name; this.author = author; this.price = price; this.type = type;    }    public String getName() { return name;    }    public void setName(String name) { this.name = name;    }    public String getAuthor() { return author;    }    public void setAuthor(String author) { this.author = author;    }    public int getPrice() { return price;    }    public void setPrice(int price) { this.price = price;    }    public String getType() { return type;    }    public void setType(String type) { this.type = type;    }    public boolean isBorrow() { return borrow;    }    public void setBorrow(boolean borrow) { this.borrow = borrow;    }    @Override    public String toString() { return "Book{" +  "name='" + name + '\'' +  ", author='" + author + '\'' +  ", price=" + price +  ", type='" + type + '\'' +  ", borrow=" + borrow +  '}';    }}

🚀5.BookList类

所谓的书架就是存放书籍的容器,再此使用数组来存储书籍,默认书架上四大名著且只容纳10本书。我们使用userSized变量记录书架上的已有的书籍

package booklist;import book.Book;/** * truth:talk is cheap, show me the code * * @author KC萧寒 * @description * @createDate: 2022-05-19 13:00 */public class BookList {    private Book[] books=new Book[10];    private int userSized;    public BookList() {books[0]=new Book("红楼梦","曹雪芹",90,"小说"); books[1]=new Book("三国演义","罗贯中",80,"小说"); books[2]=new Book("西游记","吴承恩",70,"小说"); books[3]=new Book("水浒传","施耐庵",60,"小说"); this.userSized = 4;    }    public Book getBook(int pos) {//通过pos在书架上获得图书 if(pos<0||pos>books.length){     return null; } return books[pos];    }    public void setBooks(int pos,Book book) {//根据pos书架上设置图书 if(pos<0||pos>books.length){     return ; } books[pos]=book;    }    public int getUserSized() {//获得书架已使用的空间 return userSized;    }    public void setUserSized(int userSized) {//重置书架已使用的空间 this.userSized = userSized;    }}

🚀6.IOperations接口

该接口是对图书操作的一个规范,

package operation;import booklist.BookList;public interface IOperations {    void work(BookList list);}

🚀1.新增图书功能

package operation;import book.Book;import booklist.BookList;import java.util.Scanner;/** * truth:talk is cheap, show me the code * * @author KC萧寒 * @description * @createDate: 2022-05-19 13:13 */public class AddBook implements IOperations {    @Override    public void work(BookList list) { Scanner sc = new Scanner(System.in); System.out.println("请输入你要新增的书名:"); String name = sc.next(); System.out.println("请输入你要新增图书的作者:"); String author = sc.next(); System.out.println("请输入书的价格"); int price = sc.nextInt(); System.out.println("请输入你要新增图书的类型:"); String type = sc.next(); Book book = new Book(name, author, price, type); int currentSize = list.getUserSized(); list.setBooks(currentSize, book); list.setUserSized(currentSize + 1); System.out.println("添加成功");    }}

🚀2.借阅图书功能

属性borrow默认为false即未接出,只需将其置为true就表示书籍已接出。

package operation;import book.Book;import booklist.BookList;import java.util.Scanner;/** * truth:talk is cheap, show me the code * * @author KC萧寒 * @description * @createDate: 2022-05-19 20:20 */public class BorrowBook implements IOperations {    @Override    public void work(BookList list) { Scanner sc = new Scanner(System.in); System.out.println("请输入你要借的书名:"); String name = sc.next(); for(int i=0;i<list.getUserSized();i++){     Book book=list.getBook(i);     if(name.equals(book.getName())){  book.setBorrow(true);  System.out.println("已成功借书");  return ;     } }    }}

🚀3.删除图书功能

储存书籍的容器为数组,我们可以先根据书名在书架中查找书籍书否存在,若存在则在数组中将后面的书籍覆盖要删除的书籍即可。

package operation;import book.Book;import booklist.BookList;import java.util.Scanner;/** * truth:talk is cheap, show me the code * * @author KC萧寒 * @description * @createDate: 2022-05-19 13:12 */public class DeleteBook implements IOperations {    @Override    public void work(BookList list) { Scanner sc = new Scanner(System.in); System.out.println("删除图书!"); System.out.println("请输入你要删除的图书:"); String name = sc.next(); int i=0; int index=0; for ( i = 0; i < list.getUserSized(); i++) {     Book book = list.getBook(i);     if (name.equals(book.getName())) {  index = i;  break;     } } if (i>=list.getUserSized()) {     System.out.println("没有找到此书");     return ; } for (int j = index; j < list.getUserSized(); j++) {     Book book=list.getBook(j+1);     list.setBooks(j,book); } list.setBooks(list.getUserSized()-1,null);//将没有书籍的位置置null,避免内存泄露 list.setUserSized(list.getUserSized()-1); System.out.println(name+"删除了");    }}

🚀4.退出系统功能

使用System的exit()方法终止JVM的运行,就是结束退出系统。exit中的参数非0表示异常终止。

package operation;import booklist.BookList;/** * truth:talk is cheap, show me the code * * @author KC萧寒 * @description * @createDate: 2022-05-19 20:18 */public class ExitOperation implements IOperations {    @Override    public void work(BookList list) { System.exit(0); System.out.println("退出成功!");    }}

🚀5.归还图书功能

将该图书的属性borrow置为false即可。

package operation;import book.Book;import booklist.BookList;import java.util.Scanner;/** * truth:talk is cheap, show me the code * * @author KC萧寒 * @description * @createDate: 2022-05-19 20:12 */public class ReturnBook implements IOperations {    @Override    public void work(BookList list) { System.out.println("归还图书"); Scanner sc = new Scanner(System.in); System.out.println("情书输入你要归还图书的书名:"); String name = sc.next(); int currentSize= list.getUserSized(); for(int i=0;i<currentSize;i++){     Book book = list.getBook(i);     if(name.equals(book.getName())){  book.setBorrow(false);  System.out.println("归还成功");  return ;     } }    }}

🚀6.查找图书功能

根据书名在书籍查找图书即可。

package operation;import book.Book;import booklist.BookList;import java.util.Scanner;/** * truth:talk is cheap, show me the code * * @author KC萧寒 * @description * @createDate: 2022-05-19 13:12 */public class SearchBook implements IOperations {    @Override    public void work(BookList list) { Scanner sc = new Scanner(System.in); System.out.println("请输入你要查找的书名:"); String name=sc.next(); for(int i=0;i<list.getUserSized();i++){     Book book=list.getBook(i);     if(name.equals(book.getName())){  System.out.println("找到啦");  System.out.println(book);  return ;     } } System.out.println("很遗憾,没有找到!");    }}

🚀7.显示图书功能

因为图书类已经重写了toString()方法,只需遍历书架上的图书将 其打印出来就能得到图书的信息。

package operation;import booklist.BookList;/** * truth:talk is cheap, show me the code * * @author KC萧寒 * @description * @createDate: 2022-05-19 13:15 */public class ShowBook implements IOperations {    @Override    public void work(BookList list) { System.out.println("显示图书:"); for(int i=0;i<list.getUserSized();i++){     System.out.println(list.getBook(i)); }    }}

🚀8.User

图书管理员以及普通用户都是用户,故定义一个父类,在User中定义基本属性name,并提供构造器。由于图书管理员与普通用户的权限不同,也就是登录到系统时所遇到的菜单是不一致得到,所以定义一个抽象的menu()方法供子类具体实现。有因为图书管理员和普通用户的功能不一致,为此在父类中提供一个doOperation()方法,可以根据用户在菜单的选择去调用对应的方法。

package user;import booklist.BookList;import operation.IOperations;/** * truth:talk is cheap, show me the code * * @author KC萧寒 * @description * @createDate: 2022-05-19 13:15 */public abstract class User {   protected String name;    protected IOperations [] ip;    public abstract int menu();    public User(String name){ this.name=name;    } public void doOperation(int choice, BookList List){ this.ip[choice].work(List);    }}

🚀8.1RootUser类

package user;import operation.*;import java.util.Scanner;/** * truth:talk is cheap, show me the code * * @author KC萧寒 * @description * @createDate: 2022-05-19 12:57 */public class RootUser extends User {    public RootUser(String name) { super(name); this.ip = new IOperations[]{  new ExitOperation(),  new SearchBook(),  new AddBook(),  new DeleteBook(),  new ShowBook() };    }    @Override    public int menu() { System.out.println(this.name + "欢迎来到图书馆"); Scanner sc = new Scanner(System.in); System.out.println("1.查找图书:"); System.out.println("2.新增图书:"); System.out.println("3.删除图书:"); System.out.println("4.显示图书:"); System.out.println("0.退出图书:"); System.out.println("请输入你的选择:"); int choice = sc.nextInt(); return choice;    }}

🚀8.2NormalUser类

package user;import operation.*;import java.util.Scanner;/** * truth:talk is cheap, show me the code * * @author KC萧寒 * @description * @createDate: 2022-05-19 12:56 */public class NormalUser extends User{    public NormalUser(String name) { super(name);      this.ip=new IOperations[]{new ExitOperation(),new SearchBook(),new BorrowBook(),new ReturnBook(),      };    }    @Override    public int menu() { Scanner sc = new Scanner(System.in); System.out.println(this.name+"欢迎来到图书馆"); System.out.println("1.查找图书:"); System.out.println("2.借阅图书:"); System.out.println("3.归还图书:"); System.out.println("0.退出图书:"); System.out.println("请输入你的选择:"); int choice=sc.nextInt(); return choice;    }}

🚀9.系统启动类

提供login方法根据id确定使用者的身份,最后将用户的选择和书架传入doOperation()方法即可。

import booklist.BookList;import user.NormalUser;import user.RootUser;import user.User;import java.util.Scanner;/** * truth:talk is cheap, show me the code * * @author KC萧寒 * @description * @createDate: 2022-05-19 20:50 */public class Start {    public static void main(String[] args) { User use=login(); BookList list=new BookList(); while (true){     int chiose=use.menu();//利用多态对书架操作     use.doOperation(chiose,list); }    }    private static User login() {//根据id判断用户对象 Scanner sc = new Scanner(System.in); System.out.println("请输入你的姓名:"); String name = sc.next(); System.out.println("请输入你的身份:1-》管理员,0-》普通用户"); int id = sc.nextInt(); if (id == 1) {     return new RootUser(name); } else {     return new NormalUser(name); }    }}

最后的话

各位看官如果觉得文章写得不错,点赞评论关注走一波!谢谢啦!

在这里插入图片描述