用Java实现简单的图书管理系统(Java系列7)

目录

前言:

1.基础框架的搭建

1.1图书

1.1.1书

1.1.2书架

1.2用户

1.2.1抽象类

1.2.2普通用户

 1.2.3管理员

1.3操作

1.3.1新增图书

1.3.2借阅图书

1.3.3删除图书

1.3.4退出图书

1.3.5查找图书

1.3.6归还图书

1.3.7显示图书

2.具体内容的实现

2.1Main:主代码展示

2.3user:使用者代码展示

3.结果展示

3.1管理员用户展示

3.2普通用户展示

结束语:


前言:

之前我们学习了Java的一些基础知识,我们从数据类型-》变量-》运算符-》循环选择-》方法-》数组-》类和对象-》封装-》继承-》多态-》抽象类-》接口,那么下面我们就运用这些知识来实现一个简单版的图书管理系统。

1.基础框架的搭建

1.1图书

图书我们主要分为两大模块书和书架。

1.1.1书

我们要实现的目的是下面的这种:

 

主要要有五个属性:name、author、price、type、isBorrowed。

具体代码如下所示:

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=" +
                (isBorrowed == true ? "已被借出" : "未被借出") +
                '}';
    }
}

 

1.1.2书架

在书架上我们需要先给书架上面默认放上几本书:

具体代码如下所示:

package book;
//书架
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;//记录当前的数的数目为3
    }

    public Book[] getBooks(int pos) {
        return books;
    }

    public void setBooks(Book[] books) {
        this.books = books;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

1.2用户

我们主要分为普通用户和管理员。

下面我们具体实现一下两类。

1.2.1抽象类

我们在设计的时候注意到管理员和普通用户有一些属性是一样的所以我们可以对其共性进行抽取。

此时我们还需要实现一个操作的接口。

代码如下所示:

package opear;

import book.BookList;

public interface IOperation {
    void work(BookList bookList);//操作是对书架上的书进行操作的
}

代码如下所示:

package user;

import opear.IOperation;

public class User {
    protected String name;
    protected IOperation[] iOperations;//管理员和普通用户都会有一些具体的操作方法那么此时我们就可以实现成一个接口

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

1.2.2普通用户

对于普通用户来说主要实现四个功能就行:查找图书、借阅图书、归还图书、退出系统。

具体代码如下所示:

package user;

import opear.*;

import java.util.Scanner;

public class AdminUser extends User{
    public AdminUser(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" + 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;
    }

}

 1.2.3管理员

对于管理员来说主要实现五个功能就行:查找图书、新增图书、删除图书、显示图书、退出系统。 

具体代码如下所示:

package user;

import opear.*;

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

}

1.3操作

对于操作我们基于上面管理员和普通用户的操作可以得治我们主要实现一下的操作就可以啦!下面我们来分别具体实现一下。

1.3.1新增图书

具体代码如下所示:

package opear;

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.getBooks(i);
            if(tmp.getName().equals(name)) {
                System.out.println("已经存在这本书了,不能再放入了!");
                return;
            }
        }
        //修改书架上的数目
        bookList.setBook(book);
        bookList.setUsedSize(currentSize - 1);
    }
}

1.3.2借阅图书

具体代码如下所示:

package opear;

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) && !book.isBorrowed()) {
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
        }
    }
}

1.3.3删除图书

具体代码如下所示:

package opear;

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.getBooks(i);
            if (tmp.getName().equals(name)) {
                index = i;
                break;
            }
        }
        //挪动数据
        for (int j = index; j < currentSize - 1; j++) {
            Book book = bookList.getBooks(j + 1);
            bookList.setBook(j,book);
        }
        //修改size
        bookList.setUsedSize(currentSize - 1);
        //因为删除的是对象,所以把最后一个位置置为null
        bookList.setBook(currentSize - 1,null);
        System.out.println("删除成功!");
    }
}

1.3.4退出图书

具体代码如下所示:

package opear;

import book.BookList;

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

1.3.5查找图书

具体代码如下所示:

package opear;

import book.Book;
import book.BookList;

import java.util.Scanner;

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

1.3.6归还图书

具体代码如下所示:

package opear;

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.getBooks(i);
            if (book.getName().equals(name) && book.isBorrowed()) {
                book.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            }
        }
    }
}

1.3.7显示图书

具体代码如下所示:

package opear;

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

2.具体内容的实现

2.1Main:主代码展示

import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;

import javax.jws.soap.SOAPBinding;
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();
        //int choice = user.menu();
        //根据choice和user来确定我们到底调用的是哪一个对象进行操作的。
        while (true) {
            int choice = user.menu();
            //根据choice和user来确定我们到底调用对象哪一个操作。
            user.doWork(choice,bookList);
        }
    }
}

2.2oper:操作代码展示。

①新增图书。

package opear;

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.getBooks(i);
            if(tmp.getName().equals(name)) {
                System.out.println("已经存在这本书了,不能再放入了!");
                return;
            }
        }
        //修改书架上的数目
        bookList.setBook(book);
        bookList.setUsedSize(currentSize + 1);
    }
}

 ②借阅图书。

package opear;

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) && !book.isBorrowed()) {
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
        }
    }
}

package opear;

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.getBooks(i);
            if (tmp.getName().equals(name)) {
                index = i;
                break;
            }
        }
        //挪动数据
        for (int j = index; j < currentSize - 1; j++) {
            Book book = bookList.getBooks(j + 1);
            bookList.setBook(j,book);
        }
        //修改size
        bookList.setUsedSize(currentSize - 1);
        //因为删除的是对象,所以把最后一个位置置为null
        bookList.setBook(currentSize - 1,null);
        System.out.println("删除成功!");
    }
}

④退出系统。

package opear;

import book.BookList;

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

 ⑤查找图书。

package opear;

import book.Book;
import book.BookList;

import java.util.Scanner;

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

 ⑥归还图书。

package opear;

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.getBooks(i);
            if (book.getName().equals(name) && book.isBorrowed()) {
                book.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            }
        }
    }
}

⑦显示图书。

package opear;

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

 ⑧接口代码。

package opear;

import book.BookList;

public interface IOperation {
    void work(BookList bookList);//操作是对书架上的书进行操作的
}

2.3user:使用者代码展示

①管理员代码。

package user;

import opear.*;

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

}

②普通用户代码。

package user;

import opear.*;

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" + 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 user;

import book.BookList;
import opear.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) {
        this.iOperations[choice].work(bookList);
    }
}

3.结果展示

3.1管理员用户展示

3.2普通用户展示

 

结束语:

好啦这次小编主要基于之前学习的一些Java基础语法知识带着大家一起实现了一个简易版的图书代码,这次小编就给大家分享到这里啦!希望对大家有所帮助,想要学习的同学记得关注小编和小编一起学习吧!如果文章中有任何错误也欢迎各位大佬及时为小编指点迷津(在此小编先谢过各位大佬啦!) 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力敲代码的小白✧٩(ˊωˋ*)و✧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值