第九周
package com.dmk; public class Book { private String id; private String name; private double price; public Book() { } public Book(String id, String name, double price) { this.id = id; this.name = name; this.price = price; } 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; } }
package com.dmk; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Test { static List<Book> lbs = new ArrayList<Book>(); static { Book b1 = new Book("1001", "西游记", 50.9); lbs.add(b1); Book b2 = new Book("1002", "红楼梦", 49.9); lbs.add(b2); Book b3 = new Book("1003", "水浒传", 65.9); lbs.add(b3); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub mnue(); } public static void mnue() { for (;;) { 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("* 0、退出程序 *"); System.out.println("***********************"); int i = inputInt("请输入要选择的操作:"); switch (i) { case 1: addBook(); break; case 2: delBook(); break; case 3: updateBook(); break; case 4: findAllBook(); break; case 5: findBookByName(); break; case 0: return; default: System.out.println("输入错误,请重新输入"); break; } } } private static void findBookByName() { String name = inputStr("请输入要查找的图书名称"); int index = 0; System.out.println("id\t图书名称\t价格"); for (Book book : lbs) { if (name.equals(book.getName())) { System.out.println(book.getId() + "\t" + book.getName() + "\t" + book.getPrice()); } index++; } } private static void findAllBook() { System.out.println("id\t图书名称\t价格"); for (Book book : lbs) { System.out.println(book.getId() + "\t" + book.getName() + "\t" + book.getPrice()); } } private static void updateBook() { String id = inputStr("请输入要修改的图书id"); int index = 0; for (Book book : lbs) { if (id.equals(book.getId())) { book.setName(inputStr("请输入修改后的名称:")); book.setPrice(inputDouble("请输入修改后的价格:")); lbs.remove(index); lbs.add(index, book); } index++; } } private static void delBook() { String id = inputStr("请输入要删除的图书id"); int index = 0; for (Book book : lbs) { if (id.equals(book.getId())) { lbs.remove(index); } index++; } } private static void addBook() { String id = inputStr("请输入图书id"); String name = inputStr("请输入图书名称"); double price = inputDouble("请输入图书价格"); Book book = new Book(id, name, price); lbs.add(book); } public static String inputStr(String msg) { System.out.print(msg); return new Scanner(System.in).nextLine(); } public static int inputInt(String msg) { System.out.print(msg); return new Scanner(System.in).nextInt(); } public static double inputDouble(String msg) { System.out.print(msg); return new Scanner(System.in).nextDouble(); } }
2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上
<%@page import="com.lwk.demo5.Book"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <% List<Book> lbs = new ArrayList<Book>(); lbs.add(new Book("10001", "西游记", 50.8)); lbs.add(new Book("10002", "红楼梦", 59.6)); lbs.add(new Book("10003", "三国演义", 65.5)); lbs.add(new Book("10004", "水浒传", 49.9)); %> <table> <tr> <th>图书编号</th> <th>图书名称</th> <th>图书价格</th> </tr> <% for (int i = 0; i < lbs.size(); i++) { %> <tr> <td><%=lbs.get(i).getId()%></td> <td><%=lbs.get(i).getName()%></td> <td><%=lbs.get(i).getPrice()%></td> </tr> <% } %> </table> </body> </html>