第九周作业
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参
做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
package book; import java.util.ArrayList; import java.util.List; public class Book { private int id; private String name; private double 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; } public Book(int i, String name, double price) { super(); this.id = i; this.name = name; this.price = price; } public Book() { super(); // TODO Auto-generated constructor stub } }
package book; import java.util.List; import java.util.ArrayList; import java.util.Scanner; public class sc { static java.util.List<Book> list=new ArrayList<Book>(); static{ Book book1=new Book(1,"骆驼祥子",55); Book book2=new Book(2,"我的爱人",18); Book book3=new Book(3,"老人与海",35); list.add(book1); list.add(book2); list.add(book3); } 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("请选择"); Scanner scanner=new Scanner(System.in); int a=scanner.nextInt(); switch (a) { case 1: add(); break; case 2: del(); break; case 3: change(); break; case 4: search(); break; case 5: System.out.println("退出成功"); return; } } } private static void search(){ for(Book book4:list){ System.out.println(book4); } } private static void change(){ System.out.println("请输入需要修改的图书编号"); int update=new Scanner(System.in).nextInt(); for(Book book:list){ if(update==book.getId()){ System.err.println("请输入书名"); String name1=new Scanner(System.in).next(); System.err.println("请输入价格"); int price1=new Scanner(System.in).nextInt(); book.setName(name1); book.setPrice(price1); System.out.println("修改成功"); } } } private static void del(){ System.out.println("请输入删除的图书编号"); int id=new Scanner(System.in).nextInt(); try{ for(Book book:list){ if(list.size()!=0){ if(id==book.getId()){ list.remove(book); System.out.println("删除成功"); } } } }catch(Exception e){ } } private static void add(){ Scanner scanner=new Scanner(System.in); System.err.println("请输入编号"); int idd=scanner.nextInt(); System.err.println("请输入书名"); String name2=scanner.next(); System.err.println("请输入价格"); int price2=scanner.nextInt(); Book book=new Book(idd,name2,price2); list.add(book); System.out.println("添加成功"); } }
.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。
<%@page import="book.Book"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head></head> <body> 图书:<br> <% ArrayList<Book> list=new ArrayList<Book>(); Book book1=new Book(1,"骆驼祥子",55); Book book2=new Book(2,"我的爱人",18); Book book3=new Book(3,"老人与海",35); list.add(book1); list.add(book2); list.add(book3); for (int i = 0; i < list.size(); i++) { Book b=list.get(i); int id=b.getId(); String name=b.getName(); double price=b.getPrice(); out.print("id:"+id+" , 书名为:"+name+" ,价格为:"+price); %><br><% } %> </body> </html>
3
#查询所有书籍信息
SELECT*
FROM Book
#查询价格大于10的书籍信息
SELECT*
FROM Book
WHERE price>10
#将价格大于20的书籍的价格改为18.8
update book set price=18.8 where price>20;
#删除我开头的书
DELETE
From Book
WHERE name like'我%'
#根据名称删除图书
DELETE
FROM Book
WHERE name='老人与海'
#删除所有书籍信息
DELETE from book;
#删除表
DROP table book;