第九周上机作业

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

package test;
public class Book {
     private Integer id;
     private String name;
     private double price;
     // 无参函数构造
    public Book(){
         super();
     }
     //    有参函数构造
    public Book(int id,String name,double price){
         this.id =id;
         this.name = name;
         this.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 double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
     
}
package test;
import java.util.List;
import java.util.ArrayList;
import java.util.Scanner;

public class testClass {
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<Book> list = new ArrayList<Book>();
        Book b = new Book();
        Book b1 = new Book();
        b1.setId(1);
        b1.setName("西游记");
        b1.setPrice(54.6);
        Book b2 = new Book();
        b2.setId(2);
        b2.setName("红楼梦");
        b2.setPrice(45.2);
        Book b3 = new Book();
        b3.setId(3);
        b3.setName("水浒传");
        b3.setPrice(35.7);
        list.add(b1);
        list.add(b2);
        list.add(b3);
        show(list, b);
        System.out.println("-------------");
    }
    public static void show(List<Book> list,Book b){
        System.out.println("开始你的操作");
        System.out.println("1.添加");
        System.out.println("2.删除");
        System.out.println("3.修改");
        System.out.println("4.查询");    
        System.out.println("-------------");
        select(list,b);
    }
    public static void select(List<Book> list,Book b){
        Scanner input = new Scanner(System.in);
        int k = input.nextInt();
        switch (k) {
        case 1:
            System.out.println("添加图书的的id:");
            b.setId(new Scanner(System.in).nextInt());
            System.out.println("添加图书名称:");
            b.setName(new Scanner(System.in).next());
            System.out.println("添加图书的价格:");
            b.setPrice(new Scanner(System.in).nextDouble());
            list.add(b);
            System.out.println("添加成功");
            show (list,b);
            System.out.println("-------------");
            break;
        case 2:
            System.out.println("输入你要删除图书的id");
            int id = input.nextInt();
            for (int i = 0 ;i<list.size() ;i++) {
                if (list.get(i).getId().equals(id)) {
                    list.remove(i);
                }
            }
            System.out.println("已经删除!");
            show(list,b);
            break;
        case 3:
            System.out.println("请输入你要修改图书的id");
            int changeId = input.nextInt();
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getId().equals(changeId)) {
                    list.remove(i);
                    System.out.println("输入修改之后的图书名称:");
                    String name = input.next();
                    System.out.println("输入修改之后的图书价格:");
                    Double price = input.nextDouble();
                    Book b0 = new Book(changeId,name,price);
                    list.add(b0);
                    break;
                }
            }
            System.out.println("修改成功");
            show(list,b);
         case 4:
                for (int i = 0; i< list.size(); i++) {
                    System.out.println(list.get(i).getId() + "."
                            + list.get(i).getName() + "," + list.get(i).getPrice());
                }
                show(list, b);
                break;
            }
        }
}

 

 

 

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

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="test.Book"%>

<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  
  <body>
  <div style="background-color:skyblue;width=300px;height=100px">  
      <%
        List<Book> list = new ArrayList<Book>();
        Book b1 = new Book(1, "水浒传", 54.5);
        Book b2 = new Book(2, "西游记", 45.6);
        Book b3 = new Book(3, "红楼梦", 55.82);
        list.add(b1);
        list.add(b2);
        list.add(b3);
        for (int i = 0;  i < list.size(); i++) {
            out.print(list.get(i).getId() + "." + list.get(i).getName()
                    + "," + list.get(i).getPrice()+"<br>");
        }
    %>
  </div>
  </body>
</html>

 

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

 

# 建表
create table book(
    id double(100,10) primary key auto_increment,
    name varchar(10) not null ,
    price double(100,10)
   );
#插入数据
 insert into book (name,price) values('西游记',24.5),('我和我',14.5),('重生之天才小子',10);
 insert into book (name,price) values('三国演义',25),('水浒传',16),('红楼梦',19.6);
#查询价格大于10的书籍信息
select * from  book where price>10;
#查询所有书籍信息
select * from  book;
#将价格大于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-04-27 16:06  热血青年小刘  阅读(10)  评论(0编辑  收藏  举报