Java inquiry Book Information

Use generic collections to implement the query function of a library management systems.

In the library management system in order to facilitate management books, the book is divided into several categories. Under each category there are a lot of books, each book has a corresponding category, which has a relationship to-many mapping, that category corresponds to a multiple books.

In this case, can be used to store map Map book information and the category, which is the Category key (category) Type, List value type (books Book class category), then the output of nested loops through each corresponding category the more book information. Specific steps are as follows.

1. Create Category class represents a type of books, there are two in the class attributes: id and name, category name and number, respectively, and achieve their setXxx () and getXxx () method, as follows:

public class Category {
    private int id; // 类别编号
    private String name; // 类别名称

    public Category(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public String toString() {
        return "所属分类:" + this.name;
    }

    // 上面两个属性的setXxx()和getXxx()方法
    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

2. Create a detailed information represents BookInfo books, including five properties in the class: id, name, price, author and startTime, respectively Book Number, name, price, author and publication date, also achieved their setXxx () and getXxx () method, as follows:

public class BookInfo {
    private int id; // 编号
    private String name; // 名称
    private int price; // 价格
    private String author; // 作者
    private String startTime; // 出版时间

    public BookInfo(int id, String name, int price, String author, String startTime) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.author = author;
        this.startTime = startTime;
    }

    public String toString() {
        return this.id + "\t\t" + this.name + "\t\t" + this.price + "\t\t" + this.author + "\t\t" + this.startTime;
    }

    // 上面5个属性的 setXxx() 和 getXxx() 方法
    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getPrice() {
        return price;
    }

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

    public String getAuthor() {
        return author;
    }

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

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }
}

3. Create CategoryDao class, defined in the Map or a generic class, it is an object Category key type, the object is of type List, and define printCategoryInfo () method, and for printing books detailed category information. code show as below:

public class CategoryDao {
    // 定义泛型Map,存储图书信息
    public static Map<Category, List<BookInfo>> categoryMap = new HashMap<Category, List<BookInfo>>();

    public static void printDeptmentInfo() {
        for (Category cate : categoryMap.keySet()) {
            System.out.println("所属类别:" + cate.getName());
            List<BookInfo> books = categoryMap.get(cate);
            System.out.println("图书编号\t\t图书名称\t\t图书价格\t\t图书作者\t\t出版时间");
            for (int i = 0; i < books.size(); i++) {
                BookInfo b = books.get(i); // 获取图书
                System.out.println(b.getId() + "\t\t" + b.getName() + "\t\t" + b.getPrice() + "\t\t" + b.getAuthor()
                        + "\t\t" + b.getStartTime());
            }
            System.out.println();
        }
    }
}

4. Create a test class Test, define four and eight People Deptment objects in that class objects, and divided into 4 groups 8 People objects stored in the collection of List 4, and then four and four objects Deptment List set in accordance with - stored DeptmentDao peoplesMap class map corresponding relationship. The last call DeptmentDao class printDeptmentInfo () method to print the book categories and the corresponding information. Specific code as follows:

public class Test {
    public static void main(String[] args) {
        Category category1 = new Category(1, "数据库"); // 创建类别信息
        Category category2 = new Category(2, "程序设计"); // 创建类别信息
        Category category3 = new Category(3, "平面设计"); // 创建类别信息
        BookInfo book1 = new BookInfo(1, "细说 Java 编程", 25, "张晓玲", "2012-01-01"); // 创建图书信息
        BookInfo book2 = new BookInfo(2, "影视后期处理宝典", 78, "刘芳", "2012-10-05"); // 创建图书信息
        BookInfo book3 = new BookInfo(3, "MySQL 从入门到精通", 41, "王志亮", "2012-3-2"); // 创建图书信息
        BookInfo book4 = new BookInfo(4, "Java 从入门到精通", 27, "陈奚静", "2012-11-01"); // 创建图书信息
        BookInfo book5 = new BookInfo(5, "SQL Server 一百例", 68, "张晓玲", "2012-01-01"); // 创建图书信息
        List<BookInfo> pList1 = new ArrayList<BookInfo>(); // 向类别 1 添加图书
        pList1.add(book1);
        pList1.add(book4);
        List<BookInfo> pList2 = new ArrayList<BookInfo>(); // 向类别 2 添加图书
        pList2.add(book3);
        pList2.add(book5);
        List<BookInfo> pList3 = new ArrayList<BookInfo>(); // 向类别 3 添加图书
        pList3.add(book2);
        CategoryDao.categoryMap.put(category1, pList1);
        CategoryDao.categoryMap.put(category2, pList2);
        CategoryDao.categoryMap.put(category3, pList3);
        CategoryDao.printDeptmentInfo();
    }
}

In the program, using the information in the book detail the generic and generic Map List Book store category and specific categories . It can be seen that the use of generics not only reduces the amount of code you write, but also improve the security type.

Run the program outputs the result shown below.

所属类别:平面设计
图书编号  图书名称  图书价格  图书作者  出版时间
2  影视后期处理宝典  78  刘芳  2012-10-05

所属类别:数据库
图书编号  图书名称  图书价格  图书作者  出版时间
1  细说 Java 编程  25  张晓玲  2012-01-01
4  Java 从入门到精通  27  陈奚静  2012-11-01

所属类别:程序设计
图书编号  图书名称  图书价格  图书作者  出版时间
3  MySQL 从入门到精通  41  王志亮  2012-3-2
5  SQL Server 一百例  68  张晓玲  2012-01-01
Published 457 original articles · won praise 94 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_45743799/article/details/104716899