1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| import java.util.Map; import java.util.List; import java.util.HashMap; import java.util.ArrayList;
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; }
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; } }
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 + "|" + this.name + "|" + this.price + "|" + this.author + "|" + this.startTime; }
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; } }
class CategoryDao { 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("图书编号|图书名称|图书价格|图书作者|出版时间"); for (int i = 0; i < books.size(); i++) { BookInfo b = books.get(i); System.out.println(b.getId() + "\t" + b.getName() + " " + b.getPrice() + "\t" + b.getAuthor() + "\t" + b.getStartTime()); } System.out.println(); } } }
public class Test128 { public static void main(String[] args) { Category category1 = new Category(1, "文学作品"); Category category2 = new Category(2, "程序设计");
BookInfo book1 = new BookInfo(1, "边城风云", 45, "沈从文", "1934-05-01"); BookInfo book2 = new BookInfo(2, "平凡世界", 50, "路 遥", "1986-01-01"); BookInfo book3 = new BookInfo(3, "围城故事", 42, "钱钟书", "1947-09-01"); BookInfo book4 = new BookInfo(4, "算法导论", 75, "韩家炜", "2006-08-01"); BookInfo book5 = new BookInfo(5, "代码大全", 65, "陈 皓", "2004-06-01"); BookInfo book6 = new BookInfo(6, "设计模式", 60, "李 刚", "2005-12-01");
List<BookInfo> pList1 = new ArrayList<BookInfo>(); pList1.add(book1); pList1.add(book2); pList1.add(book3);
List<BookInfo> pList2 = new ArrayList<BookInfo>(); pList2.add(book4); pList2.add(book5); pList2.add(book6);
CategoryDao.categoryMap.put(category1, pList1); CategoryDao.categoryMap.put(category2, pList2); CategoryDao.printDeptmentInfo(); } }
|