目录
🌈今天我们来做一个小东西,目标就是使用学过的知识来实现——《图书管理系统》小练习
📝图书馆作为每个大学或者城市必不可少的部分,其存放的海量书籍与文献使得其管理显得尤为困难,此时图书管理系统就起到了一个重大的作用。本次就将使用java语言中的异常处理,多态,继承,封装,接口等知识点,来综合性的设计与实现出一个简易的图书管理系统。
➡️首先我们建立一个 book 的包,在这里我们继续创建一个 book 的类,根据我们的设计思路,我们这里书籍的属性有书名、作者、价格、类型以及是否被借出,我们结合封装性,将属性设置为 private 权限,并提供相应的get方法和set方法,空参构造器,以及一个不包含是否被借出的构造器(布尔值的默认类型就是false,可以不进行初始化)并需要提供对应的toString()方法的重写,代码实现如下:
- package book;
-
- public class Book {
- private String name;//书名
- private String author;//作者
- private int price;//价格
- private String type;//类型
- private boolean isBorrowed;//是否被借出
-
- //构造方法
- public Book(String name, String author, int price, String type) {
- this.name = name;
- this.author = author;
- this.price = price;
- this.type = type;
- }
-
- //get、set方法
- 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 isBorrowed() {
- return isBorrowed;
- }
-
- public void setBorrowed(boolean borrowed) {
- isBorrowed = borrowed;
- }
-
- //toString()方法
- @Override
- public String toString() {
- return "Book{" +
- "name='" + name + '\'' +
- ", author='" + author + '\'' +
- ", price=" + price +
- ", type='" + type + '\'' +
- (isBorrowed == true ? " 已经被借出" : " 未被借出")+
- '}';
- }
- }
✨作为书架,那么可以放若干本书,那么如何放置若干本书呢?
🙉首先我们提供一个泛型数组,用usedSize来记录当前books数组中有多少本书,并且给出具体的书的参数,供对应的get和set方法
- public class BookList {
- private static final int DEFAULT_SIZE = 10;
- private Book[] books = new Book[DEFAULT_SIZE];
-
- private int usedSize;//记录当前books数组中有多少本书
-
- public BookList() {
- books[0] = new Book("三国演义","罗贯中",89,"小说");
- books[1] = new Book("西游记","吴承恩",78,"小说");
- books[2] = new Book("红楼梦","曹雪芹",49,"小说");
- this.usedSize = 3;
- }
-
- public Book getBook(int pos) {
- return this.books[pos];
- }
-
- public void setBook(Book book) {
- this.books[usedSize] = book;
- }
-
- public void setBook(int pos,Book book) {
- this.books[pos] = book;
- }
-
- public void setBooks(int pos,Book book) {
- books[pos] = book;
- }
-
- public int getUsedSize() {
- return usedSize;
- }
-
- public void setUsedSize(int usedSize) {
- this.usedSize = usedSize;
- }
- }
➡️创建一个user包,这个包有两个用户类型,一个是管理员,一个是普通用户,并且它们的功能各不相同,但是都与要输入用户姓名,因此可以用一个User类当作一个父类
✨首先定义一个用户的名字,同时要提供set和get方法,get方法,并提供抽象Operation方法给子类重写使用(用户不同,操作流程不同),同时定义一个抽象方法menu():不同的对象菜单不同
- public abstract class User {
- protected String name;
- protected IOperation[] iOperations;
-
- public User(String name) {
- this.name = name;
- }
-
- public abstract int menu();//不同对象的菜单不一样
-
- public void doWork(int choice, BookList bookList) {
- this.iOperations[choice].work(bookList);//this.iOperations[choice]-------对象
- }
- }
✨管理管的操作需要继承用户类,并且实现构造方法,同时实现抽象方法Operation(),包括:退出,查找、新增、删除、显示图书的功能,最后输入具体的操作:
- import opera.*;
-
- import java.util.Scanner;
-
- public class AdminUser extends User{
-
- //子类继承父类的构造方法
- public AdminUser(String name) {
- super(name);
- this.iOperations = new IOperation[]{
- new ExitOperation(),
- new FindOperation(),
- new AddOperation(),
- new DelOperation(),
- new ShowOperation()
- };
- }
-
- //菜单
- public int menu() {
- System.out.println("*********************************");
- System.out.println("hello " + name + " 欢迎来到图书管理系统");
- System.out.println("1.查找图书!");
- System.out.println("2.新增图书!");
- System.out.println("3.删除图书!");
- System.out.println("4.显示图书!");
- System.out.println("0.退出系统!");
- System.out.println("*********************************");
- System.out.println("请输入你的操作:");
- Scanner scanner = new Scanner(System.in);
- int choice = scanner.nextInt();
- return choice;
- }
- }
✨普通用户的操作需要继承用户类,并且实现构造方法,同时实现抽象方法Operation(),包括:退出,查找、借出、归还图书的功能,最后输入具体的操作:
- package user;
-
- import opera.*;
-
- import java.util.Scanner;
-
- /**
- * Created with IntelliJ IDEA.
- * Description:
- * User: Lenovo
- * Date: 2022-11-17
- * Time: 15:53
- */
- public class NormalUser extends User{
- //子类继承父类的构造方法
- public NormalUser(String name) {
-
- super(name);
- this.iOperations = new IOperation[] {
- new ExitOperation(),
- new FindOperation(),
- new BrrowOperation(),
- new ReturnOperation()
- };//拿到当前对象引用,并初始化
- }
-
- @Override
- public int menu() {
- System.out.println("*********************************");
- System.out.println("hello " + name + " 欢迎来到图书管理系统");
- System.out.println("1.查找图书!");
- System.out.println("2.借阅图书!");
- System.out.println("3.归还图书!");
- System.out.println("0.退出系统!");
- System.out.println("*********************************");
- System.out.println("请输入你的操作:");
- Scanner scanner = new Scanner(System.in);
- int choice = scanner.nextInt();
- return choice;
- }
- }
👉建立一个操作包,用来实现具操作的功能,例如添加图书,删除图书等。
- package opera;
-
- import book.BookList;
-
- public interface IOperation {
- void work(BookList bookList);
- }
✨首先我们需要新增一本书,具体的要求之前已经给出,在这里难点就是:我们增加的图书应该放在最后一本书的后边,并且如果这本书存在的话就不需要再次放入。最互放入书籍之后需要将整个书籍数+1
- package opera;
-
- import book.Book;
- import book.BookList;
-
- import java.util.Scanner;
-
- public class AddOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("新增图书!");
-
- Scanner scanner = new Scanner(System.in);
- System.out.println("请输入书名:");
- String name = scanner.nextLine();
- System.out.println("请输入作者:");
- String author = scanner.nextLine();
- System.out.println("请输入类型:");
- String type = scanner.nextLine();
- System.out.println("请输入价格:");
- int price = scanner.nextInt();
-
- Book book = new Book(name,author,price,type);
- //书籍默认放到数组最后一本书的后面
- int currentSize = bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- Book tmp = bookList.getBook(i);
- if(tmp.getName().equals(name)) {
- System.out.println("已经存在这本书,不能再存储了!");
- return;
- }
- }
- bookList.setBook(book);
- //修改usedSize
- bookList.setUsedSize(currentSize+1);
-
- }
- }
✨遍历数组,有两种情况:找到了或者没找到,逻辑思路比较清晰
- package opera;
-
- import book.Book;
- import book.BookList;
-
- import java.util.Scanner;
-
- public class FindOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("查找图书!");
- System.out.println("请输入书名");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- int currentSize = bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- Book book = bookList.getBook(i);
- if(book.getName().equals(name)) {
- System.out.println("找到了这本书:");
- System.out.println(book);
- return;
- }
- }
- System.out.println("没有这本书!");
- }
- }
✨同样需要遍历数组,找到删除的书籍,在这里一个难点就是:当把书籍的下边删除之后需要将后边的下标依次向前挪动,并在这里挪动的时候不可以发生数组越界。最后书籍数量-1,并且需要将最后一个数组置为null即为没有
- package opera;
-
- import book.Book;
- import book.BookList;
-
- import java.util.Scanner;
-
- public class DelOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("删除图书!");
- System.out.println("请输入你要删除的图书的名字");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- int currentSize = bookList.getUsedSize();
- int index = -1;
- for (int i = 0; i < currentSize; i++) {
- Book tmp = bookList.getBook(i);
- if(tmp.getName().equals(name)) {
- index = 1;
- break;
- }
- }
- //挪动数据
- for (int j = index; j < currentSize-1; j++) {
- Book book = bookList.getBook(j+1);
- bookList.setBook(j,book);
- }
- //修改size
- bookList.setUsedSize(currentSize-1);
- //因为删除的是对象 所以把最后一个置为null
- bookList.setBook(currentSize-1,null);
-
- System.out.println("删除成功!");
- }
- }
✨显示图书比较简单,只需要遍历数组即可
- package opera;
-
- import book.Book;
- import book.BookList;
-
- public class ShowOperation implements IOperation{
-
- @Override
- public void work(BookList bookList) {
- System.out.println("显示图书");
- int currentSize = bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- Book book = bookList.getBook(i);
- System.out.println(book);
- }
- }
- }
✨遍历数组,判断其是否已被借出
- package opera;
-
- import book.Book;
- import book.BookList;
-
- import java.util.Scanner;
-
- public class BrrowOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("借阅图书!");
- System.out.println("输入你要借阅的图书:");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- int currentSize = bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- Book book = bookList.getBook(i);
- if (book.getName().equals(name) &&
- !book.isBorrowed()) {
- book.setBorrowed(true);
- System.out.println("借阅成功");
- return;
- }
- }
- }
- }
- package opera;
-
- import book.Book;
- import book.BookList;
-
- import java.util.Scanner;
-
- public class ReturnOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("归还图书!");
- System.out.println("输入你要归还的图书:");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- int currentSize = bookList.getUsedSize();
- for (int i = 0; i < currentSize; i++) {
- Book book = bookList.getBook(i);
- if(book.getName().equals(name) &&
- book.isBorrowed()) {
- book.setBorrowed(false);
- System.out.println("归还成功");
- return;
- }
- }
- }
- }
- package opera;
-
- import book.BookList;
-
- public class ExitOperation implements IOperation{
- @Override
- public void work(BookList bookList) {
- System.out.println("退出系统!");
- System.exit(0);
- }
- }
Main类主要是把以上所有功能串起来,并且实现整个图书管理系统
- import book.BookList;
- import user.AdminUser;
- import user.NormalUser;
- import user.User;
-
- import java.util.Scanner;
-
- public class Main {
- public static User logic() {
- System.out.println("请输入你的姓名:");
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- System.out.println("请输入你的身份:1->管理员 0->普通用户");
- //输入身份会实例化一个对象
- int choice = scanner.nextInt();
- if(choice == 1) {
- return new AdminUser(name);
- }else {
- return new NormalUser(name);
- }
- }
-
- public static void main(String[] args) {
- BookList bookList = new BookList();
- User user = logic();
- while(true) {
- int choice = user.menu();//动态绑定 1️⃣向上转型;2️⃣重写;3️⃣通过父类引用,调用这个父类和子类重写的方法
- //根据 choice 和 user 来确定我到底调用哪个对象的哪个操作?
- user.doWork(choice,bookList);
- }
-
- }
-
- }