JSP第九次作业
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
package demo2; import java.util.ArrayList; import java.util.Scanner; import java.util.List; public class Text { static List<Book> lbs = new ArrayList<Book>(); /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub creatBookList(); mnue(); } public static void creatBookList() { Book b1 = new Book("1", "Book1",20); lbs.add(b1); Book b2 = new Book("2", "Book2",20); lbs.add(b2); Book b3 = new Book("3", "Book3",20); lbs.add(b3); } 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 i = 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()); } i++; } } 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(); } }
package demo2; import java.io.Serializable; public class Book { private String id; private String name; private double price; public Book(){ } 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 d) { this.price = d; } public Book(String id, String name, double price) { super(); this.id = id; this.name = name; this.price = price; } }
2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。
<%@page import="java.util.ArrayList"%> <%@page import="demo2.Book"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% List<Book> lbs = new ArrayList<Book>(); lbs.add(new Book("1", "西游记", 20)); lbs.add(new Book("2", "红楼梦", 20)); lbs.add(new Book("3", "三国演义", 20)); lbs.add(new Book("4", "水浒传", 20)); %> <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>
3.在MySQL中创建Book表,里面id,name,price,
用命令实现,
添加一个图书,
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书,
把20元以上的图书价格都修改为18.8,
查看全部图书,
查看价格高于10块钱的全部图书
CREATE TABLE `book` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `price` decimal(10, 2) NOT NULL, PRIMARY KEY (`id`) USING BTREE ); INSERT INTO `book` VALUES (1, '水浒传', 50.90); INSERT INTO `book` VALUES (2, '红楼梦', 40.50); INSERT INTO `book` VALUES (3, '西游记', 45.90); INSERT INTO `book` VALUES (4, '我的第一本书', 20.00); UPDATE `book` SET `price`=18.8 WHERE `price`>20; SELECT * FROM `book`; SELECT * FROM `book` WHERE `price`>10; DELETE FROM `book` WHERE `name` LIKE '我%'; DELETE FROM `book`;