JSP第七次作业
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参
做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
package po; public class Book { private Integer id; private String name; private Integer price; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } public Book(Integer id, String name, Integer price) { super(); this.id = id; this.name = name; this.price = price; } public Book() { super(); } @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", price=" + price + "]"; } }
package po; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import jdk.nashorn.internal.runtime.regexp.joni.SearchAlgorithm; import sun.applet.Main; public class run{ static java.util.List<Book> list =new ArrayList<Book>(); static{ Book book1 =new Book(1, "活着", 10); Book book2 =new Book(2, "哈姆雷特", 100); Book book3 =new Book(3, "天才在左疯子在右", 30); list.add(book1); list.add(book2); list.add(book3); } /** * @param args * @return */ public static void main(String[] args) { // TODO Auto-generated method stub while(true){ System.out.println("1.添加"); System.out.println("2.删除"); System.out.println("3.修改"); System.out.println("4.查询"); System.out.println("0.退出程序"); System.out.println("请选择0-4"); Scanner scanner=new Scanner(System.in); int i=scanner.nextInt(); switch (i) { case 1: addbook(); System.out.println("是否返回主界面Y/N?"); String s=scanner.next(); if (s.equals("Y")) { break; } else { return; } case 2: delBoook(); System.out.println("是否返回主界面Y/N?"); s=scanner.next(); if (s.equals("Y")) { break; } else { return; } case 3: update(); System.out.println("是否返回主界面Y/N?"); s=scanner.next(); if (s.equals("Y")) { break; } else { return; } case 4: search(); System.out.println("是否返回主界面Y/N?"); s=scanner.next(); if (s.equals("Y")) { break; } else { return; } case 0: System.out.println("退出成功"); return; } } } private static void search() { // TODO Auto-generated method stub for (Book book4 : list) { System.out.println(book4); } } private static void update() { // TODO Auto-generated method stub System.out.println("请输入修改的图书id"); int update=new Scanner(System.in).nextInt(); for (Book book : list) { if (update==book.getId()) { System.err.println("请输入名称"); String b1=new Scanner(System.in).next(); System.err.println("请输入价格"); int c1=new Scanner(System.in).nextInt(); book.setName(b1); book.setPrice(c1); System.out.println("修改成功"); } } } private static void delBoook() { System.out.println("请输入你要删除的图书id"); 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) { // TODO: handle exception } } private static void addbook() { // TODO Auto-generated method stub Scanner scanner=new Scanner(System.in); System.err.println("请输入id"); int a=scanner.nextInt(); System.err.println("请输入名称"); String b=scanner.next(); System.err.println("请输入价格"); int c=scanner.nextInt(); Book book=new Book(a, b, c); list.add(book); System.out.println("添加成功"); } }
第九次作业
1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中。做一个菜单,可以添加,删除,修改,查询
package po;
public class Book {
private Integer id;
private String name;
private Integer price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Book(Integer id, String name, Integer price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
public Book() {
super();
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", price=" + price + "]";
}
}
package po;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import jdk.nashorn.internal.runtime.regexp.joni.SearchAlgorithm;
import sun.applet.Main;
public class run{
static java.util.List<Book> list =new ArrayList<Book>();
static{
Book book1 =new Book(1, "西游记", 10);
Book book2 =new Book(2, "三毛流浪记", 100);
Book book3 =new Book(3, "三国演义", 30);
list.add(book1);
list.add(book2);
list.add(book3);
}
/**
* @param args
* @return
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
while(true){
System.out.println("1.添加");
System.out.println("2.删除");
System.out.println("3.修改");
System.out.println("4.查询");
System.out.println("0.退出程序");
System.out.println("请选择0-4");
Scanner scanner=new Scanner(System.in);
int i=scanner.nextInt();
switch (i) {
case 1:
addbook();
System.out.println("是否返回主界面Y/N?");
String s=scanner.next();
if (s.equals("Y")) {
break;
}
else {
return;
}
case 2:
delBoook();
System.out.println("是否返回主界面Y/N?");
s=scanner.next();
if (s.equals("Y")) {
break;
}
else {
return;
}
case 3:
update();
System.out.println("是否返回主界面Y/N?");
s=scanner.next();
if (s.equals("Y")) {
break;
}
else {
return;
}
case 4:
search();
System.out.println("是否返回主界面Y/N?");
s=scanner.next();
if (s.equals("Y")) {
break;
}
else {
return;
}
case 0:
System.out.println("退出成功");
return;
}
}
}
private static void search() {
// TODO Auto-generated method stub
for (Book book4 : list) {
System.out.println(book4);
}
}
private static void update() {
// TODO Auto-generated method stub
System.out.println("请输入修改的图书id");
int update=new Scanner(System.in).nextInt();
for (Book book : list) {
if (update==book.getId()) {
System.err.println("请输入名称");
String b1=new Scanner(System.in).next();
System.err.println("请输入价格");
int c1=new Scanner(System.in).nextInt();
book.setName(b1);
book.setPrice(c1);
System.out.println("修改成功");
}
}
}
private static void delBoook() {
System.out.println("请输入你要删除的图书id");
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("删除成功