Java开发实现图书管理系统(超详细)

本文用Java代码实现图书代码管理系统,有些地方可能会有纰漏,希望各位大佬鉴赏!!

文章目录

  • 文章目录

    一、Java实现图书管理系统

    1.1创建book包

    二、创建图书管理系统的操作包

    2.1创建Operation接口

    三、创建User包

    3.1创建User类

    四、主函数的实现


一、Java实现图书管理系统

1.1创建book包

首先在book中要创建Book类和BookList类,进行封装图书,在Book类中要有书名,作者,价格,类型和是否被借出图书。在BookList类中是书架对图书的封装,进行图书初始化和对图书设置的各种方法。

Book类的实现

package book;

/**
 * @author xyk的电脑
 * @version 1.0
 * @description: TODO
 * @date 2023/1/24 13:48
 */
public class Book {

    private String name;//书名
    private String author;//作者
    private int price;//价格
    private String type;//类型
    private boolean isBorrowed;//是否被借出

    public Book(){
    }

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

}

BookList类的实现

package book;

/**
 * @author xyk的电脑
 * @version 1.0
 * @description: 书架
 * @date 2023/1/24 13:51
 */
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(int pos,Book book){
        this.books[pos] = book;
    }

    public void setBook(Book book){
        this.books[usedSize] = book;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

二、创建图书管理系统的操作包

2.1创建Operation接口

创建Operation接口来实现对图书的相关具体操作,其中管理员和普通用户的功能不同,需要都来实现Operation接口。

Java开发实现图书管理系统(超详细)_第1张图片

 IOPeration接口的实现

package opera;

import book.BookList;

public interface IOPeration {

    void work(BookList bookList);

}

AddOperation类的实现

package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @author xyk的电脑
 * @version 1.0
 * @description: TODO
 * @date 2023/1/24 14:01
 */
public class AddOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        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);
    }
}

BrrowOperation类的实现

package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @author xyk的电脑
 * @version 1.0
 * @description: TODO
 * @date 2023/1/24 14:05
 */
public class BrrowOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        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;
            }
        }
    }
}

DelOperation类的实现

package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @author xyk的电脑
 * @version 1.0
 * @description: TODO
 * @date 2023/1/24 14:03
 */
public class DelOpertaion 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 book = bookList.getBook(i);
            if (book.getName().equals(name)){
               index = i;
               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);
    }
}

ExitOperation类的实现

package opera;

import book.BookList;

/**
 * @author xyk的电脑
 * @version 1.0
 * @description: TODO
 * @date 2023/1/24 14:04
 */
public class ExitOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统!");
        System.exit(0);
    }
}

FindOperation类的实现

package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @author xyk的电脑
 * @version 1.0
 * @description: TODO
 * @date 2023/1/24 14:01
 */
public class FindOperation implements IOPeration {

    @Override
    public void work(BookList bookList) {
        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("没有这本书!");
    }
}

ReturnOperation类的实现

package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @author xyk的电脑
 * @version 1.0
 * @description: TODO
 * @date 2023/1/24 14:05
 */
public class ReturnOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        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;
            }
        }

    }
}

ShowOperation类的实现

package opera;

import book.Book;
import book.BookList;

/**
 * @author xyk的电脑
 * @version 1.0
 * @description: TODO
 * @date 2023/1/24 14:04
 */
public class ShowOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

三、创建User包

3.1创建User类

创建抽象类User来实现区别管理员和用户的主要功能。

package user;

import book.Book;
import book.BookList;
import opera.IOPeration;

/**
 * @author xyk的电脑
 * @version 1.0
 * @description: TODO
 * @date 2023/1/24 13:57
 */
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);
    }
}

NormalUser类的实现

package user;

import opera.*;

import java.util.Scanner;

/**
 * @author xyk的电脑
 * @version 1.0
 * @description: TODO
 * @date 2023/1/24 13:58
 */
public class NormalUser extends User{

    public NormalUser(String name) {
        super(name);
        this.ioPerations = new IOPeration[]{
                new ExitOperation(),
                new FindOperation(),
                new BrrowOperation(),
                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;

    }
}

AdminUser类的实现

package user;

import opera.*;

import java.util.Scanner;

/**
 * @author xyk的电脑
 * @version 1.0
 * @description: TODO
 * @date 2023/1/24 13:57
 */
public class AdminUser extends User{

    public AdminUser(String name) {
        super(name);
        this.ioPerations = new IOPeration[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOpertaion(),
                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;
    }
}

四、主函数的实现

在主函数中来进行图书管理系统的操作。

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

import java.util.Scanner;

/**
 * @author xyk的电脑
 * @version 1.0
 * @description: TODO
 * @date 2023/1/24 13:44
 */
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来确定调用哪个对象的操作
            user.doWork(choice, bookList);
        }
    }
}

你可能感兴趣的:(java,java,开发语言,idea,面试)