文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2024-04-02 19:55

关注

本文实例为大家分享了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

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

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯