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

本文用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接口。

 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);
        }
    }
}
  • 20
    点赞
  • 114
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
好的,下面是一个简单的Java图书管理系统示例: ```java import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class BookManagementSystem { private static List<Book> books = new ArrayList<>(); private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { while (true) { System.out.println("请选择操作:1. 添加图书 2. 删除图书 3. 修改图书 4. 查询图书 5. 退出"); int choice = scanner.nextInt(); switch (choice) { case 1: addBook(); break; case 2: deleteBook(); break; case 3: updateBook(); break; case 4: queryBook(); break; case 5: System.out.println("谢谢使用!"); System.exit(0); break; default: System.out.println("无效的选择,请重新输入!"); break; } } } private static void addBook() { System.out.println("请输入图书名称:"); String name = scanner.next(); System.out.println("请输入图书作者:"); String author = scanner.next(); System.out.println("请输入图书价格:"); double price = scanner.nextDouble(); Book book = new Book(name, author, price); books.add(book); System.out.println("添加成功!"); } private static void deleteBook() { System.out.println("请输入要删除的图书名称:"); String name = scanner.next(); boolean flag = false; for (Book book : books) { if (book.getName().equals(name)) { books.remove(book); flag = true; break; } } if (flag) { System.out.println("删除成功!"); } else { System.out.println("没有找到该图书!"); } } private static void updateBook() { System.out.println("请输入要修改的图书名称:"); String name = scanner.next(); boolean flag = false; for (Book book : books) { if (book.getName().equals(name)) { System.out.println("请输入新的图书名称:"); String newName = scanner.next(); book.setName(newName); System.out.println("请输入新的图书作者:"); String author = scanner.next(); book.setAuthor(author); System.out.println("请输入新的图书价格:"); double price = scanner.nextDouble(); book.setPrice(price); flag = true; break; } } if (flag) { System.out.println("修改成功!"); } else { System.out.println("没有找到该图书!"); } } private static void queryBook() { System.out.println("请输入要查询的图书名称:"); String name = scanner.next(); boolean flag = false; for (Book book : books) { if (book.getName().equals(name)) { System.out.println(book); flag = true; break; } } if (!flag) { System.out.println("没有找到该图书!"); } } static class Book { private String name; private String author; private double price; public Book(String name, String author, double price) { this.name = name; this.author = author; this.price = price; } 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 double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "书名:" + name + " 作者:" + author + " 价格:" + price; } } } ``` 运行程序后,用户可以选择添加、删除、修改或查询图书。添加图书需要输入图书名称、作者和价格,删除图书需要输入图书名称,修改图书需要输入原有的图书名称和新的图书信息,查询图书需要输入图书名称。系统会根据用户的选择执行相应的操作,并输出结果。用户可以选择退出程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值