第九周作业
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参
做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
package BookClass; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class Demo { public static void main(String[] args) { // List<Book> listbooks = new ArrayList<Book>(); Book book1 = new Book("格林童话", 12.0, 1); Book book2 = new Book("安徒生童话", 20.0, 2); Book book3 = new Book("西游记", 100.0, 3); listbooks.add(book1); listbooks.add(book2); listbooks.add(book3); System.out.print("输入数字1查看图书"+"输入数字2查看并修改图书图书"+"输入数字3查看并删除图书"+"输入数字0返回主菜单"); Scanner in = new Scanner(System.in); String indexS = in.next(); Integer index = Integer.valueOf(indexS); if(index==1) { for (Book book : listbooks) { System.out.println(book); } System.out.print("输入数字1查看图书"+"输入数字2查看并修改图书图书"+"输入数字3查看并删除图书"+"输入数字0返回主菜单"); Scanner in1 = new Scanner(System.in); String indexS1 = in1.next(); Integer index1 = Integer.valueOf(indexS1); } else if(index==3) { System.out.print("删除您所想要删除的图书"); Scanner in1 = new Scanner(System.in); String indexS1 = in1.next(); Integer index1 = Integer.valueOf(indexS1); for(int i=0;i<listbooks.size();i++) { if(i==index1) { listbooks.remove(i); } } for (Book book : listbooks) { System.out.println(book); } } else if(index==2) { System.out.print("修改您所想要增加的图书id:"); Scanner in4 = new Scanner(System.in); String indexS4 = in4.next(); Integer index4 = Integer.valueOf(indexS4); System.out.println("新书ID:"+index4); System.out.print("修改您所想要增加的图书书名:"); Scanner in5 = new Scanner(System.in); String bookname = in5.next(); System.out.println("新书书名:"+bookname); System.out.print("修改您所想要增加的图书价格为:"); Scanner in6 = new Scanner(System.in); String indexS6 = in6.next(); Double bookprice = Double.valueOf(indexS6); System.out.println("新书价格为:"+bookprice); listbooks.add(new Book(bookname, bookprice,index4 )); for (Book book : listbooks) { System.out.println(book); } System.out.print("找到您所要修改的图书:"); Scanner in7 = new Scanner(System.in); String indexS7 = in7.next(); Integer index7 = Integer.valueOf(indexS7); listbooks.remove(index7); System.out.print("修改您的图书id:"); Scanner in8 = new Scanner(System.in); String indexS8 = in8.next(); Integer index8= Integer.valueOf(indexS8); System.out.println("新书ID:"+index8); System.out.print("修改您图书书名:"); Scanner in9 = new Scanner(System.in); String bookname1 = in9.next(); System.out.println("新书书名:"+bookname1); System.out.print("修改您所想要增加的图书价格为:"); Scanner in10 = new Scanner(System.in); String indexS10 = in10.next(); Double bookprice1 = Double.valueOf(indexS10); System.out.println("新书价格为:"+bookprice1); listbooks.add(new Book(bookname1, bookprice,index4 )); for (Book book : listbooks) { System.out.println(book); } } else if(index==0) { System.out.println("输入指令有误"); } } }
2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。
<%@page import="ww.lcq.Book"%> <%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%> <%@page import="java.util.List"%> <%@page import="java.util.*"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% List<Book> listbooks = new ArrayList<Book>(); Book book1 = new Book("格林童话", 12.0, 1); Book book2 = new Book("安徒生童话", 20.0, 2); Book book3 = new Book("西游记", 100.0, 3); listbooks.add(book1); listbooks.add(book2); listbooks.add(book3); request.setAttribute("listbooks", listbooks); %> ${listbooks} </body> </html>
3.在MySQL中创建Book表,里面id,name,price,
用命令实现,
添加一个图书,
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书,
把20元以上的图书价格都修改为18.8,
查看全部图书,
查看价格高于10块钱的全部图书
create table t_book( bookid int primary key auto_increment, bookname varchar(255), bookprice double ) insert into t_book(bookname,bookprice ) values("安徒生童话",12.3); insert into t_book(bookname,bookprice ) values("白雪公主",20.0); insert into t_book(bookname,bookprice ) values("三国演义",9.0); insert into t_book(bookname,bookprice ) values("红楼梦",21.0); insert into t_book(bookname,bookprice ) values("格林童话",8.0);
select * from t_book where bookprice>20;
DELETE FROM t_book where bookname='安徒生童话';
UPDATE t_book SET bookprice=18.8;