JSP第九周作业
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
public class Book { int id; String name; double price; public Book() { super(); } public Book(int id, String name, double price) { super(); this.id = id; this.name = name; this.price = 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; } }
import java.util.ArrayList; import java.util.Scanner; public class main { static ArrayList<Book> booklist=new ArrayList<Book>(); static Scanner sc=new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub Book book1=new Book(100,"三国演义",20); Book book2=new Book(200,"红楼梦",30); Book book3=new Book(300,"水浒传",10); booklist.add(book1); booklist.add(book2); booklist.add(book3); for (int i = 0; i <booklist.size(); i++) { Book b= booklist.get(i); System.out.println("图书编号:"+b.getId()+",图书名称:"+b.getName()+",图书价格"+b.getPrice()); } System.out.println("==========================="); shouye(); } public static void shouye() { System.out.println("请输入你需要的选项:"); System.out.println("1.添加图书"); System.out.println("2.删除图书"); System.out.println("3.修改图书"); System.out.println("4.查询图书"); System.out.println("==========================="); caidan(); } public static void caidan() { int num=sc.nextInt(); switch (num) { case 1: tianjia(); break; case 2: shanchu(); break; case 3: xiugai(); break; case 4: chaxun(); break; default: break; } } private static void chaxun() { // TODO Auto-generated method stub System.out.println("请输入要查询图书名称:"); String bookname=sc.next(); for (int i = 0; i < booklist.size(); i++) { if(booklist.get(i).getName().equals(bookname)){ System.out.println("==========================="); Book b= booklist.get(i); System.out.println("图书编号:"+b.getId()+",图书名称:"+b.getName()+",图书价格"+b.getPrice()); } } } private static void xiugai() { // 修改图书信息 System.out.println("请输入要修改的图书id:"); int bookid=sc.nextInt(); for (int i = 0; i < booklist.size(); i++) { if(booklist.get(i).getId()==bookid){ booklist.remove(i); System.out.println("请输入新的图书编号:"); int id=sc.nextInt(); System.out.println("请输入新的图书名称:"); String name=sc.next(); System.out.println("请输入新的图书价格:"); double price=sc.nextDouble(); Book newbook=new Book(id,name,price); booklist.add(newbook); } } System.out.println("图书信息修改完成!"); System.out.println("==========================="); mulu(); shouye(); } private static void shanchu() { // 删除图书 System.out.println("请输入要删除的图书名称:"); String bookname=sc.next(); for (int i = 0; i < booklist.size(); i++) { if(booklist.get(i).getName().equals(bookname)){ booklist.remove(i); } } System.out.println("删除成功!"); System.out.println("==========================="); mulu(); shouye(); } private static void tianjia() { //添加图书 System.out.println("请输入图书编号:"); int id=sc.nextInt(); System.out.println("请输入图书名称:"); String name=sc.next(); System.out.println("请输入图书价格:"); double price=sc.nextDouble(); Book newbook=new Book(id,name,price); booklist.add(newbook); System.out.println("新图书添加完成!"); System.out.println("==========================="); mulu(); shouye(); } private static void mulu() { // 更新后图书目录 for (int i = 0; i <booklist.size(); i++) { Book b= booklist.get(i); System.out.println("图书编号:"+b.getId()+",图书名称:"+b.getName()+",图书价格"+b.getPrice()); } System.out.println("==========================="); } }
2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。
<html> 5 <head> 6 </head> 7 8 <body> 9 <% 10 List<Book> list = new ArrayList<Book>(); 11 Book b1=new Book(1 "JavaScript " 59.8); 12 Book b2=new Book(2 "Android移动应用 " 49.9); 13 Book b3=new Book(3 "信息系统分析与设计" 49.8); 14 list.add(b1); 15 list.add(b2); 16 list.add(b3); 17 for (int i = 0; list != null && i < list.size(); i++) { 18 out.print(list.get(i).getId() + "." + list.get(i).getName() 19 + "," + list.get(i).getPrice() + "<br>"); 20 } 21 %> 22 </body> 23 </html>
3.在MySQL中创建Book表,里面id,name,price,
用命令实现,
添加一个图书,
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书,
把20元以上的图书价格都修改为18.8,
查看全部图书,
查看价格高于10块钱的全部图书