Java图书管理系统(!代码在这里!)

上一篇博客带有讲解,所以为了更直观的看代码,这里就直接放好给大家了~


总体创建如下:

BookShelf

Book

package BookShelf;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isborrowed;

    public Book(String name, String author, String type, int price) {
        this.name = name;
        this.author = author;
        this.type = type;
        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 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 isborrowed) {
        this.isborrowed = isborrowed;
    }

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

BookList

package BookShelf;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isborrowed;

    public Book(String name, String author, String type, int price) {
        this.name = name;
        this.author = author;
        this.type = type;
        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 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 isborrowed) {
        this.isborrowed = isborrowed;
    }

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

operation

AddBook

package operation;

import BookShelf.Book;
import BookShelf.BookList;

import java.util.Scanner;

public class AddBook implements IOperation {
    public void work(BookList bookList){
        System.out.println("正在增加图书......");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书的名称");
        String bookName = 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(bookName, author, type, price);

        int currentSize = bookList.getValidszie();
        for (int i = 0; i < currentSize; i++) {
            if(bookList.getBooks(i).getName().equals(bookName)){
                System.out.println("存在此书,请勿重复添加");
                return;
            }
        }
        bookList.setBooks(currentSize,book);
        bookList.setValidSzie(currentSize+1);
    }

    private boolean isFull(BookList bookList){
        return bookList.getBooks().length == bookList.getValidszie();
    }
}

BorrowBook

package operation;

import BookShelf.Book;
import BookShelf.BookList;

import java.util.Scanner;

public class BorrowBook implements IOperation {
    public void work(BookList bookList){
        System.out.println("开始借阅图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要借阅的图书");
        String bookName = scanner.nextLine();

        int currentSize = bookList.getValidszie();
        for (int i = 0; i < currentSize; i++) {
            if(bookList.getBooks(i).getName().equals(bookName)){
                Book book = bookList.getBooks(i);
                if(book.isborrowed()){
                    System.out.println("此书已被借走");
                }else{
                    book.setborrowed(true);
                    System.out.println(book);
                    System.out.println("借阅成功");
                }
                return;
            }
        }
        System.out.println("未找到此书");
    }
}

DeleteBook

package operation;

import BookShelf.Book;
import BookShelf.BookList;

import java.util.Scanner;

public class DeleteBook implements IOperation {
    public void work(BookList bookList){
        System.out.println("正在删除图书......");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除图书的名称");
        String bookName = scanner.nextLine();

        int currentSize = bookList.getValidszie();
        int index = -1;
        int i = 0;
        for (i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBooks(i);
            if(bookList.getBooks(i).getName().equals(bookName)){
                index = i;
                break;
            }
        }
        if(index < 0){
            System.out.println("没有此书");
            return;
        }
        for (int j = index; j < currentSize-1; j++) {
            Book tmp = bookList.getBooks(j+1);
            bookList.setBooks(j,tmp);
        }
        bookList.setValidSzie(currentSize-1);
        bookList.setBooks(currentSize-1,null);
        System.out.println("已删除图书");
    }
}

ExitBook

package operation;
import BookShelf.BookList;

public class ExitBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("Exit BookSystem");
        int currentSize = bookList.getValidszie();
        for (int i = 0; i < currentSize; i++) {
            bookList.setBooks(i,null);
        }
        bookList.setValidSzie(0);
        System.exit(0);
    }
}

FindBook

package operation;

import BookShelf.Book;
import BookShelf.BookList;

import java.util.Scanner;

public class FindBook implements IOperation {
    public void work(BookList bookList){
        System.out.println("正在查找图书......");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要查找的书籍名称");
        String bookName = scanner.nextLine();

        int currentSize = bookList.getValidszie();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(bookName)) {
                System.out.println("找到了这本书,书籍信息如下所示");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有此书");

    }
}

IOperation【接口】

package operation;
import BookShelf.BookList;

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

ReturnBook

package operation;

import BookShelf.Book;
import BookShelf.BookList;

import java.util.Scanner;

public class ReturnBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("开始归还图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要归还的图书");
        String bookName = scanner.nextLine();

        int currentSize = bookList.getValidszie();
        for (int i = 0; i < currentSize; i++) {
            if(bookList.getBooks(i).getName().equals(bookName)){
                Book book = bookList.getBooks(i);
                if(!book.isborrowed()){
                    System.out.println("此书未被借出");
                }else{
                    book.setborrowed(false);
                    System.out.println(book);
                    System.out.println("归还成功");
                }
                return;
            }
        }
        System.out.println("未找到此书");
    }

}

ShowBook 

package operation;
import BookShelf.Book;
import BookShelf.BookList;

public class ShowBook implements IOperation{
    public void work(BookList bookList) {
        System.out.println("正在显示图书......");

        int currentSize = bookList.getValidszie();
        for (int i = 0; i < currentSize; i++) {
           Book tmp = bookList.getBooks(i);
           System.out.println(tmp);
        }
    }

}

User

AdminUser

package User;

import operation.*;

import java.util.Scanner;

public class AdminUser extends User{

    public AdminUser(String name) {
        super(name);
        this.ioPerations = new IOperation[]{
                new ExitBook(),
                new AddBook(),
                new FindBook(),
                new DeleteBook(),
                new ShowBook(),
        };
    }

    @Override
    public int menu() {
        System.out.println("欢迎"+this.name+"来到图书管理系统");
        System.out.println("******管理员菜单******");
        System.out.println("1 增加图书");
        System.out.println("2 查找图书");
        System.out.println("3 删除图书");
        System.out.println("4 显示图书");
        System.out.println("0 退出系统");
        System.out.println("********************");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;



    }
}

NormalUser

package User;

import operation.*;

import java.util.Scanner;

public class NormalUser extends User {
    public NormalUser(String name) {
        super(name);
        this.ioPerations = new IOperation[]{
                new ExitBook(),
                new BorrowBook(),
                new FindBook(),
                new ShowBook(),
                new ReturnBook()
        };
    }

    @Override
    public int menu() {
        System.out.println("欢迎"+this.name+"来到图书管理系统");
        System.out.println("******普通用户菜单******");
        System.out.println("1 借阅图书");
        System.out.println("2 查找图书");
        System.out.println("3 显示图书");
        System.out.println("4 归还图书");
        System.out.println("0 退出系统");
        System.out.println("********************");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;

    }
}

User 

package User;

import BookShelf.BookList;
import operation.IOperation;

public abstract class User {
    public String name;
    public IOperation[] ioPerations;

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

    public abstract int menu();

    public void doOperations(int choice, BookList books) {
        IOperation iOperation = this.ioPerations[choice];
        iOperation.work(books);
    }
}

 Main

import BookShelf.BookList;
import User.AdminUser;
import User.NormalUser;
import User.User;

import java.util.Scanner;

public class Main {
    public static User login(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your username: ");
        String name = sc.nextLine();

        System.out.println("Enter your identity: 1->AdminUser 2->NormalUser");
        int choice = sc.nextInt();


        if(choice == 1){
            return new AdminUser(name);
        }else if(choice == 2) 
            return new NormalUser(name);
        return null;
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = login();
        while(true){
            int choice = user.menu();
            user.doOperations(choice,bookList);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Re.不晚

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

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

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

打赏作者

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

抵扣说明:

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

余额充值