图书管理系统(Java)

我们运用方法、继承、接口、多态以及包等相关知识,编写一个图书管理系统。
首先,我们建立一个TestMain主方法,进行登录操作:

public class TestMain {

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

根据用户选择,我们分别设置了管理者和普通用户两个界面,实现对操作系统的区分:
建立User包里的User文件

public abstract class User {
    protected String name;
    protected IOperation[] operations;

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

    public abstract int menu();

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

对User进行划分,分为管理者Admin和普通用户NormalUser

public class Admin extends User {

    public Admin(String name) {
        super(name);
        this.operations = new IOperation[] {
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new DisplayOperation()
        };
    }

    @Override
    public int menu() {
        System.out.println("=======================");
        System.out.println("hello " + this.name + "欢迎来到图书管理系统");
        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 choice = scanner.nextInt();

        return choice;
    }
}

public class NormalUser extends User {

    public NormalUser (String name) {
        super(name);
        this.operations = new IOperation[] {
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()
        };
    }

    @Override
    public int menu() {
        System.out.println("=======================");
        System.out.println("hello " + this.name + "欢迎来到图书管理系统");
        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 choice = scanner.nextInt();

        return choice;
    }
}

当登录页面设计完成后,我们考虑建立一个图书库,首先构造一本书里面所包含的属性,在这,我设计了包括书名,作者,价格,类型以及是否借出五个属性

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=" + isBorrowed +
                '}';
    }
}

然后为了操作方便,我现在图书库里放置几本书

public class BookList {
    private Book[] books = new Book[100];
    private int usedSize = 0;

    public BookList() {
        books[0] = new Book("三国演义","罗贯中",58,"文学小说");
        books[1] = new Book("水浒传","施耐庵",48,"文学小说");
        books[2] = new Book("西游记","吴承恩",68,"文学小说");
        books[3] = new Book("红楼梦","曹雪芹",58,"文学小说");

        this.usedSize = 4;
    }

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

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

    public int getUsedSize() {
        return usedSize;
    }

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

接下来,我们就要对该图书管理系统的相关操作进行实现,在这里我设计了查找、退出公共属性,以及属于管理者具有的增加、删除、打印书单,和属于普通用户具有的借阅、归还的属性,依次进行实现:

//设置一个接口(为了熟练接口操作)
public interface IOperation {
    void work(BookList bookList);
}
 //添加图书
public class AddOperation implements IOperation {
    @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.next();

        Book book = new Book(name,author,price,type);
        int curSize = bookList.getUsedSize();
        bookList.setBooks(curSize,book);
        bookList.setUsedSize(curSize+1);

        System.out.println("成功添加!");

    }
}
 // 借阅图书
public class BorrowOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入图书的名字:");
        String name = scanner.nextLine();

        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(name)) {
                //查询是否有所借图书
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
        }
        System.out.println("没有所借图书");
    }
}
 // 删除图书
public class DelOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要删除图书的名字:");
        String name = scanner.nextLine();

        int i = 0;
        for (i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(name)) {
                //查询是否有该图书
                break;
            }
        }
        if (i == bookList.getUsedSize()) {
            System.out.println("没有这本书!");
            return;
        }

        //删除
        for (int pos = 0; pos < bookList.getUsedSize() - 1; pos++) {
            Book book = bookList.getBooks(pos + 1);
            bookList.setBooks(pos, book);
        }
        bookList.setUsedSize(bookList.getUsedSize() - 1);
        System.out.println("删除成功!");
    }
}
 // 打印书单
public class DisplayOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("打印书单");

        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getBooks(i);
            System.out.println(book);
        }
    }
}

// 退出系统
public class ExitOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        System.exit(1);
    }
}
// 查找图书
public class FindOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入图书的名字:");
        String name = scanner.nextLine();

        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(name)) {
                System.out.println(book);
                System.out.println("查找成功!");
                return;
            }
        }
    }
}

 // 归还图书
public class ReturnOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入归还图书的名字:");
        String name = scanner.nextLine();

        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(name)) {
                book.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            }
        }
    }
}

最后,我们就成功实现了一个图书管理系统。
运行结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值