JSP第七次作业

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

 1 package com.gd.entity;
 2 
 3 public class Book {
 4     private Integer id;
 5     private String name;
 6     private double price;
 7     
 8     public Book() {
 9         super();
10         // TODO Auto-generated constructor stub
11     }
12 
13     public Book(Integer id, String name, double price) {
14         super();
15         this.id = id;
16         this.name = name;
17         this.price = price;
18     }
19 
20     public Integer getId() {
21         return id;
22     }
23 
24     public void setId(Integer id) {
25         this.id = id;
26     }
27 
28     public String getName() {
29         return name;
30     }
31 
32     public void setName(String name) {
33         this.name = name;
34     }
35 
36     public double getPrice() {
37         return price;
38     }
39 
40     public void setPrice(double price) {
41         this.price = price;
42     }
43 
44     @Override
45     public String toString() {
46         return "Book [id=" + id + ", name=" + name + ", price=" + price + "]";
47     }
48 
49     
50     
51 }
 1 package com.gd.entity;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Scanner;
 6 
 7 public class TestBook {
 8 
 9     /**
10      * @param args
11      */
12     public static void main(String[] args) {
13         List<Book> list = new ArrayList<Book>();
14         Book book = new Book();
15         Book b1=new Book(1,"安徒生童话",15.0);
16         Book b2=new Book(2,"马化腾全传",20.5);
17         Book b3=new Book(3,"马云全传",50.00);
18         list.add(b1);
19         list.add(b2);
20         list.add(b3);
21         menu(list, book);
22         
23     }
24     public static void menu(List<Book> list, Book book){
25         System.out.println("1.添加图书");
26         System.out.println("2.删除图书");
27         System.out.println("3.修改图书");
28         System.out.println("4.查询图书");
29         System.out.println("请选择");
30         select(list, book);
31             
32     }
33     public static void select(List<Book> list, Book book) {
34         int i = new Scanner(System.in).nextInt();
35         switch (i) {
36         case 1:
37             System.out.println("请添加图书的编号:");
38             book.setId(new Scanner(System.in).nextInt());
39             System.out.println("请添加图书的名称:");
40             book.setName(new Scanner(System.in).next());
41             System.out.println("请添加图书的价格:");
42             book.setPrice(new Scanner(System.in).nextDouble());
43             list.add(book);
44             System.out.println("添加成功");
45             menu(list, book);
46 
47             break;
48         case 2:
49             System.out.println("请输入要删除图书的名称:");
50             String name=new Scanner(System.in).next();
51             for (int j = 0; list != null && j < list.size(); j++) {
52                 if (list.get(j).getName().equals(name)) {
53                     list.remove(j);
54                 }
55             }
56             System.out.println("删除成功");
57             menu(list, book);
58             break;
59         case 3:
60             System.out.println("请输入要修改图书的编号:");
61             int k=new Scanner(System.in).nextInt();;
62             for (int j = 0; list != null && j < list.size(); j++) {
63                 if (list.get(j).getId().equals(k)) {
64                     list.remove(j);
65                     System.out.println("请输入修改后图书的名称:");
66                     String name1=new Scanner(System.in).next();
67                     System.out.println("请输入修改后图书的价格:");
68                     double price1=new Scanner(System.in).nextDouble();
69                     Book b=new Book(k,name1,price1);
70                     list.add(b);
71                     break;
72                 }
73             }
74             System.out.println("修改成功");
75             menu(list, book);
76             break;
77 
78         case 4:
79             for (int j = 0; list != null && j < list.size(); j++) {
80                 System.out.println(list.get(j).getId() + "."
81                         + list.get(j).getName() + "," + list.get(j).getPrice());
82             }
83             menu(list, book);
84             break;
85         }
86     }
87 
88 }

 

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

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@page import="com.gd.entity.Book"%>
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5 <head>
 6 </head>
 7 
 8 <body>
 9     <%
10         List<Book> list = new ArrayList<Book>();
11         Book b1=new Book(1,"安徒生童话",15.0);
12         Book b2=new Book(2,"马化腾全传",20.5);
13         Book b3=new Book(3,"马云全传",50.00);
14         list.add(b1);
15         list.add(b2);
16         list.add(b3);
17         for (int i = 0; list != null && i < list.size(); i++) {
18             out.print(list.get(i).getId() + "." + list.get(i).getName()
19                     + "," + list.get(i).getPrice() + "<br>");
20         }
21     %>
22 </body>
23 </html>

 

 3.在MySQL中创建Book表,里面id,name,price,
用命令实现,
添加一个图书,
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书,
把20元以上的图书价格都修改为18.8,
查看全部图书,
查看价格高于10块钱的全部图书

 

 

posted @ 2022-04-28 21:23  MXT16  阅读(23)  评论(0编辑  收藏  举报