jsp九周作业

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

package BookManagement.Dao;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestBook {
    static List<Book> book_list = new ArrayList<>();
    static Scanner scanner = new Scanner(System.in);
static {
    
        book_list.add(new Book(01, "西游记", 55.5));
        book_list.add(new Book(02, "红楼梦", 45.0));
        book_list.add(new Book(03, "三国演义", 63.5));
        book_list.add(new Book(04, "水浒传", 52.0));
    }   
    public static void main(String[] args) {
        while (true){
            System.out.println("1.查询图书");
            System.out.println("2.添加图书");
            System.out.println("3.更新图书");
            System.out.println("4.删除图书");
            System.out.println("5.退出系统");
            System.out.println("请输入操作");

            int num = scanner.nextInt();
            switch (num) {
    
                case 1:
                    selectBook();
                    break;
                case 2:
                    addBook();
                    break;
                case 3:
                    updateBook();
                    break;
                case 4:
                    deleteBook();
                    break;
                case 5:
                    System.out.println("系统已经退出。。。。");
                    return;
                default:
                    System.out.println("输入错误请重新输入");
                    break;
            }
        }
    }
    public static void addBook(){
        Book book = new Book();
        System.out.println("请输入图书编号:");
        int id = scanner.nextInt();
        book.setBookId(id);

        System.out.println("请输入图书名称:");
        String name = scanner.next();
        book.setBookName(name);

        System.out.println("请输入图书价格:");
        Double price = scanner.nextDouble();
        book.setBookPrice(price);

        book_list.add(book);
        System.out.println("添加成功!");

        prompt();
    }
   public static void selectBook() {
        for (Book book: book_list) {  
            System.out.println(book);
        }
    }
    public static void deleteBook() {
    System.out.println("请输入需要删除的图书编号:");
        int id = scanner.nextInt();
        int count = 0;
        for (Book book: book_list) {
            if(id == book.getBookId()){
                book_list.remove(book);
                count++;
                break;
            }
        }

        if(count == 0){
    
            System.out.println("没有这本书...");
        }else {
    
            System.out.println("删除成功");
        }
    }
    public static void updateBook() {
        System.out.println("请输入要修改的图书编号:");
        int find_id = scanner.nextInt();
        int count = 0;
        for(int i = 0; i < book_list.size(); i++){
            Book book = book_list.get(i);

            if(find_id == book.getBookId()){
    
                System.out.println("已经找到图书 \n" + book_list.get(i));

                System.out.println("修改图书编号:");
                int id = scanner.nextInt();
                book.setBookId(id);

                System.out.println("修改图书名称:");
                String name = scanner.next();
                book.setBookName(name);

                System.out.println("修改图书价格:");
                Double price = scanner.nextDouble();
                book.setBookPrice(price);
                book_list.set(i, book);
                count++;
                break;
            }
        }
        if(count == 0){   
            System.out.println("没有这本书...");
        }else {    
            System.out.println("更新成功");
        }
    }
    public static void prompt(){
        System.out.println("是否继续添加");
        System.out.println("1.继续添加");
        System.out.println("2.结束添加");
        int num = scanner.nextInt();
        if(num == 1){
            addBook();
        }else if(num == 2){
            System.out.println("添加结束!");
        }else {
            System.out.println("输入错误!");
            prompt();
        }
    }

}
package BookManagement.Dao;

public class Book {
    private int bookId;  // 编号
    private String bookName;   //
    private double bookPrice;  // 价格
    public Book(int bookId, String bookName, double bookPrice) {  
        this.bookId = bookId;
        this.bookName = bookName;
        this.bookPrice = bookPrice;
    }
    public Book() {
    }
    public int getBookId() {
        return bookId;
    }
    public void setBookId(int bookId) {
    
        this.bookId = bookId;
    }

    public String getBookName() {
    
        return bookName;
    }

    public void setBookName(String bookName) {
    
        this.bookName = bookName;
    }

    public double getBookPrice() {
    
        return bookPrice;
    }

    public void setBookPrice(double bookPrice) {
    
        this.bookPrice = bookPrice;
    }

    @Override
    public String toString() {
    
        return "Book{" +
                "bookId=" + bookId +
                ", bookName='" + bookName + '\'' +
                ", bookPrice=" + bookPrice +
                '}';
    }
    
    
}

 

 

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

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="BookMan.Book" %>
    
  </head>
 
  <body>
  <%
        ArrayList<Book> list=new ArrayList<Book>();
        Book book1=new Book(1,"钢铁是怎样炼成的",43);
        Book book2=new Book(2,"海底两万里",44.5);
        Book book3=new Book(3,"Java",54.2);
        list.add(book1);
        list.add(book2);
        list.add(book3); 
        for(Book book:list){
        out.println(book);
        }
    %>
  </body>
</html>

 

 

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

create database book;

use book;

 

create table book(

id int primary key,

name varchar(20),

price double(6,2)

);

 

 

insert into book values(1,'雪中悍刀行',38.5);

INSERT INTO book VALUES (2,'我的钢铁',18.7),(3,'我是什么',34.5),

(4,'Javaweb',36.5);

 

 

 

select *from book;

delete from book where name like'%';

update book set price = 18.8 where price> 20;

 

 

 

 

posted @ 2022-04-28 13:40  jth12  阅读(10)  评论(0编辑  收藏  举报