第九周

package diqizhouzuoye;

public class Book {
    private Integer id;
    private String name;
    private double price;
    public Book() {
        super();
        // TODO Auto-generated constructor stub
    }
    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;
    }
    public Book(Integer id, String name, double price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }
    @Override
    public String toString() {
        return "book [id=" + id + ", name=" + name + ", price=" + price + "]";
    }
    
}
package diqizhouzuoye;

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

public class xiugai {

    public static void main(String[] args) {
        List<Book> list = new ArrayList<Book>();
        Book book = new Book();
        Book b1 = new Book(1, "《关于我在校园里遇到的诡异故事》", 15.00);
        Book b2 = new Book(2, "《狗都不看的书》", 25.00);
        Book b3 = new Book(3, "《十八般武艺第二转》", 35.00);
        Book b4 = new Book(4, "《龙珠Z》", 45.00);
        list.add(b1);
        list.add(b2);
        list.add(b3);
        list.add(b4);
        caidan(list, book);
    }

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

    }

    public static void caozuo(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("**********添加成功*********");
            caidan(list, book);
            break;
        case 2:
            System.out.println("******请选择删除图书的编号*****");
            int k = new Scanner(System.in).nextInt();
            for (int j = 0; list != null && j < list.size(); j++) {
                if (list.get(j).getId().equals(k)) {
                    list.remove(j);
                }

            }
            System.out.println("***********删除成功***********");
            caidan(list, book);
            break;
        case 3:
            System.out.println("请输入要修改图书的编号:");
            int ks = new Scanner(System.in).nextInt();
            ;
            for (int j = 0; list != null && j < list.size(); j++) {
                if (list.get(j).getId().equals(ks)) {
                    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(ks, name1, price1);
                    list.add(b);
                    break;
                }
            }
            System.out.println("修改成功");
            caidan(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());
            }
            caidan(list, book);
            break;
        case 0 :
            System.out.println("再见");
            return;
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
%>
<%@page import='java.util.*' %>
<%@page import='diqizhouzuoye.Book' %>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <%
            List<Book> list = new ArrayList<Book>();
            Book book = new Book();
            Book b1 = new Book(1, "《关于我在校园里遇到的诡异故事》", 15.00);
            Book b2 = new Book(2, "《狗都不看的书》", 25.00);
            Book b3 = new Book(3, "《十八般武艺第二转》", 35.00);
            Book b4 = new Book(4, "《龙珠Z》", 45.00);
            list.add(b1);
            list.add(b2);
            list.add(b3);
            list.add(b4);
            for(int i =0;list !=null && i<list.size();i++){
                out.print(list.get(i).getId() + "." + list.get(i).getName()
                + "," + list.get(i).getPrice() + "<br>");
            }
        %>
    </body>
</html>

 

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

 

 

posted on 2022-05-01 19:07  不顾倾世、只倾她一  阅读(33)  评论(0编辑  收藏  举报

导航