首页 > 其他分享 >运用ArrayList编写简单的图书管理系统

运用ArrayList编写简单的图书管理系统

时间:2022-12-28 20:00:29浏览次数:70  
标签:String 管理系统 ArrayList System public println id 图书 out

图书管理系统

程序主入口
public class SystemEntry {

    public static void main(String[] args) {
        BookOperator operator = new BookOperator();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("--------欢迎来到图书管理系统--------");
            System.out.println("1 添加图书");
            System.out.println("2 删除图书");
            System.out.println("3 修改图书");
            System.out.println("4 查看图书");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    operator.addBook();
                    break;
                case "2":
                    operator.delBook();
                    break;
                case "3":
                    operator.updeteBook();
                    break;
                case "4":
                    operator.findAllBooks();
                    break;
                case "5":
                    System.out.println("欢迎下次再来");
                    System.exit(0);//退出
                default:
                    System.out.println("输入有误!!");
                    break;
            }
        }
    }
}
封装图书数据的类
public class Book {
   private String id;
   private String name;
   private double price;
   private String description;

    public Book() {
    }

    public Book(String id, String name, double price, String description) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.description = description;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

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

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", description='" + description + '\'' +
                '}';
    }
}
操作图书的功能类
public class BookOperator {

    private ArrayList<Book> books = new ArrayList<>();
    private Scanner sc = new Scanner(System.in);

    public void addBook() {
        String id = null;
        while (true) {
            System.out.println("请输入图书id");
            id = sc.next();
            int index = findIndexById(id);
            if (index < 0) {
                break;
            } else {
                System.out.println("输入的图书id重复,请重新输入!!");
            }
        }

        System.out.println("请输入书名");
        String name = sc.next();
        System.out.println("请输入价格");
        double price = sc.nextDouble();
        System.out.println("请输入图书描述");
        String description = sc.next();
        Book book = new Book(id, name, price, description);
        books.add(book);
        System.out.println("添加成功!!");
    }

    public void delBook() {
        System.out.println("请输入图书id");
        String id = sc.next();
        int index = findIndexById(id);
        if (index < 0) {
            System.out.println("删除的图书不存在,请重新输入");
        } else {
            books.remove(index);
            System.out.println("删除成功!!");
        }
    }


    public void updeteBook() {
        System.out.println("请输入图书id");
        String id = sc.next();
        int index = findIndexById(id);
        if (index < 0) {
            System.out.println("更新的图书不存在,请重新输入");
        } else {
            System.out.println("请输入书名");
            String name = sc.next();
            System.out.println("请输入价格");
            double price = sc.nextDouble();
            System.out.println("请输入图书描述");
            String description = sc.next();
            Book book = new Book(id, name, price, description);
            books.set(index, book);
            System.out.println("更新成功!!");
        }
    }

    private int findIndexById(String id) {
        for (int i = 0; i < books.size(); i++) {
            if (id.equals(books.get(i).getId())) {
                return i;
            }
        }
        return -1;
    }


    public void findAllBooks() {
        if (books == null || books.size() == 0) {
            System.out.println("图书为空,请先添加");
            return;
        }
        System.out.println("图书id\t书名\t价格\t描述");
        for (int i = 0; i < books.size(); i++) {
            Book book = books.get(i);
            System.out.println(book.getId() + "\t" + book.getName() + "\t" +
                    book.getPrice() + "元\t" + book.getDescription());
        }
    }
}

标签:String,管理系统,ArrayList,System,public,println,id,图书,out
From: https://www.cnblogs.com/OKGOsky/p/17011159.html

相关文章

  • 公租房管理系统,新增租赁住房传递民生温度
    版权声明:本文章由“厦门多米克信息技术有限公司”编辑组汇编而成,未经授权和许可,任何个人或媒体不得对本网站的文章及其他信息资料予以复制、转载、抄袭、改编。厦门多米克......
  • 廉租房管理系统,优秀的长租公寓对租赁住房市场的正向引导作用
    版权声明:本文章由“厦门多米克信息技术有限公司”编辑组汇编而成,未经授权和许可,任何个人或媒体不得对本网站的文章及其他信息资料予以复制、转载、抄袭、改编。厦门多米克......
  • C++工资管理系统
    C++工资管理系统参考题目3:工资管理系统该系统在磁盘上存储了某单位全体职工的工资信息。对于每位职工存储以下信息:职工编号、基本工资、津贴、岗贴、补贴、应发数、个......
  • JAVA代码审计_若依管理系统
    若依管理系统代码审计该项目使用Mybatis作为数据库持久化框架。全局搜索${、关注xxxmapper.xml文件寻找SQL注入。1)后台角色管理处SQL注入漏洞点“SysRoleMapper.x......
  • Vue:TDesign Starter 定制改造,中后台管理系统(前端)
    目前来说,TDesignStarter和普通的业务代码一样,没法做到像npm包一样通过升级来达到同步新增功能或修复问题的效果,所以这里记录一下TDesignStarter的定制改造过程。0.......
  • 基于springboot+mybatis+mysql+html实现校园宿舍管理系统
    @目录一、系统简介二、系统主要功能界面三、其它系统四、源码下载一、系统简介本系统功能模块主要分为:信息浏览浏览功能、宿舍打卡浏览功能、学生提交信息功能、宿舍搜索......
  • ArrayList-ConcurrentModificationException异常分析记录
    ConcurrentModificationException记录与分析publicstaticvoidmain(String[]args){ArrayList<Integer>list=newArrayList<>();list.add(1);Itera......
  • 学生成绩信息管理系统
    学生管理类的设计与实现设计一个学生信息管理类StudentList用于实现对所有学生信息的管理,如学生信息的添加、删除、修改和显示等功能。 ......
  • 【学生管理系统】权限管理之角色管理
    目录​​6.3角色管理​​​​6.3.1查询所有角色​​​​6.3.2核心2:给角色授予权限(菜单)​​​​6.3.3添加角色​​6.3角色管理6.3.1查询所有角色1)后端【已有】2)前端......
  • 职工管理系统
    1、管理系统需求管理公司所有员工的信息基于C++和多态进行实现公司员工分为三类:老板普工经理显示信息:职工编号姓名岗位职责普工职责:完成经理交代的任务经理职责......