JSP第九周作业

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

package com.week09;

public class Book {
    private String id;
    private String name;
    private double price;

    public String getId() {
        return id;
    }

    public void setId(String _id) {
        id = _id;
    }

    public String getName() {
        return name;
    }

    public void setName(String _name) {
        name = _name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double _price) {
        price = _price;
    }

    public Book() {
        id = "";
        name = "";
        price = 0.0;
    }
    public Book(String _id, String _name, double _price) {

        id = _id;
        name = _name;
        price = _price;
    }
}


package com.week09;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {
        ArrayList<Book> books = new ArrayList<>();
        while (true) {
            System.out.println("请输入数字选择: ");
            System.out.println("1. 添加数据");
            System.out.println("2. 删除数据");
            System.out.println("3. 修改数据");
            System.out.println("4. 查询数据");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            String op;
            op = readLine(bufferedReader);
            System.out.println(op);
            if (op != null) {
                if (op.equals("1")) {
                    Book book = new Book();

                    System.out.println("请输入id: ");
                    book.setId(readLine(bufferedReader));
                    System.out.println("请输入名称: ");
                    book.setName(readLine(bufferedReader));
                    System.out.println("请输入价格: ");
                    book.setPrice(Double.parseDouble(readLine(bufferedReader)));
                    books.add(book);
                    System.out.println("添加完成");
                    continue;
                }
                else if (op.equals("2")) {
                    Book book = new Book();

                    System.out.println("请输入id: ");
                    book.setId(readLine(bufferedReader));

                    boolean deleted = false;

                    for (int n = 0; n < books.size(); n++) {
                        if (books.get(n).getId().equals(book.getId())) {
                            books.remove(n);
                            deleted = true;
                            break;
                        }
                    }

                    if (deleted) {
                        System.out.println("删除完成");
                    }
                    else {
                        System.out.println("无此书籍");
                    }

                    continue;
                }
                else if (op.equals("3")) {
                    Book book = new Book();

                    System.out.println("请输入id: ");

                    System.out.println("请输入新id: ");
                    book.setId(readLine(bufferedReader));
                    System.out.println("请输入名称: ");
                    book.setName(readLine(bufferedReader));
                    System.out.println("请输入价格: ");
                    book.setPrice(Double.parseDouble(readLine(bufferedReader)));

                    boolean found = false;
                    int index = 0;

                    for (index = 0; index < books.size(); index++) {
                        if (books.get(index).getId().equals(book.getId())) {
                            found = true;
                            break;
                        }
                    }

                    if (found) {
                        System.out.println("修改完成");
                    }
                    else {
                        System.out.println("无此书籍");
                    }

                    continue;
                }
                else if (op.equals("4")) {
                    System.out.println("请输入id: ");
                    String id = readLine(bufferedReader);
                    for (int n = 0; n < books.size(); n++) {
                        if (books.get(n).getId().equals(id)) {
                            System.out.printf("book: %s %s %f\n", books.get(n).getId(), books.get(n).getName(), books.get(n).getPrice());
                        }
                    }
                }
            }
        }
    }
    private static String readLine(BufferedReader bufferedReader) {
        try {
            return bufferedReader.readLine();
        }
        catch (Exception e) {
            return null;
        }
    }
}

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

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    
<%@page import="week09.Book"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Page</title>
</head>
<body>
<%
	Book[] books = new Book[3];
	books[0] = new Book("0", "aa", 20);
	books[1] = new Book("1", "图书", 20);
	books[2] = new Book("2", "字典", 20);
	for (int n = 0; n < books.length; n++)
	{
		out.println("<p>" + books[n].getId() + " " + books[n].getName() + " " + books[n].getPrice() + "</p>");
	}
%>
</body>
</html>

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

CREATE TABLE `week09`.`book` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(255) NOT NULL DEFAULT '',
  `Price` double NULL DEFAULT NULL,
  PRIMARY KEY (`Id`)
) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO `week09`.`book` SET `Id`=1,`Name`='aa',`Price`=20.0000000000000000000000;

DELETE FROM `week09`.`book` WHERE name LIKE '我%';

DELETE FROM `week09`.`book`;

UPDATE `week09`.`book` SET price = 18.8 WHERE price > 20;

SELECT * FROM `week09`.`book`;

SELECT * FROM `week09`.`book` WHERE price > 10.0;
posted @ 2022-05-01 17:01  SuoJing  阅读(12)  评论(0编辑  收藏  举报