第九周作业

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

package packk;

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


}
package com.lwk.demo5;

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

public class Test {
    static List<Book> lbs = new ArrayList<Book>();

    static {
        Book b1 = new Book("1001", "西游记", 50.9);
        lbs.add(b1);
        Book b2 = new Book("1002", "红楼梦", 49.9);
        lbs.add(b2);
        Book b3 = new Book("1003", "水浒传", 65.9);
        lbs.add(b3);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        mnue();
    }

    public static void mnue() {
        for (;;) {
            System.out.println("***********************");
            System.out.println("*    1、添加图书                            *");
            System.out.println("*    2、删除图书                            *");
            System.out.println("*    3、修改图书                            *");
            System.out.println("*    4、查询所有图书                    *");
            System.out.println("*    5、查询一本图书                    *");
            System.out.println("*    0、退出程序                            *");
            System.out.println("***********************");
            int i = inputInt("请输入要选择的操作:");
            switch (i) {
            case 1:
                addBook();
                break;
            case 2:
                delBook();
                break;
            case 3:
                updateBook();
                break;
            case 4:
                findAllBook();
                break;
            case 5:
                findBookByName();
                break;
            case 0:
                return;
            default:
                System.out.println("输入错误,请重新输入");
                break;
            }
        }
    }

    private static void findBookByName() {
        String name = inputStr("请输入要查找的图书名称");
        int index = 0;
        System.out.println("id\t图书名称\t价格");
        for (Book book : lbs) {
            if (name.equals(book.getName())) {
                System.out.println(book.getId() + "\t" + book.getName() + "\t"
                        + book.getPrice());
            }
            index++;
        }
    }

    private static void findAllBook() {
        System.out.println("id\t图书名称\t价格");
        for (Book book : lbs) {
            System.out.println(book.getId() + "\t" + book.getName() + "\t"
                    + book.getPrice());
        }

    }

    private static void updateBook() {
        String id = inputStr("请输入要修改的图书id");
        int index = 0;
        for (Book book : lbs) {
            if (id.equals(book.getId())) {
                book.setName(inputStr("请输入修改后的名称:"));
                book.setPrice(inputDouble("请输入修改后的价格:"));
                lbs.remove(index);
                lbs.add(index, book);
            }
            index++;
        }
    }

    private static void delBook() {
        String id = inputStr("请输入要删除的图书id");
        int index = 0;
        for (Book book : lbs) {
            if (id.equals(book.getId())) {
                lbs.remove(index);
            }
            index++;
        }

    }

    private static void addBook() {
        String id = inputStr("请输入图书id");
        String name = inputStr("请输入图书名称");
        double price = inputDouble("请输入图书价格");
        Book book = new Book(id, name, price);
        lbs.add(book);
    }

    public static String inputStr(String msg) {
        System.out.print(msg);
        return new Scanner(System.in).nextLine();
    }

    public static int inputInt(String msg) {
        System.out.print(msg);
        return new Scanner(System.in).nextInt();
    }

    public static double inputDouble(String msg) {
        System.out.print(msg);
        return new Scanner(System.in).nextDouble();
    }

}

 

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

<%@page import="com.lwk.demo5.Book"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>

<body>
    <%
        List<Book> lbs = new ArrayList<Book>();
        lbs.add(new Book("10001", "西游记", 50.8));
        lbs.add(new Book("10002", "红楼梦", 59.6));
        lbs.add(new Book("10003", "三国演义", 65.5));
        lbs.add(new Book("10004", "水浒传", 49.9));
    %>
    <table>
        <tr>
            <th>图书编号</th>
            <th>图书名称</th>
            <th>图书价格</th>
        </tr>
        <%
            for (int i = 0; i < lbs.size(); i++) {
        %>
        <tr>
            <td><%=lbs.get(i).getId()%></td>
            <td><%=lbs.get(i).getName()%></td>
            <td><%=lbs.get(i).getPrice()%></td>
        </tr>
        <%
            }
        %>
    </table>
</body>
</html>

 

 3.在MySQL中创建Book表,里面id,name,price,

用命令实现,
添加一个图书,
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书,
把20元以上的图书价格都修改为18.8,
查看全部图书,
查看价格高于10块钱的全部图书
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '图书编号',
  `name` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '图书名称',
  `price` decimal(10, 2) NOT NULL COMMENT '图书价格',
  PRIMARY KEY (`id`) USING BTREE
);

INSERT INTO `book` VALUES (1, '水浒传', 50.90);
INSERT INTO `book` VALUES (2, '红楼梦', 40.50);
INSERT INTO `book` VALUES (3, '西游记', 45.90);
INSERT INTO `book` VALUES (4, '我的第一本书', 20.00);

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

SELECT * FROM `book`;

SELECT * FROM `book` WHERE `price`>10;

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

DELETE FROM `book`;

 

 

posted @ 2022-04-30 23:10  kgbk  阅读(8)  评论(0编辑  收藏  举报