第九周作业

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

复制代码
public class Book {
     
    private int id;
    private String name;
    private double price;
    
    public Book(int id, String name, double price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }
    
    public Book() {
        super();
    }
    
    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;
    }
    
    


}
复制代码
复制代码
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;

    
    public class main {
        public static void main(String[] args) {
            List<Book> list = new ArrayList<Book>();
            Book book = new Book();

             Book b1 = new Book(1, "斗破苍穹", 666);
             Book b2 = new Book(2, "斗罗大陆", 999);
             Book b3 = new Book(3, "你是我的荣耀", 123);

            list.add(b1);
            list.add(b2);
            list.add(b3);
            show(list, book);
        }

        public static void show(List<Book> list, Book book) {
            System.out.println("1.添加图书");
            System.out.println("2.删除图书");
            System.out.println("3.修改图书");
            System.out.println("4.查询图书");
            System.out.println("请选择");
            select(list, book);
        }

        public static void select(List<Book> list, Book book) {
            int i = new Scanner(System.in).nextInt();
            switch (i) {
            case 1:
                System.out.println("请添加图书的编号:");
                book.setId(new Scanner(System.in).nextInt());
                System.out.println("请添加图书的名称:");
                book.setName(new Scanner(System.in).next());
                System.out.println("请添加图书的价格:");
                book.setPrice(new Scanner(System.in).nextDouble());
                list.add(book);
                System.out.println("添加成功");
                show(list, book);

                break;
            case 2:
                System.out.println("请输入要删除图书的名称:");
                String name=new Scanner(System.in).next();
                for (int j = 0; list != null && j < list.size(); j++) {
                    if (list.get(j).getName().equals(name)) {
                        list.remove(j);
                    }
                }
                System.out.println("删除成功");
                show(list, book);
                break;
            case 3:
                System.out.println("请输入要修改图书的编号:");
                int k=new Scanner(System.in).nextInt();;
                for (int j = 0; list != null && j < list.size(); j++) {
                    if (list.get(j).getName().equals(k)) {
                        list.remove(j);
                        System.out.println("请输入修改后图书的名称:");
                        String name1=new Scanner(System.in).next();
                        System.out.println("请输入修改后图书的价格:");
                        double price1=new Scanner(System.in).nextDouble();
                        Book b=new Book(k,name1,price1);
                        list.add(b);
                        break;
                    }
                }
                System.out.println("修改成功");
                show(list, book);
                break;

            case 4:
                for (int j = 0; list != null && j < list.size(); j++) {
                    System.out.println(list.get(j).getId() + "."
                            + list.get(j).getName() + "," + list.get(j).getPrice());
                }
                show(list, book);
                break;
            }
        }

    }
复制代码

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

复制代码
<%@page import="lmq.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>
  <head> 
  </head>
  <body>
   <% 
      List<Book> list=new ArrayList<Book>();
        Book b1 = new Book(1, "book1", 11.11);
        Book b2 = new Book(2, "book2", 22.22);
        Book b3 = new Book(3, "book3", 33.33);
        list.add(b1);
        list.add(b2);
        list.add(b3);
        for(Book book4:list){
        out.println(book4+"<br>");
        }
   %>
  </body>
</html>
复制代码

 

 

3.在MySQL中创建Book表,里面id,name,price,用命令实现

复制代码
create database books;

use books;

create table book(
id int(10),
name varchar(10),
price float
);
复制代码

添加一个图书,

insert into book values('1','我我我','9.99');
insert into book values('2','嗨害嗨','99.99');

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

update book set price=18.8 where price>20;

查看全部图书,

 select * from book;

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

select * from book where price >10;

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

delete from book where name like '我%';

删除全部图书,

delete   from book;

 

posted @ 2022-05-01 17:17  陈曲晴  阅读(15)  评论(0编辑  收藏  举报