Java项目——图书馆管理系统

在实现一个项目的时候,我们一般将不同功能放在不同的文件中,以下目录中,标题一是软件包,标题一下的标题二,是我在软件包中定义的类。最后的标题一,是主函数。

目录

1. Book——书软件包

1.1 Book——书类

1.2 BookList——书架类

2. Operations——方法软件包

2.1Ioperations——方法接口类

2.2 AddOperations——添加图书类

2.3 BorrowOperations——借阅图书方法类

2.4 DeleteOperations——删除图书方法类

2.5 DisplayOperations——显示图书方法类

2.6 FindOperations——查询图书方法类

2.6 ReturnOperations——归还图书方法类

2.7 ExitOperations——退出程序方法类

3. User——用户软件包

3.1 User——用户抽象类

3.2 AdminUser——管理员类

3.3 NormalUser——普通用户类

4. 主函数


1. Book——书软件包

1.1 Book——书类

这里面定义了一个书的类,包括了书名,作者,价格,类型,借阅状态,其中有get方法,set方法,便于我们后续的使用。

package book;

/**
 * @author shkstart
 * @creat 2024-04-19 18:51
 */
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 String getAuthor() {
        return author;
    }

    public int getPrice() {
        return price;
    }

    public String getType() {
        return type;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

    @Override
    public String toString() {
        String a = "未借出";
        String b = "已借出";
        String c;
        if(isBorrowed){
            c=b;
        }
        else{
            c=a;
        }
        return "Book{" +
                "书名='" + name + '\'' +
                ", 作者='" + author + '\'' +
                ", 价格=" + price +
                ", 类型='" + type + '\'' +
                ","+ c +
                '}';
    }
}

1.2 BookList——书架类

书架类,这里面我们定义了一个书类的数组用来保存书。

package book;

public class BookList {
    public Book[] books = new Book[10];
    public int usedSize;
    public BookList(){
        books[0] = new Book("三国演义","罗贯中",30,"小说");
        books[1] = new Book("西游记","吴承恩",40,"小说");
        books[2] = new Book("红楼梦","曹雪芹",50,"小说");
        books[3] = new Book("水浒传","施耐庵",20,"小说");
        this.usedSize = 4;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
    public Book getPos(int pos){
        return books[pos];
    }
    public void setBooks(Book book){
        books[usedSize] = book;
        usedSize +=1;
    }

    public void deleteBooks(String name){
        for (int i = 0; i < usedSize; i++) {
            if(name.equals(books[i].getName())){
                for(int j=i;j<usedSize-1;j++){
                    books[j] = books[j+1];
                }
                usedSize-=1;
                books[i] =null;
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("没有找到你要删除的这本书");
    }
}

2. Operations——方法软件包

2.1Ioperations——方法接口类

work抽象方法,之后的动态绑定,让程序更加简洁有序。

package operations;
import book.BookList;

public interface IOperations {
    void work(BookList bookList);
}

2.2 AddOperations——添加图书类

package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;

public class AddOperation implements IOperations{
    public void work(BookList bookList){
        //添加图书
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你要添加图书的名称:");
        String name = scanner.nextLine();
        System.out.print("请输入你要添加图书的作者:");
        String author = scanner.nextLine();
        System.out.print("请输入你要添加图书的价格:");
        int price = scanner.nextInt();
        System.out.print("请输入你要添加图书的类型:");
        String type = scanner.nextLine();
        type = scanner.nextLine();
        Book book = new Book(name,author,price,type);
        bookList.setBooks(book);
    }
}

2.3 BorrowOperations——借阅图书方法类

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @author shkstart
 * @creat 2024-04-19 19:10
 */
public class BorrowOperation implements IOperations{
    @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.getPos(i);
            if(name.equals(book.getName())){
                if(!(book.isBorrowed())){
                    book.setBorrowed(true);
                    System.out.println("你已经成功借走这本书了");
                    return;
                }else{
                    System.out.println("这本书已经被借走了");
                }
            }
        }
        System.out.println("没有这本书");
    }
}

2.4 DeleteOperations——删除图书方法类

package operations;

import book.BookList;

import java.util.Scanner;

/**
 * @author shkstart
 * @creat 2024-04-19 19:06
 */
public class DeleteOperation implements IOperations{

    public void work(BookList bookList){
        //删除图书
        System.out.println("请输入你要删除的图书的名字");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        bookList.deleteBooks(name);
    }
}

2.5 DisplayOperations——显示图书方法类

package operations;

import book.Book;
import book.BookList;

import java.util.Arrays;

/**
 * @author shkstart
 * @creat 2024-04-19 19:06
 */
public class DisplayOperation implements IOperations{

    public void work(BookList bookList){
        //显示图书
       for(int i =0;i<bookList.getUsedSize();i++){
           Book book = bookList.getPos(i);
           System.out.println(book);
       }
    }
}

2.6 FindOperations——查询图书方法类

package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;

//实现接口的意义,我们可以轻易的完成向上转型,就意味着动态绑定,和多态
public class FindOperation implements IOperations{
    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.getPos(i);
            if(name.equals(book.getName())){
                System.out.println("找到了这本书");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有这本书");
    }
}

2.6 ReturnOperations——归还图书方法类

package operations;
import book.Book;
import book.BookList;
import java.util.Scanner;

public class ReturnOperation implements IOperations{
    @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.getPos(i);
            if(name.equals(book.getName())){
                if(book.isBorrowed()){
                    System.out.println("这本书已经还在书架上呢");
                }
                else{
                    book.setBorrowed(false);
                    System.out.println("归还成功");
                }
            }
        }
        System.out.println("没有查询到你输入的这本书");
    }
}

2.7 ExitOperations——退出程序方法类

package operations;

import book.BookList;

/**
 * @author shkstart
 * @creat 2024-04-19 19:09
 */
public class ExitOperation implements IOperations{
    @Override
    public void work(BookList bookList) {
        System.exit(0);
    }
}

3. User——用户软件包

不同的用户有不同的权限,我们将所有的方法都写出来,不同的用户有不同的方法使用权限。

3.1 User——用户抽象类

package user;

import book.BookList;
import operations.IOperations;

public abstract class User {
    protected String name;
    protected IOperations[] iOperations;
    public User(String name){
        this.name= name;
    }

    public abstract int menu();
    public void doOperations(int choice, BookList bookList){
        iOperations[choice].work(bookList);
    }
}

3.2 AdminUser——管理员类

package user;

import operations.*;

import java.util.Scanner;

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.iOperations=new IOperations[] {
            new ExitOperation(),
            new FindOperation(),
            new AddOperation(),
            new DeleteOperation(),
            new DisplayOperation()
        }; //需要写一个操作数组的方法
    }

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

3.3 NormalUser——普通用户类

package user;

import operations.*;

import java.util.Scanner;

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.iOperations=new IOperations[] {
            new ExitOperation(),
            new FindOperation(),
            new AddOperation(),
            new DeleteOperation(),
            new DisplayOperation()
        }; //需要写一个操作数组的方法
    }

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

4. 主函数

最后,就是主函数,在主函数里我们写了登陆界面和主循环,通过不同的选择,进入到不同的用户类。通过不同的用户类,有不同的方法类使用权限。

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 userName = scanner.nextLine();
        System.out.println("请输入你的身份\n1->普通用户\n2->管理员\n");
        int choice = scanner.nextInt();
        if (choice == 1) {
            return new NormalUser(userName);
        } else {
            return new AdminUser(userName);
        }
    }
    public static void main(String[] args) {
        //0. 准备数据
        BookList bookList = new BookList();
        //1. 登录
        User user = Login();
        while (true){
           int choice = user.menu();
           user.doOperations(choice,bookList);
       }
    }
}


最后,通过运行主函数,我们可以使用图书馆管理系统。

  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值