第九周作业
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。
3.在MySQL中创建Book表,里面id,name,price,
用命令实现,
添加一个图书,
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书,
把20元以上的图书价格都修改为18.8,
查看全部图书,
查看价格高于10块钱的全部图书
package wjl; public class Book { private Integer id; private String name; private Integer price; public Book(Integer id, String name, Integer price) { super(); this.id = id; this.name = name; this.price = 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 Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } public Book(){ super(); } public String toString() { return "图书馆 [编号=" + id + ", 书名=" + name + ", 价格=" + price + "]"; } }
package wjl; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class BookTest { static Scanner sc=new Scanner(System.in); static List<Book> blist=new ArrayList<Book>(); public static void main(String[] args) { // TODO Auto-generated method stub Book b1=new Book(1, "唐诗三百首", 12); Book b2=new Book(2, "活着", 21); Book b3=new Book(3, "狼图腾", 25); blist.add(b1); blist.add(b2); blist.add(b3); System.out.println(blist); show(); } public static void show() { int menu = 0; while(true) { System.out.println("\n-------- 一、添加-二、删除-三、修改-四、查询 --------"); System.out.println("请输入你的选择:"); menu = sc.nextInt(); switch (menu) { case 1: add(); break; case 2: del(); break; case 3: update(); break; case 4: select(); break; default: System.out.println("没有这个选项!\n"); break; } } } public static void add() { System.out.println("请输入要添加的Id"); int id=sc.nextInt(); System.out.println("请输入要添加的书名"); String name=sc.next(); System.out.println("请输入要添加书的价格"); double price=sc.nextDouble(); Book b=new Book(id,name,price); blist.add(b); } public static void del() { System.out.println("请输入要删除书的Id"); int id=sc.nextInt(); for (Book b:blist) { if (b.getId() == id) { blist.remove(b); } } } public static void update() { System.out.println("请输入要修改的Id"); int id=sc.nextInt(); System.out.println("请输入要修改书的价格"); double price=sc.nextDouble(); for (Book b : blist) { if (b.getId()== id) { b.setPrice(price); } } } public static void select() { for (Book book : blist) { System.out.println(book); } } }
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import= "wjl.* "%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% List<Book> list = new ArrayList<Book>(); Book b1=new Book("唐诗三百首",12); Book b2=new Book("活着",21); Book b3=new Book("狼图腾",25); list.add(b1); list.add(b2); list.add(b3); for(Book b:list){ out.println(b); } %> </body> </html>