第九次作业

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

package sss;
/*
 * 1.做一个图书类Book  id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参
做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
 */
 
public class Book {
    private Integer id;
    private String name;
    private Integer price;
    public Book() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Book(Integer id, String name, Integer price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }
    @Override
    public String toString() {
        return "Book [id=" + id + ", name=" + name + ", price=" + price + "]";
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getPrice() {
        return price;
    }
    public void setPrice(Integer price) {
        this.price = price;
    }
     
     
}
package sss;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class test {
    static List<Book> list=new ArrayList<Book>();   
    static{
        Book book1=new Book(1,"java",30);
        Book book2=new Book(2,"html",25);
        Book book3=new Book(3,"jsp",31);
        list.add(book1);
        list.add(book2);
        list.add(book3);
    }
    public static void search(){
        for(Book book4:list){
            System.out.println(book4);
        }
    }
    public static void change(){
        System.out.println("请输入需要修改的图书编号");
        Integer update=new Scanner(System.in).nextInt();
        for(Book book:list){
            if(update == book.getId()){
                System.err.println("请输入书名");
                String name1=new Scanner(System.in).next();
                System.err.println("请输入价格");
                int price1=new Scanner(System.in).nextInt();
                book.setName(name1);
                book.setPrice(price1);
                System.out.println("修改成功");
            }
        }
    }
    public static void del(){
        System.out.println("请输入删除的图书编号");
        int id=new Scanner(System.in).nextInt();
        try{
            for(Book book:list){
                if(list.size()!=0){
                    if(id==book.getId()){
                        list.remove(book);
                        System.out.println("删除成功");
                    }
                }
            }
        }catch(Exception e){
             
        }
    }
    public static void add(){
        Scanner scanner=new Scanner(System.in);
        System.err.println("请输入编号");
        int idd=scanner.nextInt();
        System.err.println("请输入书名");
        String name2=scanner.next();
        System.err.println("请输入价格");
        int price2=scanner.nextInt();
        Book book=new Book(idd,name2,price2);
        list.add(book);
        System.out.println("添加成功");
    }
}
package sss;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;
 
public class show {
    private static test b = new test();
    public static void main(String[] args) {
         
    while(true){
        System.out.println("1.添加");
        System.out.println("2.删除");
        System.out.println("3.修改");
        System.out.println("4.查询");
        System.out.println("5.退出");
        System.out.println("请选择");
        Scanner scanner=new Scanner(System.in);
        int a=scanner.nextInt();
            switch (a) {
            case 1:
                b.add();       
                break;
            case 2:
                b.del();
                break;
            case 3:
                b.change();
                break;
            case 4:
                b.search();
                break;
            case 5:
                System.out.println("退出成功");
                return;
                default:System.out.println("输入错误请重新输入");
                break;
            }
        }
    }
}

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

<%@page import="yxf.Book"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <%List<Book> list =new ArrayList<Book>();
        Book book1=new Book(1,"java",30);
        Book book2=new Book(2,"html",25);
        Book book3=new Book(3,"jsp",31);
        list.add(book1);
        list.add(book2);
        list.add(book3);
    for (Book book4 : list) {
            out.print(book4+"<br>");
    }
  %>
  <body>
  </body>
</html><%@page import="yxf.Book"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <%List<Book> list =new ArrayList<Book>();
        Book book1=new Book(1,"java",30);
        Book book2=new Book(2,"html",25);
        Book book3=new Book(3,"jsp",31);
        list.add(book1);
        list.add(book2);
        list.add(book3);
    for (Book book4 : list) {
            out.print(book4+"<br>");
    }
  %>
  <body>
  </body>
</html>

3.在MySQL中创建Book表,里面id,name,price,

用命令实现,添加一个图书,

create database myBook;
use myBook;
create table book(
    id int comment '书籍编号',
    bookName varchar(10) comment '书名',
    bookPrice double(6,2) comment '书籍价格'
)comment '书籍表';
desc book;
insert into book values (1,'高数',100);
insert into book values (2,'政治',100);
insert into book values (3,'英语',100);
insert into book values (4,'专业课',100);
select * from book;

  

根据名称删除图书,

delete from book where bookName ='高数';

 

把所有名称是“专”开头的图书删除,

elete from book where bookName like '专%';

 

 

删除全部图书,

delete from book;

 

把20元以上的图书价格都修改为18.8,查看全部图书,

update book set bookPrice=18.8 where bookPrice>20;

 

查看价格高于10块钱的全部图书

select * from book where bookPrice>10;

 

posted @ 2022-05-01 15:54  无言De钉钉  阅读(11)  评论(0编辑  收藏  举报