【JavaSE】图书管理系统

项目介绍

本项目为图书管理系统,对象主要是书和用户,用户包括管理员和普通用户,管理员的行为包括:添加图书,删除图书,查找图书,显示图书和退出程序,普通用户的行为包括:查找图书,借阅图书,归还图书和退出程序

 详细代码

book包 

Book类 

package book;

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

 BookList类

package book;

public class BookList {
    private Book[] books = new Book[100];
    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(Book book){
        this.books[usedSize] = book;
    }

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

    public void setBooks(int pos,Book book){
        books[pos] = book;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

 opera包

 IOperation接口

package opera;

import book.BookList;

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

 AddOperation类

package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOpration{

    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书!");
        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);

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            if (tmp.getName().equals(name)){
                System.out.println("已经存在这本书了,不能再放入了!");
                System.out.println(book);
                return;
            }
        }
        bookList.setBook(book);
        bookList.setUsedSize(currentSize+1);
        System.out.println("成功添加图书!");
    }
}

 BrrowOperation类

package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BrrowOperation implements IOpration{
    @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();
        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;
            }else if (book.getName().equals(name) && book.isBorrowed()){
                System.out.println("很抱歉,该图书已被借出!");
            }
        }
    }
}

 DelOperation类

package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOpration{
    @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 tmp = bookList.getBook(i);
            if (tmp.getName().equals(name)){
                index = i;
                break;
            }
        }
        for (int j = index; j < currentSize - 1; j++) {
            Book book = bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
        bookList.setUsedSize(currentSize - 1);

        bookList.setBook(currentSize - 1,null);
        System.out.println("删除成功!");
    }
}

 ExitOperation类

package opera;

import book.BookList;

public class ExitOperation implements IOpration{
    @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;

public class FindOperation implements IOpration{

    @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();
        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;

public class ReturnOperation implements IOpration{
    @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();
        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;
            }else if (book.getName().equals(name) && !book.isBorrowed()){
                System.out.println("很抱歉,该图书无需归还,已在图书馆中!");
            }
        }
    }
}

 ShowOperation类

package opera;

import book.Book;
import book.BookList;

public class ShowOperation implements IOpration{
    @Override
    public void work(BookList bookList) {
        System.out.println("打印所有图书!");
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

 user包

User类 

package user;

import book.BookList;
import opera.IOpration;

public abstract class User {
    protected String name;
    protected IOpration[] iOprations;

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

    public abstract int menu();

    public void doWork(int choice, BookList bookList){
        this.iOprations[choice].work(bookList);
    }
}

 AdminUser类

package user;

import opera.*;

import java.util.Scanner;

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.iOprations = new IOpration[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                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;
    }
}

 NormalUser类

import opera.*;

import java.util.Scanner;

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.iOprations = new IOpration[] {
                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;
    }
}

 Main--控制器

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 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();
            user.doWork(choice,bookList);
        }
    }
}

效果演示

管理者 

普通用户 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

墨染无尘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值