JSP第七次上机作业

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

package work;

public class Book {
    private String id;
    private String name;
    private double price;
    //get、set访问器
    public String getId() {
        return id;
    }

    public void setId(String 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(String id, String name, double price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }
    //无参的方法调用
    public Book() {
        super();
    }
    
    @Override
    public String toString() {
        return "Book [id=" + id + ", name=" + name + ", price=" + price + "]";
    }

}
package work;

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

public class Test {

    static java.util.List<Book> list = new ArrayList<Book>();
    static {// static是静态修饰符,修饰的成员可以通过类名加'.'进直接访问
        Book b1 = new Book("A001", "Java基础入门", 49.00);
        Book b2 = new Book("A002", "JavaScript前端开发案例教程", 59.80);
        Book b3 = new Book("A003", "JSP实用教程", 49.80);
        list.add(b1);
        list.add(b2);
        list.add(b3);
    }

    // 功能选择
    public static void main(String[] args) {
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("功能列表:\n1.查看图书\n2.插入图书\n3.修改图书\n4.删除图书\n5.退出程序");
            System.out.println("请选择1-5");
            int opt = scanner.nextInt();
            switch (opt) {
            case 1:
                showBook();
                System.out.println("是否返回主界面Y/N?");
                String w = scanner.next();
                if (w.equals("Y")) {
                    break;
                } else {
                    return;
                }
            case 2:
                addBook();
                System.out.println("是否返回主界面Y/N?");
                w = scanner.next();
                if (w.equals("Y")) {
                    break;
                } else {
                    return;
                }
            case 3:
                updateBook();
                System.out.println("是否返回主界面Y/N?");
                w = scanner.next();
                if (w.equals("Y")) {
                    break;
                } else {
                    return;
                }
            case 4:
                delBook();
                System.out.println("是否返回主界面Y/N?");
                w = scanner.next();
                if (w.equals("Y")) {
                    break;
                } else {
                    return;
                }
            case 5:
                System.out.println("退出成功");
                return;
            }
        }
    }

    private static void showBook() {
        // TODO Auto-generated method stub
        for (Book book4 : list) {
            System.out.println(book4);
        }
    }

    private static void updateBook() {
        // TODO Auto-generated method stub
        System.out.println("请输入修改的图书编号");
        String id = new Scanner(System.in).next();
        for (int i = 0; i < list.size(); i++) {
            Book book = list.get(i);
            if (id.equals(book.getId())) {
                System.err.println("请输入名称");
                String mc = new Scanner(System.in).next();
                System.err.println("请输入价格");
                double jg = new Scanner(System.in).nextDouble();
                book.setName(mc);
                book.setPrice(jg);
                System.out.println("修改成功");
            }
        }
    }

    private static void delBook() {
        System.out.println("请输入你要删除的图书编号");
        String id = new Scanner(System.in).next();
        for (int i = 0; i < list.size(); i++) {
            Book book = list.get(i);
            if (id.equals(book.getId())) {
                list.remove(i);
                System.out.println("删除成功!");
            }
        }

    }

    private static void addBook() {
        // TODO Auto-generated method stub
        // 构造一个Scanner类的对象来获取用户的输入
        Scanner scanner = new Scanner(System.in);
        System.err.println("请输入图书编号");
        String id = scanner.next();
        System.err.println("请输入图书名");
        String name = scanner.next();
        System.err.println("请输入图书价格");
        double price = scanner.nextDouble();

        Book book = new Book(id, name, price);
        list.add(book);
        System.out.println("添加成功");

    }
}

 

 

 

 

 

 

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

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="work.Book"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
<title></title>
</head>
<body>
    <%
        ArrayList<Book> list = new ArrayList<Book>();
        Book b1 = new Book("A001", "Java基础入门", 49.00);
        Book b2 = new Book("A002", "JavaScript前端开发案例教程", 59.80);
        Book b3 = new Book("A003", "JSP实用教程", 49.80);
        list.add(b1);
        list.add(b2);
        list.add(b3);
        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 @ 2022-04-30 23:25  小猪哏哏  阅读(14)  评论(0编辑  收藏  举报