Java实现简单的图书管理系统

user通过operation操作book,大致思想如下

 在对于图书的操作中,一共出现了三类对象 分别是:用户  操作  书 ,其中 用户分为管理员和普通用户,操作是例如借阅归还等具体动作,书分为 书本身 以及 书架

主函数如上,

第一步,new一个BookList 类型的bookList ,对图书的所有操作基于bookList

第二部,login() 函数回根据你选择的不同,new不同的用户身份,用父类user接收,此处发生向上转型

第三步,根据用户所选的choice,和bookList当成参数完成具体操作

具体代码

user包

User

package user;

import Operation.IOperation;
import book.BookList;

//普通用户和管理员都将继承User
public abstract class User {
    protected String name;
    protected IOperation[] IOperations;
    public User(String name){

        this.name = name;
    }
    public abstract int menu();
    public void doOperation(int choice, BookList bookList){
       // this.ioPerations[choice].work(bookList);
        IOperation ioPeration = this.IOperations[choice];
        ioPeration.work(bookList);
    }
}

AdminUser

package user;

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 ShowOperation(),
        };
    }
    public int menu(){
        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("**************************");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

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 ReturnOperatuon(),
                new ShowOperation()
        };
    }
    public int menu(){
       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("**************************");
       System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
   }
}

Operation包

IOperation

package Operation;
import book.BookList;
public interface IOperation {
    void work(BookList bookList);
}

AddOpeation

package Operation;

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 book = new Book(name,author,type,price);
        int currentSize = bookList.getUsedSize();
        for (int i = 0;i<currentSize;i++){
            Book tmp = bookList.getBooks(i);
            if (tmp.getName().equals(name)){//book.getName是什么 name是什么
                System.out.println("存在这本书,信息如下");
                System.out.println(book);
                return;
            }
        }
        //没有重复的书,开始新增
        bookList.setBooks(book,currentSize);
        System.out.println("新增成功! 新增书的信息如下:");
        System.out.println(book);
        bookList.setUsedSize(currentSize+1);
    }
}

BorrowOperation

package Operation;
import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOperation 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.getBooks(i);
            if(book.getName().equals(name)){
                //存在这本书且未被借出
                if(!book.isBorrowed()){
                    book.setBorrowed(true);
                    System.out.println("借阅成功!");
                    return;
                }
            }
        }
        System.out.println("没有此书或者该书已经借出:"+name);
    }
}

DelOperation

import java.util.Scanner;

public class DelOperation implements IOperation {
    public void work(BookList bookList){
        System.out.println("删除图书!");
        Scanner scanner = new Scanner(System.in) ;
        System.out.println("请输入你要删除的图书名:");
        int index = -1;
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        for (int i=0;i<currentSize;i++){
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)){
                index=i;//找到书,得到该书的下标
                System.out.println("删除成功! 删除书目为:");
                System.out.println(book);
                break;
            }
        }
        if(index==-1){
            System.out.println("没有你要删除的图书!");
            return;
        }
        //可以删除了
        //移动数据进行覆盖,size--
        for (int j=index;j<currentSize-1;j++){
           // bookList[j]=bookList[j+1];
            //前移 覆盖
            Book book = bookList.getBooks(j+1);
            bookList.setBooks(book,j);
            //图书的size--
            bookList.setUsedSize(currentSize-1);
            //回收被删除的空间
            bookList.setBooks(null,currentSize-1);
        }

    }

}

ExitOperation

package Operation;
import book.BookList;
public class ExitOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统!");
        //应该还需要对booklist进行资源回收
        System.exit(0);
    }
}

FindOperation

package Operation;

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.getBooks(i);
            if(book.getName().equals(name)){
                System.out.println("存在此书,信息如下:");
                System.out.println(book);
                return;
            }
        }
        //代码还没return出去,说明没有找到
        System.out.println("没有找到书名为:"+name+"的书");
    }
}

ReturnOperation

package Operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOperatuon 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.getBooks(i);
            if(book.getName().equals(name)){
                //这本书被借出
                if(book.isBorrowed()){
                    //归还
                    book.setBorrowed(false);
                    System.out.println("归还成功!");
                    return;
                }
            }
        }
        System.out.println("没有此书或者该书已经被归还:"+name);
    }
}

ShowOperation

package Operation;

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.getBooks(i);
            System.out.println(book);
            System.out.println();
        }
    }
}

book包

Book

package book;

public class Book {
    private String name;//书名
    private String author;//作者
    private String type;//类型
    private int price;//价格
    private  boolean isBorrowed;//是否被借出 默认false


    public Book(String name,String author,String type,int price) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name=name;
    }
    public String getAuthor(){
        return this.author;
    }
    public void setAuthor(String author){
        this.author=author;
    }
    public String getType(){
        return this.type;
    }
    public void setType(String type){
        this.type=type;
    }
    public int getPrice(){
        return this.price;
    }
    public void setPrice(int price){
        this.price=price;
    }
    public boolean isBorrowed(){
        return this.isBorrowed;
    }
    public void setBorrowed(boolean borrowed){
        this.isBorrowed=borrowed;
    }

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

}

BookList

package book;

public class BookList {
    private Book[] books;
    private int usedSize;//记录当前书架放了几本书
    public BookList(){
        this.books = new Book[10];
        this.books[0] = new Book("三国演义","罗贯中","小说",10);
        this.books[1] = new Book("水浒传","施耐庵","小说",11);
        this.books[2] = new Book("红楼梦","曹雪芹","小说",12);
        this.books[3] = new Book("西游记","吴承恩","小说",13);
        this.usedSize = 4;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
    public Book getBooks(int pos){
        return books[pos];
    }
    public void setBooks(Book book,int pos){
        books[pos] = book;
    }
    public void delBooks(String name){

    }
}

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.管理员  2.普通用户  --->");
        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();
           user.doOperation(choice,bookList);
           //根据菜单返回的choice来执行对应的操作
       }
    }
}

图书资料管理信息系统,带源代码、数据库sql文件、课设报告,具备如下基本功能: 1、 系统管理功能有:角色管理、用户管理、修改密码。主要实现系统的安全管理,不同的操作者有不同的权限,可以执行不同的操作。普通读者的权限只能是查询图书及自己的借阅情况;而图书馆管理员可以对图书信息进行管理,如对新书入库,也可以管理用户,如添加新用户和删除不用的账号等。 2、 进书管理功能有:登记基本的图书信息。这部分的功能用于登记新书的书名、作者、出版社、价格、进书的册数、进书日期、ISBN等。 3、 图书入库管理功能有:对新书分类编目,及时更新图书库中的图书信息。这部分的功能用于对所购进的新书,按其种类学科进行编目,给与唯一的书号;及时更新书库中的图书信息,包括书名、书号、作者、出版社、价格、库存位置和库存册数这些信息,方便读者查询借阅。 4、 查询功能功能有:查询图书的信息,查询读者的借阅情况。这部分的功能主要提供多种方式的查询服务。读者可以根据书名、作者或关键字模糊查询图书信息;读者也可以根据自己的借书证号查询自己的借阅情况,如已借了几本书,借书日期,还书日期,有没有续借等。 5、 借书/还书管理功能有:借书管理、还书管理。这部分的功能是当读者借书时,系统根据借书证号识别读者身份,核对读者的借书信息,做出判断如可不可以借、还可借几本,成功借阅后记录在借书信息并修改书库图书信息。当读者还书时,系统根据借书证号识别读者身份,核对读者的借书信息,做出判断如有没有超期,要不要罚款,需要罚多少等,最后还书成功,修改书库图书信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值