JAVA SE 图书管理系统

图书部分

该部分中有两个类分别为书籍(Book )和 书架(BookList),用来管理图书。

书籍(Book)

在Book类中我们定义了姓名,作者,价格,图书类型,借阅状态等六个字段。并使用快捷键(Alt+Insert)获取getset方法。

使用快捷键(Alt+Insert)写了一个包含姓名,作者,价格,图书类型的构造方法,还有toString方法。

注意:toString中boolean类型的isBrrowed要表示是否被借出,要用三目运算符:

((isBrrowed == true) ? "已借出" : "未借出")
package books;


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

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

    public void setBrrowed(boolean brrowed) {
        isBrrowed = brrowed;
    }

    @Override
    public String toString() {
        return "BookList{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ((isBrrowed == true) ? "已借出" : "未借出")+
                '}';
    }
}

书架(BookList)

该类是我们用来管理图书的,所以我们定以了Book[]数组用来放书,useSized来统计书架上一共有多少本书,并用快捷键(Alt+Inset)来获取Book[],useSized的get和set方法,并在构造方法初始化三本书。

package books;

import ioperations.Ioperations;


public class BookList {
    private Book[] books = new Book[10];
    private int useSized;

    public Book[] getBooks() {
        return books;
    }

    public Book getBooks(int pos) {
        return books[pos];
    }

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

    public int getUseSized() {
        return useSized;
    }

    public void setUseSized(int useSized) {
        this.useSized = useSized;
    }

    public BookList(){
        books[0] = new Book("三国演义","罗贯中",10,"小说");
        books[1] = new Book("西游记","吴承恩",12,"小说");
        books[2] = new Book("红楼梦","曹雪芹",11,"小说");
        this.useSized = 3;
    }
}

用户部分

该部分中用用户,管理员,普通用户这三大类。

用户(Users)

在管理员类和普通用户类包含许多共性,所以我们设置一个抽象类Users来抽取管理员类和普通用户类的共性。

package users;

import books.BookList;
import ioperations.Ioperations;

public abstract class Users {
    protected String name;
    public Ioperations[] ioperations;

    public Users(String name) {
        this.name = name;

    }
    public abstract int meau();

    public void doIoperations(int choice, BookList bookList) {
        ioperations[choice].work(bookList);
    }
}

管理员类 (AdminUser)

我们设置一个管理员菜单,在对着菜单设置接口的数组的具体操作。

package users;

import ioperations.*;

import java.util.Scanner;

public class AdminUser extends Users{
    public AdminUser(String name) {
        super(name);
        this.ioperations = new Ioperations[]{
                           new ExitOperation(),
                           new FindOperation(),
                           new AddOperation(),
                           new DelOperation(),
                           new ShowOperation()};}
    public int meau() {
        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("*********************");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

普通用户类(NormalUser)

和管理员设置一样,我们只需对着菜单把接口数组操作即可。

package users;

import ioperations.*;

import java.util.Scanner;

public class NormalUser extends Users{
    public NormalUser(String name) {
        super(name);
        this.ioperations = new Ioperations[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()
        };
    }

    public int meau() {
        System.out.println("******普通用户菜单******");
        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;
    }
}

操作部分

该部分实现了对书的增加,借阅,删除,查找,归还,展示,退出程序。这七个类和操作接口;

操作接口(Ioperations)

所有的操作都是对书架进行操作,所以我们设置一个接口供这些操作来实现,并传入书架的具体参数。

package ioperations;

import books.BookList;

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

添加操作(AddOperation)

首先,我们先判断书架是否已满,然后用户添加图书的相关信息,在为用户输入的信息新建一个对象,接下来,用新建的图书对象和书架上的图书一 一遍历,看看书架上是否有这本书,如果有则不能插入,如果没有则通过书架类的setBooks方法新增图书,并书架上的数量也加一,也就是setUserSized方法来增加数量。

package ioperations;

import books.Book;
import books.BookList;

import java.util.Scanner;


public class AddOperation implements Ioperations{
    @Override
    public void work(BookList bookList) {
        int correntSized = bookList.getUseSized();

        //判断书架是否已满
        if(correntSized == bookList.getBooks().length) {
            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 newbook = new Book(name,author,price,type);

        //判断书架是否有这本书
        for (int i = 0; i < correntSized; i++) {
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)) {
                System.out.println("有这本书不能插入!!!");
                return;
            }
        }

        //插入
        bookList.setBooks(correntSized,newbook);

        bookList.setUseSized(correntSized+1);

        System.out.println("新增成功!!!");

    }
}

借阅操作(BorrowOperation)

首先,用户添加想借阅书的相关信息,在和书架上的书进行遍历,如果借阅书的借阅状态为true,则直接输出该书已被借阅,如果书架上有并未借出,那就借,如果书架上没有,那就输出书架上没有这本书。

package ioperations;

import books.Book;
import books.BookList;

import java.util.Scanner;
public class BorrowOperation implements Ioperations{
    @Override
    public void work(BookList bookList) {
        int currentSized = bookList.getUseSized();
        System.out.println("请输入借阅书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < currentSized; i++) {
            Book book = bookList.getBooks(i);
            String bookName = book.getName();
            if(book.isBrrowed()) {
                System.out.println("书已经被借阅了.....");
                return;
            }
            if(bookName.equals(name)) {
                book.setBrrowed(true);
                System.out.println("借阅成功!!!");
                return;
            }
        }
        System.out.println("没有这本书!!!");
    }
}

删除操作(DelOperation)

首先,用户添加想要删除图书的信息,和借阅操作一样,遍历书架上所有书,如果有记入他的位置,再用书架上setBooks方法进行重新存放,如果书架上没有这本书直接打印没有这本书。

package ioperations;

import books.Book;
import books.BookList;

import java.util.Scanner;

public class DelOperation implements Ioperations{
    @Override
    public void work(BookList bookList) {
        int currentSized = bookList.getUseSized();
        System.out.println("请输入删除书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int pos = -1;
        for (int i = 0; i < currentSized; i++) {
            Book book = bookList.getBooks(i);
            if(book.getName().equals(name)) {
                pos = i;
            }
        }

        if(pos == -1) {
            System.out.println("没有这本书");
            return;
        }
        int i = pos;
        for (; i < currentSized-1; i++) {
            Book book = bookList.getBooks(i);
            bookList.setBooks(i,book);
        }
        bookList.setBooks(i,null);
        bookList.setUseSized(currentSized-1);
        System.out.println("删除成功!!!");
    }
}

查找操作(FindOperation)

这个操作和借阅操作十分相似,只需把借阅状态删除即可。

package ioperations;

import books.Book;
import books.BookList;

import java.util.Scanner;

public class FindOperation implements Ioperations{
    @Override
    public void work(BookList bookList) {

        int currentSized = bookList.getUseSized();
        System.out.println("请输入查询书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < currentSized; i++) {
            Book book = bookList.getBooks(i);
            String bookName = book.getName();
            if(bookName.equals(name)) {
                System.out.println(book);
                System.out.println("找到了!!!");
                return;
            }
        }
        System.out.println("没有这本书");

    }
}

归还操作(ReturnOperation)

这个操作和借阅操作也十分相似,只需将图书的借阅状态改为false和删除判断借阅状态即可。

package ioperations;

import books.Book;
import books.BookList;

import java.util.Scanner;

public class ReturnOperation implements Ioperations{
    @Override
    public void work(BookList bookList) {
        int currentSized = bookList.getUseSized();
        System.out.println("请输入归还书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < currentSized; i++) {
            Book book = bookList.getBooks(i);
            String bookName = book.getName();
            if(bookName.equals(name)) {
                if(book.isBrrowed()) {
                    book.setBrrowed(false);
                    System.out.println("归还成功!!!");
                    return;
                }
            }
        }
    }
}

展示操作(ShowOperation)

这个操作就十分简单了,只需循环遍历书架上的书,并打印即可。

package ioperations;

import books.Book;
import books.BookList;

public class ShowOperation implements Ioperations {
    @Override
    public void work(BookList bookList) {

        int currentSized = bookList.getUseSized();
        for (int i = 0; i < currentSized; i++) {
            Book book = bookList.getBooks(i);
            System.out.println(book);
        }
        System.out.println("显示完成");
    }
}

退出程序(ExitOperation)

这个操作只需调用exit函数即可。

package ioperations;

import books.BookList;

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

测试操作(Test)

这里我们设置一个登入界面,输入你的姓名,如果你是管理员,就新建一个管理员对象,如果你是普通用户,就新建一个普通用户对象,然后根据你的身份来调用你的操作菜单,并通过接口数组来进行操作。

import books.BookList;
import ioperations.Ioperations;
import users.AdminUser;
import users.NormalUser;
import users.Users;

import java.util.Scanner;


public class Test {
    public static Users login() {
        System.out.println("请输入你的姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入你的身份,1:管理员   2:普通用户");
        int chioce = scanner.nextInt();
        if(chioce == 1) {
            return new AdminUser(name);
        }else if(chioce == 2) {
            return new NormalUser(name);
        }else {
            System.out.println("输入错误");
        }
        return null;
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        Users users = login();
        while (true) {
            int choice = users.meau();
            users.doIoperations(choice,bookList);
        }
    }
}

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值