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
TABLE
books(
id
INT
NOT
NULL
AUTO_INCREMENT,
name
VARCHAR
(100)
NOT
NULL
,
price
VARCHAR
(100)
NOT
NULL
,
PRIMARY
KEY
( id )
)ENGINE=InnoDB
DEFAULT
CHARSET=utf8;
INSERT INTO books VALUES(1,'老子',15.5),(2,'孙子',21),(3,'墨子',33),(4,'春秋',18.8),(5,'战国',19);
update books set price=18.8 where price>20;
delete from books;
select * from books where price>10;
select * from books;
DELETE FROM books WHERE name LIKE '我%';
posted on 2022-05-01 10:52 HumbleSwag 阅读(13) 评论(0) 编辑 收藏 举报