JSP第九周作业

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

 

package com.gn;

public class book {
    private int id;
    private String name;
    private double price;
    public int getId() {
    return id;
}
public void setId(int 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 price) {
    this.price = price;
}
public book() {
    super();
}
public book(int id, String name, double price) {
    super();
    this.id = id;
    this.name = name;
    this.price = price;
}


}
package com.gn;

import java.util.ArrayList;
import java.util.Scanner;

public class test {

    static ArrayList<book> list=new ArrayList<book>();
    static Scanner sc=new Scanner(System.in); 
    public static void main(String[] args) {
        book book1=new book(1,"红楼梦",103);
        book book2=new book(2,"西游记",98);
        book book3=new book(3,"三国演义",55);
        
        list.add(book1);
        list.add(book2);
        list.add(book3);
        for (int i = 0; i < list.size(); i++) {
            book b=list.get(i);
            System.out.println("id为"+b.getId()+","+"《"+b.getName()+"》"+"价格为"+b.getPrice());
        }
        showmain();
    }
    private static void showmain() {
    
        System.out.println("1.添加图书");
        System.out.println("2.删除图书");
        System.out.println("3.修改图书");
        System.out.println("4.查询图书");
        System.out.println("请选择你的操作:");
        
        int num=sc.nextInt();
        switch(num){
        case 1:add();break;
        case 2:delete();break;
        case 3:date();break;
        case 4:select();break;
        }
    }

    private static void select() {
      
        System.out.println("请输入图书id");
        int id=sc.nextInt();
            for (int i = 0; i < list.size(); i++) {
                if (id == list.get(i).getId()) {
                        System.out.println("查询结果为");
                        book b1=list.get(i);
                        System.out.println("id为"+b1.getId()+","+"《"+b1.getName()+"》"+"价格为"+b1.getPrice());                                                    
                    }
            }
        showmain();
    }

    private static void date() {
       
        System.out.println("请输入图书id");
        int id=sc.nextInt();
        
            for (int i = 0; i < list.size(); i++) {
                if (id == list.get(i).getId()) {
                    list.remove(i);
                    System.out.println("请输入图书name");
                    String name=sc.next();
                    System.out.println("请输入图书price");
                    int price=sc.nextInt();
                    book b=new book();
                    b.setId(id);
                    b.setName(name);
                    b.setPrice(price);
                    list.add(b);
                    System.out.print("操作成功为");
                    f();
                
                }
            }
             showmain();
    }

    private static void delete() {
        
        System.out.println("请输入图书id");
        int id=sc.nextInt();
            for (int i = 0; i < list.size(); i++) {
                if (id == list.get(i).getId()) {
                    list.remove(i);
                    System.out.println("删除成功");
                    }
            }
            f();
        showmain();
    }

    private static void add() {
        
        System.out.println("请输入图书id");
        int id=sc.nextInt();
        System.out.println("请输入图书name");
        String name=sc.next();
        System.out.println("请输入图书price");
        int price=sc.nextInt();
        book b=new book(id,name,price);
        list.add(b);
        f();
        showmain();
    }

    private static void f() {
        System.out.println("最新的图书列表是:");
        for (int i = 0; i < list.size(); i++) {
            book b1=list.get(i);
            System.out.println("id为"+b1.getId()+","+"《"+b1.getName()+"》"+"价格为"+b1.getPrice());
        }
    }
}

 

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

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.gn.book"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>My JSP 'booklist.jsp' starting page</title>  
  </head>
  
  <body>
     信息如下:<br>
<%
        ArrayList<book> list=new ArrayList<book>();
        book book1=new book(1,"《红楼梦》",103);
        book book2=new book(2,"《西游记》",98);
        book book3=new book(3,"《三国演义》",55);
        
        list.add(book1);
        list.add(book2);
        list.add(book3);
        for (int i = 0; i < list.size(); i++) {
            book b=list.get(i);
            int id=b.getId();
            String name=b.getName();
            double price=b.getPrice();
            out.print("id为"+id+",书名为"+name+",价格为"+price);
%><br><% 
        }
        
    %>

  </body>
</html>

 

 

 

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

//建库
create database books;
use books;

//建表
create table book(
    id double(100,10) primary key auto_increment,
    name varchar(10) not null ,
    price double(100,10)
   );

//插入数据
insert into book(id,name,price) values(1,"我和我的祖国",20),(2,"我和我的家乡",30),(3,"红楼梦",103),(4,"西游记",98),(5,"三国演义",55),(6,"水浒传",180);

 

 

//查看全部图书
select * from  book;

//查看价格高于10块钱的全部图书
select * from  book where price>10;

//把20元以上的图书价格都修改为18.8
update book set price=18.8 where price>20;

//把所有名称是“我”开头的图书删除
delete from book where name like'我%';

//根据名称删除图书
delete from book where name='水浒传';

//删除全部图书
delete from book;

//删除表
drop table book;

 

posted @ 2022-05-01 16:28  飞七  阅读(14)  评论(0编辑  收藏  举报