第九周作业

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

public class bookclass {
    private int bid;
    private String bname;
    private double bprice;
 
    public bookclass() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    public bookclass(int bid, String bname, double bprice) {
        super();
        this.bid = bid;
        this.bname = bname;
        this.bprice = bprice;
    }
 
    public int getBid() {
        return bid;
    }
 
    public void setBid(int bid) {
        this.bid = bid;
    }
 
    public String getBname() {
        return bname;
    }
 
    public void setBname(String bname) {
        this.bname = bname;
    }
 
    public double getBprice() {
        return bprice;
    }
 
    public void setBprice(double bprice) {
        this.bprice = bprice;
    }
 
    @Override
    public String toString() {
        return "Book [bid=" + bid + ", bname=" + bname + ", bprice=" + bprice + "]";
    }
 
}
package book;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class books {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        List<bookclass> blist = new ArrayList<>();
        bookclass b1 = new bookclass();
        b1.setBid(1);
        b1.setBname("孙子");
        b1.setBprice(18.8);
        blist.add(b1);
        bookclass b2 = new bookclass();
        b2.setBid(2);
        b2.setBname("老子");
        b2.setBprice(20);
        blist.add(b2);
        bookclass b3 = new bookclass();
        b3.setBid(3);
        b3.setBname("春秋");
        b3.setBprice(25);
        blist.add(b3);
        w: while (true) {
            System.out.println("1.增加2.删除3.修改4.查询");
            System.out.println("输入您的选择:");
            int x = sc.nextInt();
            int i;
            String n;
            double p;
            switch (x) {
            case 1:
                System.out.println("输入id:");
                i = sc.nextInt();
                System.out.println("输入name:");
                n = sc.next();
                System.out.println("输入price:");
                p = sc.nextDouble();
                add(i, n, p, blist);
                break;
            case 2:
                System.out.println("输入id:");
                i = sc.nextInt();
                delete(i, blist);
                break;
            case 3:
                System.out.println("输入id:");
                i = sc.nextInt();
                System.out.println("输入price:");
                p = sc.nextDouble();
                update(i, p, blist);
                break;
            case 4:
                System.out.println("是否查询所有:(y/n)");
                String pd = sc.next();
                if (pd.equals("y")) {
                    for (bookclass book : blist) {
                        System.out.println(book.toString());
                    }
                } else {
                    System.out.println("输入name:");
                    n = sc.next();
                    List<bookclass> blist1 = select(n, blist);
                    for (bookclass book : blist1) {
                        System.out.println(book.toString());
                    }
                }
 
                break;
            default:
 
                break w;
            }
        }
    }
 
    public static void add(int i, String n, double p, List<bookclass> blist) {
        bookclass b1 = new bookclass();
        b1.setBid(i);
        b1.setBname(n);
        b1.setBprice(p);
        blist.add(b1);
    }
 
    public static void delete(int i, List<bookclass> blist) {
        for (bookclass b : blist) {
            if (b.getBid() == i) {
                blist.remove(b);
            }
        }
    }
 
    public static void update(int i, double p, List<bookclass> blist) {
        for (bookclass b : blist) {
            if (b.getBid() == i) {
                b.setBprice(p);
            }
        }
    }
 
    public static List<bookclass> select(String n, List<bookclass> blist) {
        List<bookclass> blist1 = new ArrayList<>();
        for (bookclass b : blist) {
            if (b.getBname().equals(n)) {
                blist1.add(b);
            }
        }
        return blist1;
    }
}

 

 

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

package po;

public class Book {
    private Integer id;
    private String name;
    private double price;
    public Book(Integer 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;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public Book() {
        super();
        // TODO Auto-generated constructor stub
    }


}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="po.Book"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<%-- books.jsp --%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    List<Book> blist = new ArrayList<>();
    Book b1 = new Book();
    b1.setId(1);
    b1.setName("孙子");
    b1.setPrice(18.8);
    blist.add(b1);
    Book b2 = new Book();
    b2.setId(2);
    b2.setName("老子");
    b2.setPrice(20);
    blist.add(b2);
    Book b3 = new Book();
    b3.setId(3);
    b3.setName("春秋");
    b3.setPrice(25);
    blist.add(b3);
    for (Book b : blist) {
        int id = b.getId();
        String name = b.getName();
        double price = b.getPrice();

        out.print("<li>商品序号&nbsp" + id + "&nbsp商品名:" + name + "&nbsp价格:" + price + "&nbsp</li>");

    }
%>
</body>
</html>

 

 

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

CREATE TABLE books(
   id INT NOT NULL AUTO_INCREMENT,
   name VARCHAR(100) NOT NULL,
  price VARCHAR(100) NOT NULL,
    PRIMARY KEY ( id )
   )ENGINE=InnoDB DEFAULT CHARSET=utf8;

  

INSERT INTO books VALUES(1,'老子',15.5),(2,'孙子',21),(3,'墨子',33),(4,'春秋',18.8),(5,'战国',19);
update books set price=18.8 where price>20;
delete from books;
select * from books where price>10;
 select * from books;
DELETE FROM books WHERE name LIKE '我%';

 

posted @ 2022-04-28 14:02  王磊1  阅读(20)  评论(0编辑  收藏  举报