jsp第九周作业

package pojo;

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

import pojo.Book;

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

public class App {
    public static void main(String[] args) {
        List<Book> list = new ArrayList<Book>();
        Book book = new Book();
        Book b1 = new Book(1, "算法导论", 66);
        Book b2 = new Book(2, "图解算法", 77);
        Book b3 = new Book(3, "剑指offer", 88);
        list.add(b1);
        list.add(b2);
        list.add(b3);
        show(list, book);
    }

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

    public static void select(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("添加成功");
                show(list, book);

                break;
            case 2:
                System.out.println("请输入要删除图书的名称:");
                String name=new Scanner(System.in).next();
                for (int j = 0; list != null && j < list.size(); j++) {
                    if (list.get(j).getName().equals(name)) {
                        list.remove(j);
                    }
                }
                System.out.println("删除成功");
                show(list, book);
                break;
            case 3:
                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("请输入修改后图书的名称:");
                        String name1=new Scanner(System.in).next();
                        System.out.println("请输入修改后图书的价格:");
                        double price1=new Scanner(System.in).nextDouble();
                        Book b=new Book(k,name1,price1);
                        list.add(b);
                        break;
                    }
                }
                System.out.println("修改成功");
                show(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());
                }
                show(list, book);
                break;
        }
    }
}

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="pojo.Book"%>
<!DOCTYPE HTML >
<html>
<head>
    <title>My JSP 'index.jsp' starting page</title>
</head>
<body>

<%
    List<Book> list = new ArrayList<Book>();
    Book b1 = new Book(1, "算法导论", 66);
    Book b2 = new Book(2, "图解算法", 77);
    Book b3 = new Book(3, "剑指offer", 88);
    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>

 

/*
 Navicat Premium Data Transfer

 Source Server         : MySQL57
 Source Server Type    : MySQL
 Source Server Version : 50721
 Source Host           : localhost:3306
 Source Schema         : 1

 Target Server Type    : MySQL
 Target Server Version : 50721
 File Encoding         : 65001

 Date: 01/05/2022 15:05:22
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for book
-- ----------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book`  (
  `id` int(11) NOT NULL,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `price` decimal(10, 2) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of book
-- ----------------------------
INSERT INTO `book` VALUES (1, '算法导论', 66.00);
INSERT INTO `book` VALUES (2, '图解算法', 77.00);
INSERT INTO `book` VALUES (3, '剑指offer', 88.00);

SET FOREIGN_KEY_CHECKS = 1;

 

 

posted on 2022-05-01 15:07  chenyulin11  阅读(22)  评论(0编辑  收藏  举报

导航