jsp7
作者:互联网
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
1 package text; 2 3 public class Book { 4 private String id; 5 private String name; 6 private double price; 7 public String getId() { 8 return id; 9 } 10 public void setId(String id) { 11 this.id = id; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 public double getPrice() { 20 return price; 21 } 22 public void setPrice(double price) { 23 this.price = price; 24 } 25 public Book(String id, String name, double price) { 26 super(); 27 this.id = id; 28 this.name = name; 29 this.price = price; 30 } 31 public Book() { 32 super(); 33 } 34 35 }
1 package text; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Scanner; 6 7 public class BookTest { 8 static List<Book> listbooks = new ArrayList<Book>(); 9 10 /** 11 * @param args 12 */ 13 public static void main(String[] args) { 14 // TODO Auto-generated method stub 15 creatBookList(); 16 menu(); 17 18 } 19 20 public static void creatBookList() { 21 // TODO Auto-generated method stub 22 Book b1 = new Book("1", " JavaScript ", 59.8); 23 listbooks.add(b1); 24 Book b2 = new Book("2", " Android移动应用 ", 49.9); 25 listbooks.add(b2); 26 Book b3 = new Book("3", " 信息系统分析与设计", 49.8); 27 listbooks.add(b3); 28 } 29 30 public static void menu() { 31 for (;;) { 32 System.out.println("1.添加图书"); 33 System.out.println("2.修改图书"); 34 System.out.println("3.删除图书"); 35 System.out.println("4.查询图书"); 36 System.out.println("5.退出程序"); 37 int i = inputInt("请选择你的操作:"); 38 switch (i) { 39 case 1: 40 addBook(); 41 break; 42 case 2: 43 updateBook(); 44 break; 45 case 3: 46 delBook(); 47 break; 48 case 4: 49 findBook(); 50 break; 51 case 5: 52 return; 53 default: 54 System.out.println("输入错误,请重新输入"); 55 break; 56 } 57 } 58 } 59 60 private static void findBook() { 61 // TODO Auto-generated method stub 62 System.out.println("id 图书名称 图书价格"); 63 for (Book book : listbooks) { 64 System.out.println(book.getId() + " " + book.getName() + " " 65 + book.getPrice()); 66 } 67 68 } 69 70 private static void delBook() { 71 // TODO Auto-generated method stub 72 String id = inputStr("请输入要删除图书的id"); 73 for (int i = 0; i < listbooks.size(); i++) { 74 Book book = listbooks.get(i); 75 if (id.equals(book.getId())) { 76 listbooks.remove(i); 77 } 78 } 79 80 } 81 82 private static void updateBook() { 83 // TODO Auto-generated method stub 84 String id = inputStr("请输入要修改的图书id"); 85 for (int i = 0; i < listbooks.size(); i++) { 86 Book book = listbooks.get(i); 87 if (id.equals(book.getId())) { 88 book.setName(inputStr("请输入修改后的图书名称:")); 89 book.setPrice(inputDouble("请输入修改后的图书价格")); 90 listbooks.remove(i); 91 listbooks.add(i, book); 92 } 93 } 94 95 } 96 97 private static void addBook() { 98 // TODO Auto-generated method stub 99 String id = inputStr("请输入要添加的图书id"); 100 String name = inputStr("请输入要添加的图书名称"); 101 double price = inputDouble("请输入要添加的图书价格"); 102 Book book = new Book(id, name, price); 103 listbooks.add(book); 104 105 } 106 107 public static String inputStr(String msg) { 108 System.out.print(msg); 109 return new Scanner(System.in).nextLine(); 110 } 111 112 public static int inputInt(String msg) { 113 System.out.print(msg); 114 return new Scanner(System.in).nextInt(); 115 } 116 117 public static double inputDouble(String msg) { 118 System.out.print(msg); 119 return new Scanner(System.in).nextDouble(); 120 } 121 122 }
2.上题的类,在一个JSP页面中,创建一个集合,里面放3个图书,集合循环遍历显示在页面上。
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@page import="com.gd.entity.Book"%> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <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块钱的全部图书
标签:String,System,id,Book,jsp7,public,图书 来源: https://www.cnblogs.com/yanggongbo/p/16212654.html