简易版图书管理系统

图书管理系统分析: 1.定义Book类 2.完成主界面和选择 3.完成查询所有图书 4.完成添加图书 5. 修改图书 6.完成修改图书 

1.定义Book类

public class demo01 {
    public static void main(String[] args) {
        ArrayList<Book> mz = new ArrayList<>();
        mz.add(new Book("西游记", 19));
        mz.add(new Book("红楼梦", 33.99));

        ArrayList<Book> it = new ArrayList<>();
        it.add(new Book("java入门到放弃", 88.8));
        it.add(new Book("php入门到放弃", 88.8));
        Map<String, ArrayList<Book>> bookMap = new HashMap<>();
        bookMap.put("名著", mz);
        bookMap.put("it书籍", it);
        while (true) {
            System.out.println("--------欢迎来到图书管理系统--------");
            // 1.2.输出功能选项
            System.out.println("1.查看书籍");
            System.out.println("2.添加书籍");
            System.out.println("3.删除书籍");
            System.out.println("4.修改书籍");
            System.out.println("5.退出");
            // 1.3.提示用户输入
            Scanner sc = new Scanner(System.in);
            int operation = sc.nextInt();
            switch (operation) {
                case 1:
                    showBooks(bookMap);
                    break;
                case 2:
                    addBook(bookMap);
                    break;
                case 3:
                    deleteBook(bookMap);
                    break;
                case 4:
                    editBook(bookMap);
                    break;
                case 5:
                    System.out.println("大爷慢走,欢迎下次再来");
                    System.exit(0);
                    break;
                default:
                    System.out.println("大爷您输入的不存在,请重新输入:");
                    break;
            }
        }
    }

    //完成删除图书
    private static void deleteBook(Map<String, ArrayList<Book>> bookMap) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的书籍类型:");
        String type = sc.next();
        System.out.println("请输入要删除的书名:");
        String name = sc.next();
        ArrayList<Book> books = bookMap.get(type);
        if (books == null) {
            System.out.println("没有" + type + "这种类型的书籍");
            return;
        }
        for (int i = 0; i < books.size(); i++) {
            Book book = books.get(i);
            if (book.getName().equals(name)) {
                books.remove(i);
            }
            System.out.println("恭喜您,删除" + name + "成功");
            return;
        }
    }

    //完成修改图书
    private static void editBook(Map<String, ArrayList<Book>> bookMap) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的书籍类型:");
        String type = sc.next();
        System.out.println("请输入要修改的书名:");
        String name = sc.next();
        System.out.println("请输入要新的的书籍名称:");
        String newname = sc.next();
        System.out.println("请输入要新的的书籍新的价格:");
        double newprice = sc.nextDouble();

        ArrayList<Book> books = bookMap.get(type);
        if (books == null) {
            System.out.println("您输入的类型不存在!");
            return;
        }
        for (Book book : books) {
            if (book.getName().equals(name)) {
                book.setName(newname);
                book.setPrice(newprice);
                System.out.println("恭喜您,修改" + name + "书籍成功");
                return;
            }
        }
        System.out.println("您输入的书籍不存在!");
    }

    //完成添加图书
    private static void addBook(Map<String, ArrayList<Book>> bookMap) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要添加的书籍类型:");
        String type = sc.next();
        System.out.println("请输入要添加的书名:");
        String name = sc.next();
        System.out.println("请输入要添加的书的价格:");
        double price = sc.nextDouble();
        Book newbook = new Book(name, price);
        ArrayList<Book> books = new ArrayList<>();
        bookMap.put(type, books);
        if (books == null) {
            books = new ArrayList<>();
            bookMap.put(type, books);
        }
        boolean exists = false;
        for (Book book : books) {
            if (book.getName().equals(name)) {
                exists = true;
            }
        }
        if (exists) {
            System.out.println(name + "书籍存在");
        } else {
            books.add(newbook);
            System.out.println("添加" + name + "成功");
        }
    }

    // 完成查询所有图书
    private static void showBooks(Map<String, ArrayList<Book>> bookMap) {
        System.out.println("类名\t书名\t价格");
        Set<Map.Entry<String, ArrayList<Book>>> entrySet = bookMap.entrySet();
        for (Map.Entry<String, ArrayList<Book>> entry : entrySet) {
            String key = entry.getKey();
            System.out.println(key);

            ArrayList<Book> value = entry.getValue();
            for (Book book : value) {
                System.out.println("\t\t" + book.getName() + "\t" + book.getPrice());
            }
        }
    }
}

2.测试类并完成各项功能

/**
 * created by sheting on
 * User: hl.c
 * Date:2019/3/15
 * Time: 22:00
 */
public class Book {
    private String name;
    private double price;


    public Book() {
    }

    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;
    }
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';

    }


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

运行效果:

这系统很简易啊,有什么意见留言哦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值