【JAVA】经典图书管理系统的实现

这算是对JAVA板块的补充,一个小型练手程序,作为新手程序员必须要经历的一个过程,下面会附上源代码,以及运行结果。

目录结构如图所示

1.Book

java 复制代码
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;
    }

    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;
    }

    @Override
    public String toString() {
        return "BookList{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ( (isBorrowed == true) ? " 已借出" : " 未借出") +
                '}';
    }
}

2.BookList

java 复制代码
package book;

public class BookList {
    private Book[] books = new Book[10];
    private int usedSize;

    public BookList() {
        this.books[0] = new Book("三国演义","罗贯中",10,"小说");
        this.books[1] = new Book("西游记","吴承恩",59,"小说");
        this.books[2] = new Book("红楼梦","曹雪芹",16,"小说");

        this.usedSize = 3;
    }

    public int getUsedSize() {
        return usedSize;
    }
    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
    public Book getBook(int pos) {
        return books[pos];
    }

    public void setBook(int pos,Book book) {
        this.books[pos] = book;
    }
    public Book[] getBooks() {
        return books;
    }
}

3.AddOperation

java 复制代码
package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements  IOPeration{

    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书.....");

        //1. 判满
        int currentSize = bookList.getUsedSize();
        if(currentSize == bookList.getBooks().length) {
            System.out.println("书架满了 不能放了.....");
            return;
        }

        //2. 构建对象
        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 newBook = new Book(name,author,price,type);

        //3. 判断书架有没有这本书
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println("有这本书不能插入!");
                return;
            }
        }

        //4.插入这本书
        bookList.setBook(currentSize,newBook);

        bookList.setUsedSize(currentSize+1);

        System.out.println("新增图书成功!!!");
    }
}

4.BrrowOperation

java 复制代码
package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书");


        Scanner scanner = new Scanner(System.in);
        System.out.printf("请输入你借阅的书名:");
        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)) {
                if(book.isBorrowed()) {
                    System.out.println("这本书已经被借出了!");
                    return;
                }
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
        }
        System.out.println("没有你要借阅的这本书....");
    }
}

5.DelOperation

java 复制代码
package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书.....");

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你删除的书名:");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();

        int pos = -1;
        //找到这本书
        int i = 0;
        for (; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                pos = i;
                break;
            }
        }

        if(i == currentSize) {
            System.out.println("没有你要删除的书!");
            return ;
        }

        //这里开始删除
        for (int j = pos; j < currentSize-1; j++) {
            //bookList[j] = bookList[j+1];
            Book book = bookList.getBook(j+1);

            bookList.setBook(j,book);
        }

        bookList.setBook(currentSize-1,null);

        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功!!!!");

    }
}

6.ExitPeration

java 复制代码
package ioperation;

import book.BookList;

public class ExitOperation implements  IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统....");
        System.exit(0);
    }
}

7.FindPeration

java 复制代码
package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOPeration{
    public void work(BookList bookList) {
        System.out.println("查找图书.....");
        Scanner scanner = new Scanner(System.in);
        System.out.printf("请输入你的书名:");
        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("没有你要找的这本书....");
    }
}

8.IOPeration

java 复制代码
package ioperation;

import book.BookList;

public interface IOPeration {
    void work(BookList bookList);

}

9.ReturnOperation

java 复制代码
package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书....");
        Scanner scanner = new Scanner(System.in);
        System.out.printf("请输入你归还的书名:");
        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)) {
                if(book.isBorrowed()) {
                    book.setBorrowed(false);
                    System.out.println("归还成功!");
                    return;
                }
            }
        }
        System.out.println("没有你要归还的图书!!!");
    }
}

10.ShowOperation

java 复制代码
package ioperation;

import book.Book;
import book.BookList;

public class ShowOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("显示图书....");
        int currentSize = bookList.getUsedSize();//3
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
            //Book book1 = bookList[i];
        }
    }
}

11.AdminUser

java 复制代码
package user;

import ioperation.*;

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("欢迎"+this.name+"来到图书系统");
        System.out.println("*******管理员菜单*******");
        System.out.println("1. 查找图书");
        System.out.println("2. 新增图书");
        System.out.println("3. 删除图书");
        System.out.println("4. 显示图书");
        System.out.println("0. 退出系统");
        System.out.println("**********************");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的操作:");
        int choice = scanner.nextInt();
        return choice;
    }
}

12.NormalUser

java 复制代码
package user;

import ioperation.*;

import java.util.Scanner;

public class NormalUser extends User {

    public NormalUser(String name) {
        super(name);
        this.ioPerations = new IOPeration[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()
        };
    }

    public int menu() {
        System.out.println("欢迎"+this.name+"来到图书系统");
        System.out.println("*******普通用户菜单*******");
        System.out.println("1. 查找图书");
        System.out.println("2. 借阅图书");
        System.out.println("3. 归还图书");
        System.out.println("0. 退出系统");
        System.out.println("**********************");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的操作:");
        int choice = scanner.nextInt();
        return choice;
    }
}

13.User

java 复制代码
package user;

import book.BookList;
import ioperation.IOPeration;

public abstract class User {

    protected String name;
    public IOPeration[] ioPerations;

    public User(String name) {
        this.name = name;
    }

    public abstract int menu();

    public void doIoperation(int choice, BookList bookList) {
        ioPerations[choice].work(bookList);
    }

}

14.Main

java 复制代码
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

public class Main {
    public static User login() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的姓名:");
        String name = scanner.nextLine();

        System.out.println("请输入你的身份,1:管理员   2:普通用户");

        int choice = scanner.nextInt();//1   2

        //问题:如果是1 管理员对象,如果是2 普通用户对象
        if(choice == 1) {
            return new AdminUser(name);
        }else {
            return new NormalUser(name);
        }
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();

        User user = login();

        while (true) {
            int choice = user.menu();
            //要根据这个返回值 来看 调用哪个对象的 哪个方法

            user.doIoperation(choice, bookList);

        }
    }

}
相关推荐
昊坤说不出的梦3 小时前
【实战】监控上下文切换及其优化方案
java·后端
今天_也很困4 小时前
LeetCode热题100-560. 和为 K 的子数组
java·算法·leetcode
在繁华处4 小时前
线程进阶: 无人机自动防空平台开发教程V2
java·无人机
A懿轩A4 小时前
【Java 基础编程】Java 变量与八大基本数据类型详解:从声明到类型转换,零基础也能看懂
java·开发语言·python
m0_740043734 小时前
【无标题】
java·spring boot·spring·spring cloud·微服务
@ chen5 小时前
Spring事务 核心知识
java·后端·spring
aithinker5 小时前
使用QQ邮箱收发邮件遇到的坑 有些WIFI不支持ipv6
java
星火开发设计5 小时前
C++ 预处理指令:#include、#define 与条件编译
java·开发语言·c++·学习·算法·知识
Hx_Ma166 小时前
SpringMVC返回值
java·开发语言·servlet