Java_19_图书管理系统项目

图书管理系统

  1. 业务需求分析:
    (1)查看全部书籍。query
    (2)添加书本信息。add
    (3)删除书本信息。delete
    (4)修改书本信息。update
    (5)退出系统。 exit

  2. 书本信息的结构:
    类型 书名 价格 作者
    ---------------------------------------------------
    言情小说
    《金瓶梅》 99.9 阿猫
    《红楼梦》 198.2 曹雪芹
    武侠小说
    《三少爷的剑》 98.2 古龙
    《神雕侠侣》 98.2 金庸
    ------------------------------------------------------

  3. 分析:
    (1)定义一个书本类。
    (2)定义一个集合表示图书馆用于存储书本信息数据:Map
    (3)展示操作界面。
    (4)完成相应功能。

功能:主要是让Map中的键作为橱柜,值作为书本信息
即定义的Map为Map> BOOK_STORE = new HashMap<>();
BOOK_STORE是书本信息,单独的一个类,如下:

package _Library;

public class Book {
  private String name;
  private double price;
  private String author;


    public Book() {
    }

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

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return price
     */
    public double getPrice() {
        return price;
    }

    /**
     * 设置
     * @param price
     */
    public void setPrice(double price) {
        this.price = price;
    }

    /**
     * 获取
     * @return author
     */
    public String getAuthor() {
        return author;
    }

    /**
     * 设置
     * @param author
     */
    public void setAuthor(String author) {
        this.author = author;
    }
}

主函数代码如下:

package _Library;

import java.util.*;

public class Main {

    public static final Map<String,List<Book>> BOOK_STORE = new HashMap<>();
    public static final Scanner S_SCANNER = new Scanner(System.in);

    public static void main(String[] args){

        showCommend();
    }
    private static void showCommend(){
        System.out.println("===============欢迎您进入系统===================");
        System.out.println("(1)查看全部书籍。query");
        System.out.println("(2)添加书本信息。add");
        System.out.println("(3)删除书本信息。delete");
        System.out.println("(4)修改书本信息。update");
        System.out.println("(5)退出系统。 exit");
        System.out.print("请您输入您的操作命令:");
        String commend = S_SCANNER.nextLine();

        switch (commend){
            case "query":
                queryBooks();
                break;
            case "add":
                addBook();
                break;
            case "delete":
                deleteBook();
                break;
            case "update":
                updateBook();
                break;
            case "exit":
                System.out.println("退出系统");
                System.exit(0);
                break;
            default:
                System.out.println("输入有误,请重新输入!");
        }
        showCommend();
    }
    //修改书本信息
    public static void updateBook(){
        queryBooks();
        System.out.println("===============欢迎您进入修改书本业务=================");

        System.out.println("请您输入书本的栏目:");
        String type = S_SCANNER.nextLine();
        while(!BOOK_STORE.containsKey(type)){
            System.err.println("您输入的橱柜不存在,继续请输入yes,按任意键退出!");
            String isYes = S_SCANNER.nextLine();
            if(Objects.equals(isYes, "yes")) {
                System.out.println("请输入您的栏目:");
                type = S_SCANNER.nextLine();
            } else break;
        }
        if(!BOOK_STORE.containsKey(type)) return;
        System.out.println("请您输入书名:");
        String name = S_SCANNER.nextLine();
        Book book = getBookByTypeAndName(type,name);
        while(book == null){
            System.err.println("您输入的书名有误!输入yes继续修改,按任意键退出!");
            String isYes = S_SCANNER.nextLine();
            if(Objects.equals(isYes, "yes")){
                System.out.println("请输入您的书名:");
                name = S_SCANNER.nextLine();
                book = getBookByTypeAndName(type,name);
            }else break;
        }
        if(book == null) return;
        System.out.println("确定新书名:");
        String newName = S_SCANNER.nextLine();
        System.out.println("确定新价格:");
        String newPrice = S_SCANNER.nextLine();
        System.out.println("确定新作者:");
        String newAuthor = S_SCANNER.nextLine();
        book.setName(newName);
        book.setPrice(Double.valueOf(newPrice));
        book.setAuthor(newAuthor);
        queryBooks();
        System.out.println("修改成功!");
    }
    //在某个栏目下,根据书名查询出这本书对象返回
    public static Book getBookByTypeAndName(String type , String name){
        List<Book> books = BOOK_STORE.get(type);
        for(Book book : books)
            if(book.getName().equals(name)) return book;
        return null;
    }
    //查询全部书本信息
    private static void queryBooks(){
        System.out.println("===============欢迎您进入查询书本业务=================");
        if(BOOK_STORE.size() == 0) System.out.println("您的书柜没有书");
        else {
            System.out.println("类型\t\t\t\t书名\t\t\t\t\t价格\t\t\t作者");

            BOOK_STORE.forEach((type, books) -> {
                System.out.println(type);
                for (Book book : books)
                    System.out.println("\t\t\t\t" + book.getName() + "\t\t\t" + book.getPrice() + "\t\t" + book.getAuthor());
            });
        }
    }

    //添加书本信息数据
    private static void addBook(){
        System.out.println("===============欢迎您进入添加书本业务=================");
        System.out.println("请输入书本的栏目:");
        String type = S_SCANNER.nextLine(); //type是栏目
        //1.判断是否存在栏目
        List<Book> books = null;
        if(BOOK_STORE.containsKey(type)){
            //2.存在栏目  直接得到橱柜对象
            books = BOOK_STORE.get(type);

        }else{
            //3.栏目是第一次被添加
            books = new ArrayList<>();
            BOOK_STORE.put(type,books);
        }
        System.out.println("输入书本名称:");
        String name = S_SCANNER.nextLine();
        System.out.println("输入书本价格:");
        String price = S_SCANNER.nextLine();
        System.out.println("输入书本作者:");
        String author = S_SCANNER.nextLine();

        Book book = new Book(name,Double.parseDouble(price),author);
        books.add(book);
        System.out.println("在" + type + "下的书本添加成功");
    }
    private static void deleteBook(){
        System.out.println("===============欢迎您进入删除书本业务=================");
        queryBooks();
        System.out.println("请您输入书本的栏目:");
        String type = S_SCANNER.nextLine();
        while(!BOOK_STORE.containsKey(type)){
            System.err.println("您输入的橱柜不存在,继续请输入yes,按任意键退出!");
            String isYes = S_SCANNER.nextLine();
            if(Objects.equals(isYes, "yes")) {
                System.out.println("请输入您的栏目:");
                type = S_SCANNER.nextLine();
            } else break;
        }
        if(!BOOK_STORE.containsKey(type)) return;
        System.out.println("请您输入书名:");
        String name = S_SCANNER.nextLine();
        Book book = getBookByTypeAndName(type,name);
        while(book == null){
            System.err.println("您输入的书名有误!输入yes继续修改,按任意键退出!");
            String isYes = S_SCANNER.nextLine();
            if(Objects.equals(isYes, "yes")){
                System.out.println("请输入您的书名:");
                name = S_SCANNER.nextLine();
                book = getBookByTypeAndName(type,name);
            }else break;
        }
        if(book == null) return;
        List<Book> books = BOOK_STORE.get(type);
        books.remove(book);

    }
}

你可能感兴趣的:(Java基础教程,java,开发语言)