JSP第九周作业

1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询

 1 package com.lwk.demo5;
 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 com.lwk.demo5;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 import java.util.Scanner;
  6 
  7 public class Test {
  8     static List<Book> lbs = new ArrayList<Book>();
  9 
 10     static {
 11         Book b1 = new Book("1001", "西游记", 50.9);
 12         lbs.add(b1);
 13         Book b2 = new Book("1002", "红楼梦", 49.9);
 14         lbs.add(b2);
 15         Book b3 = new Book("1003", "水浒传", 65.9);
 16         lbs.add(b3);
 17     }
 18 
 19     /**
 20      * @param args
 21      */
 22     public static void main(String[] args) {
 23         // TODO Auto-generated method stub
 24         mnue();
 25     }
 26 
 27     public static void mnue() {
 28         for (;;) {
 29             System.out.println("***********************");
 30             System.out.println("*    1、添加图书                            *");
 31             System.out.println("*    2、删除图书                            *");
 32             System.out.println("*    3、修改图书                            *");
 33             System.out.println("*    4、查询所有图书                    *");
 34             System.out.println("*    5、查询一本图书                    *");
 35             System.out.println("*    0、退出程序                            *");
 36             System.out.println("***********************");
 37             int i = inputInt("请输入要选择的操作:");
 38             switch (i) {
 39             case 1:
 40                 addBook();
 41                 break;
 42             case 2:
 43                 delBook();
 44                 break;
 45             case 3:
 46                 updateBook();
 47                 break;
 48             case 4:
 49                 findAllBook();
 50                 break;
 51             case 5:
 52                 findBookByName();
 53                 break;
 54             case 0:
 55                 return;
 56             default:
 57                 System.out.println("输入错误,请重新输入");
 58                 break;
 59             }
 60         }
 61     }
 62 
 63     private static void findBookByName() {
 64         String name = inputStr("请输入要查找的图书名称");
 65         int index = 0;
 66         System.out.println("id\t图书名称\t价格");
 67         for (Book book : lbs) {
 68             if (name.equals(book.getName())) {
 69                 System.out.println(book.getId() + "\t" + book.getName() + "\t"
 70                         + book.getPrice());
 71             }
 72             index++;
 73         }
 74     }
 75 
 76     private static void findAllBook() {
 77         System.out.println("id\t图书名称\t价格");
 78         for (Book book : lbs) {
 79             System.out.println(book.getId() + "\t" + book.getName() + "\t"
 80                     + book.getPrice());
 81         }
 82 
 83     }
 84 
 85     private static void updateBook() {
 86         String id = inputStr("请输入要修改的图书id");
 87         int index = 0;
 88         for (Book book : lbs) {
 89             if (id.equals(book.getId())) {
 90                 book.setName(inputStr("请输入修改后的名称:"));
 91                 book.setPrice(inputDouble("请输入修改后的价格:"));
 92                 lbs.remove(index);
 93                 lbs.add(index, book);
 94             }
 95             index++;
 96         }
 97     }
 98 
 99     private static void delBook() {
100         String id = inputStr("请输入要删除的图书id");
101         int index = 0;
102         for (Book book : lbs) {
103             if (id.equals(book.getId())) {
104                 lbs.remove(index);
105             }
106             index++;
107         }
108 
109     }
110 
111     private static void addBook() {
112         String id = inputStr("请输入图书id");
113         String name = inputStr("请输入图书名称");
114         double price = inputDouble("请输入图书价格");
115         Book book = new Book(id, name, price);
116         lbs.add(book);
117     }
118 
119     public static String inputStr(String msg) {
120         System.out.print(msg);
121         return new Scanner(System.in).nextLine();
122     }
123 
124     public static int inputInt(String msg) {
125         System.out.print(msg);
126         return new Scanner(System.in).nextInt();
127     }
128 
129     public static double inputDouble(String msg) {
130         System.out.print(msg);
131         return new Scanner(System.in).nextDouble();
132     }
133 
134 }

2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。

 1 <%@page import="com.lwk.demo5.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`;

 

posted @ 2022-04-30 14:34  Lwk36  阅读(14)  评论(0编辑  收藏  举报