<< Java >> 图书管理系统

🐖🐖🐖🐖如果喜欢!!🐂🐂🐂🐂

🐖🐖🐖🐖欢迎关注!!🐂🐂🐂🐂

🐖🐖🐖🐖持续回访!  !🐂🐂🐂🐂

CSDN主页:所有博客内容,Java大树逐渐成长

Gitee地址:想看博客代码??点击这里

QQ : 1939639916(有问题可以加好友,提前说明哦)


目录

👄​​​​​1.Main

👄​​​​​2.User包

❤️User父类 

🙂2.1管理员子类

🙂2.2普通用户子类

👄3Book包

🙂3.1 Book

🙂3.2BookList

👄4 .operation

❤️IOperation接口

🙂4.1Add

🙂4.2 Borrow

🙂4.3 Delete

🙂4.4 Display

🙂 4.5 Exit

🙂4.7 Return



1.Main

import book.BookList;
import com.sun.xml.internal.ws.wsdl.writer.document.Operation;
import user.AdminUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

public class Main {

    private static User login(){
        System.out.println("请输入你的姓名");
        String name;
        Scanner scanner = new Scanner(System.in);
        name = scanner.nextLine();
        System.out.println("请输入你的身份---> 1.管理员  2.普通用户");
        int flag = scanner.nextInt();
        if(flag == 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);
        }
    }

}

2.User包

User父类 

package user;

import book.BookList;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import operation.IOperation;

public abstract class User {
    protected String name;
    protected IOperation[] operation;
    public User(String name) {
        this.name = name;
    }
    public void doOperation(int choice , BookList bookList){
        this.operation[choice].work(bookList);
    }
    public abstract int menu();
}

2.1管理员子类

package user;

import book.BookList;
import operation.*;

import java.util.Scanner;

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.operation = new IOperation[]{new Exit(),new Find(),new Add(),new Delete(),new Display()};
    }
    @Override
    public int menu() {
        System.out.println("1.查找图书!");
        System.out.println("2.新增图书!");
        System.out.println("3.删除图书!");
        System.out.println("4.显示图书!");
        System.out.println("0.退出系统!");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int flag = scanner.nextInt();
        return flag;
    }
}

2.2普通用户子类

package user;

import operation.*;

import java.util.Scanner;

public class NormalUser extends  User{
    public NormalUser(String name) {
        super(name);
        this.operation = new IOperation[]{new Exit(),new Find(),new Borrow(),new Return()};
    }

    @Override
    public int menu() {
        System.out.println("1.查找图书!");
        System.out.println("2.借阅图书!");
        System.out.println("3.归还图书!");
        System.out.println("0.推出系统!");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int flag = scanner.nextInt();
        return flag;
    }
}

3Book包

3.1 Book

package book;

import java.util.Objects;

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;
        this.isborrowed = false;
    }

    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 isIsborrowed() {
        return isborrowed;
    }

    public void setIsborrowed(boolean isborrowed) {
        this.isborrowed = isborrowed;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", isborrowed=" + isborrowed +
                '}';
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return price == book.price && isborrowed == book.isborrowed && Objects.equals(name, book.name) && Objects.equals(author, book.author) && Objects.equals(type, book.type);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, author, price, type, isborrowed);
    }
}

3.2BookList

package book;

public class BookList {
    //目前最多可以放十本书
    private Book[] books = new Book[10];
    private int usedSize;//用来记录现在有几本

    public BookList() {
        books[0]=new Book("三国演义","罗贯中",30,"四大名著");
        books[1]=new Book("西游记","吴承恩",40,"四大名著");
        books[2]=new Book("红楼梦","曹雪芹",40,"四大名著");
        books[3]=new Book("水浒传","施耐庵",30,"四大名著");
        usedSize = 4;
    }

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

    public Book getBooks(int pos) {
        return books[pos];
    }
    public void setBooks(int pos,Book books) {//pos位置合法,把book书放到pos位置
        this.books[pos] = books;
    }

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


}

4 .operation

IOperation接口

package operation;

import book.BookList;

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

4.1Add

package operation;

import book.Book;
import book.BookList;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.Scanner;

public class Add 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("请输入这本书的价格:");
        int price = scanner.nextInt();
        System.out.println("请输入这本书的类型:");
        String type = scanner.nextLine();
        Book book = new Book(name,author,price,type);
        bookList.setBooks(bookList.getUsedSize(), book);
        //将书架的数目增加 1
        bookList.setUsedSize(bookList.getUsedSize()+1);
    }
}

4.2 Borrow

package operation;

import book.BookList;

import java.util.Scanner;

public class Borrow implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入你要借的书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            if(name == bookList.getBooks(i).getName()){
                bookList.getBooks(i).setIsborrowed(true);
                System.out.println("借书成功!");
                return;
            }
        }
    }
}

4.3 Delete

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Delete implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("请输入你要删除的书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            if(name.equals(bookList.getBooks(i).getName())){

                for (int j = i; j<bookList.getUsedSize()-1;j++){
                    Book book = bookList.getBooks(j+1);
                    bookList.getBooks(j).setName(book.getName());
                    bookList.getBooks(j).setPrice(book.getPrice());
                    bookList.getBooks(j).setType(book.getType());
                    bookList.getBooks(j).setIsborrowed(book.isIsborrowed());
                }
                bookList.setUsedSize(bookList.getUsedSize()-1);
                System.out.println("已经成功删除这本书!!");
                return;
            }
        }
        System.out.println("没有这本书!!!");
        return;
    }
}

4.4 Display

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Delete implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("请输入你要删除的书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            if(name.equals(bookList.getBooks(i).getName())){

                for (int j = i; j<bookList.getUsedSize()-1;j++){
                    Book book = bookList.getBooks(j+1);
                    bookList.getBooks(j).setName(book.getName());
                    bookList.getBooks(j).setPrice(book.getPrice());
                    bookList.getBooks(j).setType(book.getType());
                    bookList.getBooks(j).setIsborrowed(book.isIsborrowed());
                }
                bookList.setUsedSize(bookList.getUsedSize()-1);
                System.out.println("已经成功删除这本书!!");
                return;
            }
        }
        System.out.println("没有这本书!!!");
        return;
    }
}

 4.5 Exit

package operation;

import book.BookList;

public class Exit implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("退出成功");
        System.exit(0);
    }
}

4.6Find

package operation;

import book.BookList;

import java.util.Scanner;

public class Find implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("请输入你要查找的书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            System.out.println("i=="+i);
            if(name.equals(bookList.getBooks(i).getName())){
                System.out.println("这本书的位置是"+i);
                System.out.println("这本书的信息是"+bookList.getBooks(i).toString());
                return;
            }
        }
        System.out.println("没有这本书!!");
        return;
    }
}

4.7 Return

package operation;

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

import java.util.Scanner;

public class Return implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("请输入你要归还的书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for(int i = 0;i< bookList.getUsedSize();i++){
            if(name == bookList.getBooks(i).getName()){
                bookList.getBooks(i).setIsborrowed(false);
                System.out.println("成功还书!");
                return;
            }
        }
        System.out.println("这本书不是图书馆的书,but已成功捐赠!!!");
        User user = new AdminUser("shan");
        user.doOperation(2,bookList);        
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

TestDevHub测试宝库

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

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

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

打赏作者

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

抵扣说明:

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

余额充值