当前位置 : 主页 > 编程语言 > java >

Java顺序表实现图书管理系统

来源:互联网 收集:自由互联 发布时间:2021-12-01
本文实例为大家分享了Java顺序表实现图书管理系统的具体代码,供大家参考,具体内容如下 一、简介 实现此项目的目的是巩固并理解前面的知识点:类,抽象类,封装,继承,多态,接

本文实例为大家分享了Java顺序表实现图书管理系统的具体代码,供大家参考,具体内容如下

一、简介

实现此项目的目的是巩固并理解前面的知识点:类,抽象类,封装,继承,多态,接口等

二、核心需求

管理端

  查阅书籍
  增加书籍
  删除书籍
  打印书籍列表
  退出系统

用户端

  查询书籍
  借阅书籍
  归还书籍
  打印书籍列表
  退出系统

三、类的设计

1. 创建图书类

图书类中包含图书的名称,价格,类型,作者和是否被借出等信息,并生成构造方法,Getter()和Setter()方法,toString方法(注意成员变量应该尽可能使用private关键字修饰)

public class Book {
    private String name;
    private double price;
    private String type;
    private String author;
    private boolean isBorrowed;

    public Book(String name, double price, String type, String author) {
        this.name = name;
        this.price = price;
        this.type = type;
        this.author = author;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", author='" + author + '\'' +
                ", 状态:" +((isBorrowed) ? "已借出":"未借出")+
                '}';
    }
}

2. 创建图书列表类

图书列表类用于存放图书,我们可以先在列表中初始化几本书以方便后续测试

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

    private int usedSize;
    public BookList(){
        books[0] = new Book("三国演义",19,"小说","罗贯中");
        books[1] = new Book("水浒传",29,"小说","施耐庵");
        books[2] = new Book("西游记",39,"小说","吴承恩");
        usedSize = 3;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

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

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

3. 创建用户类

创建一个用户类并将其定义为抽象类,再创建普通用户类和管理员类继承于用户类:

创建用户类并定义为抽象类:

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

创建管理员用户类:

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
            new ExitOperation(),
            new FindOperation(),
            new AddOperation(),
            new DisplayOperation(),
            new DelOperation()
        };
    }

    @Override
    public int menu(){
        System.out.println("===========管理员菜单============");
        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 scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

创建普通用户类:

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new DisplayOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation(),
        };
    }

    @Override
    public int menu(){
        System.out.println("===========普通用户菜单============");
        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 scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

4. 创建操作相关的类

首先创建一个接口用于实现多态:

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

创建添加书籍类:

public class AddOperation implements IOperation{
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入图书名称:");
        String name = scanner.nextLine();
        System.out.println("请输入价格:");
        double price = scanner.nextDouble();
        System.out.println("请输入类型:");
        String type = scanner.next();
        System.out.println("请输入作者:");
        String author = scanner.next();
        Book book = new Book(name,price,type,author);
        int usedSize = bookList.getUsedSize();
        bookList.setBook(usedSize,book);
        bookList.setUsedSize(++usedSize);
        System.out.println("添加图书成功!");
    }
}

创建查找书籍类:

public class FindOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("请输入书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        for(int i=0;i<bookList.getUsedSize();i++){
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())){
                System.out.println(book);
                return;
            }
        }
        System.out.println("找不到 《"+name+"》 这本书");
    }
}

创建借阅书籍类:

public class BorrowOperation implements IOperation {
    public void work(BookList bookList) {
        System.out.println("请输入你要借阅的书籍:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        int i = 0;
        for (i = 0; i < bookList.getUsedSize() - 1; i++) {
            Book book = bookList.getBook(i);
            if (name.equals(book.getName()) && !book.isBorrowed()) {
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
            if (name.equals(book.getName()) && book.isBorrowed()) {
                System.out.println("该书籍已被借出");
                return;
            }
        }
        System.out.println("找不到你要借阅的书籍!");
    }
}

创建归还书籍类:

public class ReturnOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("请输入你要归还的书籍:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        int i=0;
        for(i=0;i<bookList.getUsedSize()-1;i++){
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())&& book.isBorrowed()){
                book.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            }
            if(name.equals(book.getName())&& !book.isBorrowed()){
                System.out.println("此书处于未借出状态!");
                return;
            }
        }
        System.out.println("找不到你要归还的书籍!");
    }
}

创建删除书籍类:

public class DelOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("请输入要删除的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        int index = 0;
        int i = 0;
        for(i=0;i<bookList.getUsedSize();i++){
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())){
                index = i;
                break;
            }
        }
        if(i>=bookList.getUsedSize()) {
            System.out.println("找不到这本书");
            return;
        }
        int j = 0;
        for (j = index;j< bookList.getUsedSize()-1;j++){
            Book book = bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
        bookList.setBook(bookList.getUsedSize()-1, null);
        bookList.setUsedSize(bookList.getUsedSize()-1);
        System.out.println("删除成功!");
    }
}

创建打印书籍列表类:

public class DisplayOperation implements IOperation{
    public void work(BookList bookList){
        int usedSize = bookList.getUsedSize();
        for (int i=0;i<usedSize;i++){
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

退出系统类:

public class ExitOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("退出系统!");
        System.exit(0);
    }
}

主函数类:

public class Main {
    public static User work(){
        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);
        }
            return new NormalUser(name);
    }

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

The end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

上一篇:Spring Data JPA 实体类中常用注解说明
下一篇:没有了
网友评论