jsp第九周作业
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
package Cun; public class Cun { 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 Cun(int id,String name, double price) { super(); this.id=id; this.name = name; this.price = price; } public Cun(){ // TODO Auto-generated constructor stub } public void print() { } }
package Cun; import java.util.ArrayList; import java.util.Scanner; public class zhu { static ArrayList<Cun> list = new ArrayList<Cun>(); static Scanner ce = new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub Cun book1 = new Cun(1, "青年1", 77); book1.print(); Cun book2 = new Cun(2, "青年2", 88); book2.print(); Cun book3 = new Cun(3, "青年3", 99); book3.print(); list.add(book1); list.add(book2); list.add(book3); for (int i = 0; i < list.size(); i++) { Cun shu1 = list.get(i); System.out.println("id为" + shu1.getId() + ",名称为" + shu1.getName() + ",价格为" + shu1.getPrice()); } show(); } private static void show() { // 展示 System.out.println("请输入数字"); System.out.println("1、添加"); System.out.println("2、删除"); System.out.println("3、修改"); System.out.println("4、查询"); int xuan = ce.nextInt(); switch (xuan) { case 1: add(); break; case 2: delete(); break; case 3: update(); break; case 4: select(); break; } } private static void add() { // 添加 System.out.println("输入要添加图书的id"); int id = ce.nextInt(); System.out.println("输入要添加图书的名称"); String name = ce.next(); System.out.println("输入要添加图书的价格"); int price = ce.nextInt(); Cun shu1 = new Cun(id, name, price); list.add(shu1); System.out.println("添加的图书id为" + shu1.getId() + ",名称为" + shu1.getName() + ",价格为" + shu1.getPrice()); show(); } private static void delete() { // 删除 System.out.println("请输入要删除图书的id"); int id = ce.nextInt(); for (int i = 0; i < list.size(); i++) { if (id == list.get(i).getId()) { list.remove(i); System.out.println("删除成功"); } } show(); } private static void update() { // 修改 System.out.println("输入要修改图书的id"); int id = ce.nextInt(); for (int i = 0; i < list.size() - 1; i++) { if (id == list.get(i).getId()) { list.remove(i); System.out.println("输入修改后的名称"); String name = ce.next(); System.out.println("输入修改后的价格"); int price = ce.nextInt(); Cun shu1 = new Cun(id, name, price); System.out.println("id为" + shu1.getId() + ",修改后的名称为" + shu1.getName() + ",价格为" + shu1.getPrice()); list.add(shu1); } } show(); } private static void select() { // 查询 System.out.println("请输入要查询图书的id"); int id = ce.nextInt(); for (int i = 0; i < list.size(); i++) { if (id == list.get(i).getId()) { Cun shu2 = list.get(i); System.out.println("id为" + shu2.getId() + ",名称为" + shu2.getName() + ",价格为" + shu2.getPrice()); } } show(); } }
2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" import="Cun.Cun"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>index.jsp</title> </head> <body> <% ArrayList<Cun> list=new ArrayList<Cun>(); Cun book1=new Cun(1,"青年1",77); Cun book2=new Cun(2,"青年2",88); Cun book3=new Cun(3,"青年3",99); list.add(book1); list.add(book2); list.add(book3); for (int i = 0; i < list.size(); i++) { Cun shu=list.get(i); int id=shu.getId(); String name=shu.getName(); double price=shu.getPrice(); out.print("id为"+id+",书名为"+name+",价格为"+price+"<br>"); } %> </body> </html>
3.在MySQL中创建Book表,里面id,name,price,
用命令实现,
添加一个图书,
根据名称删除图书,
把所有名称是“我”开头的图书删除,
删除全部图书,
把20元以上的图书价格都修改为18.8,
查看价格高于10块钱的全部图书
创建表
查看全部图书