第九周作业
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
package packk; 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() { super(); } public book(int id, String name, double price) { super(); this.id = id; this.name = name; this.price = price; } }
package packk; import java.util.ArrayList; import java.util.Scanner; public class test { static ArrayList<book> list=new ArrayList<book>(); static Scanner sc=new Scanner(System.in); public static void main(String[] args) { book book1=new book(001,"昨日今昔",21); book book2=new book(002,"我的家乡",12); book book3=new book(003,"Java之路",8); list.add(book1); list.add(book2); list.add(book3); for (int i = 0; i < list.size(); i++) { book b=list.get(i); System.out.println("id为"+b.getId()+" , "+b.getName()+" ¥价格为"+b.getPrice()); } showmain(); } private static void showmain() { // TODO Auto-generated method stub System.out.println("1.添加图书"); System.out.println("2.删除图书"); System.out.println("3.修改图书"); System.out.println("4.查询图书"); System.out.println("请选择你的操作"); int num=sc.nextInt(); switch(num){ case 1:add();break; case 2:delete();break; case 3:uodate();break; case 4:select();break; } } private static void select() { // TODO Auto-generated method stub System.out.println("请输入图书id"); int id=sc.nextInt(); for (int i = 0; i < list.size(); i++) { if (id == list.get(i).getId()) { System.out.println("查询结果为"); book b1=list.get(i); System.out.println("id为"+b1.getId()+" , "+b1.getName()+" ¥价格为"+b1.getPrice()); } } showmain(); } private static void uodate() { // TODO Auto-generated method stub System.out.println("请输入图书id"); int id=sc.nextInt(); for (int i = 0; i < list.size(); i++) { if (id == list.get(i).getId()) { list.remove(i); System.out.println("请输入图书name"); String name=sc.next(); System.out.println("请输入图书price"); int price=sc.nextInt(); book b=new book(); b.setId(id); b.setName(name); b.setPrice(price); list.add(b); System.out.print("操作成功为"); f(); } } showmain(); } private static void delete() { // TODO Auto-generated method stub System.out.println("请输入图书id"); int id=sc.nextInt(); for (int i = 0; i < list.size(); i++) { if (id == list.get(i).getId()) { list.remove(i); System.out.println("删除成功"); } } f(); showmain(); } private static void add() { // TODO Auto-generated method stub System.out.println("请输入图书id"); int id=sc.nextInt(); System.out.println("请输入图书name"); String name=sc.next(); System.out.println("请输入图书price"); int price=sc.nextInt(); book b=new book(id,"name",price); list.add(b); f(); showmain(); } private static void f() { System.out.println("图书列表最新版本为"); for (int i = 0; i < list.size(); i++) { book b1=list.get(i); System.out.println("id为"+b1.getId()+" , "+b1.getName()+" ¥价格为"+b1.getPrice()); } } }
2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。
<%@page import="packk.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(001,"昨日今昔",21); book book2=new book(002,"我的家乡",12); book book3=new book(003,"Java之路",8); 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.在MySQL中创建Book表,里面id,name,price,
用命令实现,
添加一个图书,
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书,
把20元以上的图书价格都修改为18.8,
查看全部图书,
查看价格高于10块钱的全部图书
# 建表 create table book( id double(100,10) primary key auto_increment, name varchar(10) not null , price double(100,10) ); #插入数据 insert into book (name, price) values ('昨日今昔',21),('我的家乡',12),('Java之路',8); insert into book (name, price) values ('不一样的成长',15),('我你他',6); insert into book (name, price) values ('中国简史',25); #查询价格大于10的书籍信息 select * from book where price>10; #查询所有书籍信息 select * from book; #将价格大于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;