JAVA9
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
1 package gxlbao ; 2 3 public class Book { 4 private String id; 5 private String name; 6 private double price; 7 8 public Book() { 9 10 } 11 12 public Book(String id, String name, double price) { 13 this.id = id; 14 this.name = name; 15 this.price = price; 16 } 17 18 public String getId() { 19 return id; 20 } 21 22 public void setId(String id) { 23 this.id = id; 24 } 25 26 public String getName() { 27 return name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 public double getPrice() { 35 return price; 36 } 37 38 public void setPrice(double price) { 39 this.price = price; 40 } 41 42 }
1 package gxlbao; 2 import java.util.ArrayList; 3 import java.util.List; 4 import java.util.Scanner; 5 public class ONE { 6 static List<Book> lbs = new ArrayList<Book>(); 7 8 static { 9 Book b1 = new Book("1001", "西游记", 50.9); 10 lbs.add(b1); 11 Book b2 = new Book("1002", "红楼梦", 49.9); 12 lbs.add(b2); 13 Book b3 = new Book("1003", "水浒传", 65.9); 14 lbs.add(b3); 15 } 16 17 /** 18 * @param args 19 */ 20 public static void main(String[] args) { 21 // TODO Auto-generated method stub 22 mnue(); 23 } 24 25 public static void mnue() { 26 for (;;) { 27 System.out.println("***********************"); 28 System.out.println("* 1、添加图书 *"); 29 System.out.println("* 2、删除图书 *"); 30 System.out.println("* 3、修改图书 *"); 31 System.out.println("* 4、查询所有图书 *"); 32 System.out.println("* 5、查询一本图书 *"); 33 System.out.println("* 0、退出程序 *"); 34 System.out.println("***********************"); 35 int i = inputInt("请输入要选择的操作:"); 36 switch (i) { 37 case 1: 38 addBook(); 39 break; 40 case 2: 41 delBook(); 42 break; 43 case 3: 44 updateBook(); 45 break; 46 case 4: 47 findAllBook(); 48 break; 49 case 5: 50 findBookByName(); 51 break; 52 case 0: 53 return; 54 default: 55 System.out.println("输入错误,请重新输入"); 56 break; 57 } 58 } 59 } 60 61 private static void findBookByName() { 62 String name = inputStr("请输入要查找的图书名称"); 63 int index = 0; 64 System.out.println("id\t图书名称\t价格"); 65 for (Book book : lbs) { 66 if (name.equals(book.getName())) { 67 System.out.println(book.getId() + "\t" + book.getName() + "\t" 68 + book.getPrice()); 69 } 70 index++; 71 } 72 } 73 74 private static void findAllBook() { 75 System.out.println("id\t图书名称\t价格"); 76 for (Book book : lbs) { 77 System.out.println(book.getId() + "\t" + book.getName() + "\t" 78 + book.getPrice()); 79 } 80 81 } 82 83 private static void updateBook() { 84 String id = inputStr("请输入要修改的图书id"); 85 int index = 0; 86 for (Book book : lbs) { 87 if (id.equals(book.getId())) { 88 book.setName(inputStr("请输入修改后的名称:")); 89 book.setPrice(inputDouble("请输入修改后的价格:")); 90 lbs.remove(index); 91 lbs.add(index, book); 92 } 93 index++; 94 } 95 } 96 97 private static void delBook() { 98 String id = inputStr("请输入要删除的图书id"); 99 int index = 0; 100 for (Book book : lbs) { 101 if (id.equals(book.getId())) { 102 lbs.remove(index); 103 } 104 index++; 105 } 106 107 } 108 109 private static void addBook() { 110 String id = inputStr("请输入图书id"); 111 String name = inputStr("请输入图书名称"); 112 double price = inputDouble("请输入图书价格"); 113 Book book = new Book(id, name, price); 114 lbs.add(book); 115 } 116 117 public static String inputStr(String msg) { 118 System.out.print(msg); 119 return new Scanner(System.in).nextLine(); 120 } 121 122 public static int inputInt(String msg) { 123 System.out.print(msg); 124 return new Scanner(System.in).nextInt(); 125 } 126 127 public static double inputDouble(String msg) { 128 System.out.print(msg); 129 return new Scanner(System.in).nextDouble(); 130 } 131 }
2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。
1 <%@page import="gxlbao.Book"%> 2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 3 4 <!DOCTYPE HTML> 5 <html> 6 <head> 7 <meta charset="utf-8"> 8 <title></title> 9 </head> 10 11 <body> 12 <% 13 List<Book> lbs = new ArrayList<Book>(); 14 lbs.add(new Book("10001", "西游记", 50.8)); 15 lbs.add(new Book("10002", "红楼梦", 59.6)); 16 lbs.add(new Book("10003", "三国演义", 65.5)); 17 lbs.add(new Book("10004", "水浒传", 49.9)); 18 %> 19 <table> 20 <tr> 21 <th>图书编号</th> 22 <th>图书名称</th> 23 <th>图书价格</th> 24 </tr> 25 <% 26 for (int i = 0; i < lbs.size(); i++) { 27 %> 28 <tr> 29 <td><%=lbs.get(i).getId()%></td> 30 <td><%=lbs.get(i).getName()%></td> 31 <td><%=lbs.get(i).getPrice()%></td> 32 </tr> 33 <% 34 } 35 %> 36 </table> 37 </body> 38 </html>
3.在MySQL中创建Book表,里面id,name,price,
用命令实现,
添加一个图书,
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书,
把20元以上的图书价格都修改为18.8,
查看全部图书,
查看价格高于10块钱的全部图书
1 DROP TABLE IF EXISTS `book`; 2 CREATE TABLE `book` ( 3 `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '图书编号', 4 `name` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '图书名称', 5 `price` decimal(10, 2) NOT NULL COMMENT '图书价格', 6 PRIMARY KEY (`id`) USING BTREE 7 ); 8 9 INSERT INTO `book` VALUES (1, '水浒传', 50.90); 10 INSERT INTO `book` VALUES (2, '红楼梦', 40.50); 11 INSERT INTO `book` VALUES (3, '西游记', 45.90); 12 INSERT INTO `book` VALUES (4, '我的第一本书', 20.00); 13 14 UPDATE `book` SET `price`=18.8 WHERE `price`>20; 15 16 SELECT * FROM `book`; 17 18 SELECT * FROM `book` WHERE `price`>10; 19 20 DELETE FROM `book` WHERE `name` LIKE '我%'; 21 22 DELETE FROM `book`;