一个简单的图书管理系统

Posted 哲川.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个简单的图书管理系统相关的知识,希望对你有一定的参考价值。


简单的图书管理系统

二、各个类的实现

1.book类

(1)Book

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 "Book" +
                "name='" + name + '\\'' +
                ", author='" + author + '\\'' +
                ", price=" + price +
                ", type='" + type + '\\'' +
                ((isBorrowed == true) ? " 已经被借出 " : " 未被借出 ") +
                '';
    

(2)BookList

package book;

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

    public BookList() 
        books[0] = new Book("三国演义","罗贯中",17,"小说");
        books[1] = new Book("西游记","吴承恩",47,"小说");
        books[2] = new Book("水浒传","施耐庵",37,"小说");
        this.usedSize = 3;
    

    public int getUsedSize() 
        return usedSize;
    

    public void setUsedSize(int usedSize) 
        this.usedSize = usedSize;
    

    //获取到pos位置的一本书
    public Book getPos(int pos) 
        return this.books[pos];
    

    //设置Pos下标为一本书->[添加一本书]
    public void setBook(int pos,Book book) 
        this.books[pos] = book;
    


2.operation类

(1)IOperation

因为Operation包中各个操作都需要用到book,所以创建一个接口IOperation,以便提高程序运行的效率.

package operation;
import book.BookList;
import java.util.Scanner;

public interface IOperation 
    Scanner scanner=new Scanner(System.in);
    void work(BookList bookList);

(2)AddOperation

package operation;

import book.Book;
import book.BookList;

public class AddOperation implements IOperation
       public void work(BookList bookList)
           System.out.println("新增图书!");
           System.out.println("新增图书的名字:");
           String name =scanner.nextLine();
           System.out.println("请输入图书的作者:");
           String author = scanner.nextLine();
           System.out.println("请输入图书的价格:");
           int price = scanner.nextInt();
           System.out.println("请输入图书的类型:");
           String type = scanner.nextLine();

           Book book =new Book(name,author,price,type);
           int size=bookList.getUsedSize();
           bookList.setBook(size,book);
           bookList.setUsedSize(size+1);
           System.out.println("新增图书成功!");
       

(3)BorrowOperation

package operation;

import book.BookList;
import book.Book;

public class BorrowOperation implements IOperation
    public void work(BookList bookList)
        System.out.println("借阅图书!");
        System.out.println("请输入你想借阅图书的名字:");
        String name = scanner.nextLine();
        int size=bookList.getUsedSize();
        for (int i=0;i<size;i++)
            Book book=bookList.getPos(i);
            if(name.equals(book.getName()))
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                System.out.println(book);
                return;
            
        
        System.out.println("不存在你需要借阅的书籍!");
    

(4)DelOperation

package operation;

import book.Book;
import book.BookList;

public class DelOperation implements IOperation
    public void work(BookList bookList)
        System.out.println("删除图书!");
        //1.根据书名找到书的位置 index
        System.out.println("请输入你要删除的书的名字:");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        int index = 0;//存储找到的下标
        int i=0;
        for (;i<currentSize;i++)
            Book book=bookList.getPos(i);
            if(book.getName().equals(name))
                index=i;
                break;
            
        
        if (i>=currentSize)
            System.out.println("没有你要删除的这本书!");
            return;
        
        //2.进行删除
        for (int j=0;j<currentSize-1;j++)
            Book book=bookList.getPos(j+1);
            bookList.setBook(j,book);
        
        bookList.setBook(currentSize,null);
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功!");
    

(5)DisplayOperation

package operation;

import book.BookList;
import book.Book;

public class DisplayOperation implements IOperation 
    public void work(BookList bookList)
        System.out.println("打印图书!");
        int size=bookList.getUsedSize();
        for (int i=0;i<size;i++)
            Book book=bookList.getPos(i);
            System.out.println(book);
        
    

(6)ExitOperation

package operation;

import book.BookList;

public class ExitOperation implements IOperation 
    public void work(BookList bookList)
        System.out.println("退出系统!");
        System.exit(0);
    

(7)FindOperation

package operation;

import book.BookList;
import book.Book;

public class FindOperation implements IOperation
    public void work(BookList bookList) 
        System.out.println("查找图书:");
        String name=scanner.nextLine();
        int size=bookList.getUsedSize();
        for (int i=0;i<size;i++)
            Book book=bookList.getPos(i);
            if(name.equals(book.getName()))
                System.out.println("找到了这本书,信息如下:");
                System.out.println(book);
                return;
            
        
        System.out.println("没有找到这本书!");
    

(8)ReturnOperation

package operation;

import book.Book;
import book.BookList;

public class ReturnOperation implements IOperation
    public void work(BookList bookList)
        System.out.println("归还图书");
        System.out.println("请输入你要归还的书的名字:");
        String name=scanner.nextLine();
        int size=bookList.getUsedSize();
        for (int i=0;i<size;i++)
            Book book=bookList.getPos(i);
            if (name.equals(book.getName()))
                book.setBorrowed(false);
                System.out.println("归还成功!");
                System.out.println(book);
                return;
            
        
        System.out.println("没有你要归还的书!");
    

3.user类

(1)AdminUser

package user;

import book.BookList;
import operation.*;
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 DisplayOperation()
        ;
    

    public int menu() 
        System.out.println("===========管理员菜单===========");
        System.out.println("hello " + this.name +" 欢迎来到这里!");
        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);
        int choice = scanner.nextInt();
        return choice;
    


(2)NormalUser

package user;

import operation.*;
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("===========普通用户的菜单===========");
        System.out.println("hello " + this.name +" 欢迎来到这里!");
        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);
        int choice = scanner.nextInt();
        return choice;
    

(3)User

package user;

import book.BookList;
import operation.IOperation;

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) 
        iOperations[choice].work(bookList);
    

main函数

import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;

public class Main 

    public static User login()
        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=login();//向上转型
        while (true)
            int choice=user.menu();//发生了动态绑定->多态
            //根据你的choice调用合适的操作
            user.dowork(choice,bookList);
        
    

总结

提示:以上是一个简单的图书管理系统代码实现,后续还将有更多的功能添加其中。

以上是关于一个简单的图书管理系统的主要内容,如果未能解决你的问题,请参考以下文章

使用JDBC+javafx写一个简单功能齐全的图书管理系统

vue.js快速搭建图书管理平台

源码C语言 + 图形库 = 简单又好看的图书管理系统!

控制台图书管理系统

如何使用Android片段管理器传递变量[重复]

简单工厂的使用(图书管理系统)