赞
踩
系统要求:
定义一个类Mybook,包括成员name(书名String)、price(价格
double)、press(出版社String)、author(作者String)、bookISBN(书的ISBN号String),定义一个类数组mybook books[200],编写一个函数inputData(ArrayList books]),n为书的数量,从键盘上输入n本书的信息,编写一个函数print(ArrayList books),输出全部书的信息。
增加查找功能,编写函数searchName(ArrayList books,String
name),根据书名name在数组books中查找是否存在此书,如果能找到,输出该书的详细信息,如果找不到,输出“此书不存在”。
增加删除功能,编写函数deleteName(ArrayList books,String
name),根据书名name在数组books中查找是否存在此书,如果存在,删除该书,并提示“此书删除成功。”,并显示该书的详细信息。如果不存在,输出“没有此书,删除失败.“
添加函数menu(),输出菜单项请选择你要进行的操作(请输入1-4中的任一个数字):1:添加图书2:删除图书3:查找图书(根据用户名)4:退出当用户输入1-3时,分别调用输入、删除和查找函数,当用户输入4时程序结束。
MyBook类
package com.test2; import java.util.ArrayList; import java.util.Scanner; public class Mybook { private String name; private Integer price; private String press; private String author; private String bookISBN; public Mybook(String name, Integer price, String press, String author, String bookISBN) { this.name = name; this.price = price; this.press = press; this.author = author; this.bookISBN = bookISBN; } public Mybook() { } public void inputData(ArrayList<Mybook> books) { System.out.println("/* 请输入录入图书的数量 */"); Scanner scanner = new Scanner(System.in);//创建一个键盘扫描类对象 int num = scanner.nextInt();//输入整型 for (int i = 0;i < num;i++) { Mybook mybook2 = new Mybook(); Scanner s2 = new Scanner(System.in); System.out.println("/* 请输入书名 */"); mybook2.setName(s2.next()); System.out.println("/* 请输入价格 */"); String str2 = s2.next(); mybook2.setPrice(Integer.parseInt(str2)); System.out.println("/* 请输入出版社 */"); mybook2.setPress(s2.next()); System.out.println("/* 请输入作者 */"); mybook2.setAuthor(s2.next()); System.out.println("/* 请输入ISBN */"); mybook2.setBookISBN(s2.next()); books.add(mybook2); } } public void pri(ArrayList<Mybook> books,int i) { System.out.println("书名:" + books.get(i).getName()); System.out.println("价格:" + books.get(i).getPrice()); System.out.println("出版社:" + books.get(i).getPress()); System.out.println("作者:" + books.get(i).getAuthor()); System.out.println("ISBN:" + books.get(i).getBookISBN()); } public void print(ArrayList<Mybook> books) { int num = books.size(); for (int i = 0;i<num;i++) { pri(books,i); } } public void searchName(ArrayList<Mybook> books,String name) { Scanner scanner = new Scanner(System.in); System.out.println("/* 请输入寻找的书名 */"); name = scanner.next();//输入你要找的书名 for (int i = 0;i < books.size();i++) { if (name.equals(books.get(i).getName())) { pri(books,i); break; }else { System.out.println("找不到该书"); } } } public void deleteName(ArrayList<Mybook> books,String name) { Scanner scanner = new Scanner(System.in); System.out.println("/* 请输入要删除的书名 */"); name = scanner.next();//输入你要找的书名 for (int i = 0;i < books.size();i++) { if (name.equals(books.get(i).getName())) { pri(books,i);//显示该书的信息 books.remove(i);//删除该书 System.out.println("此书删除成功。"); break; }else { System.out.println("没有此书,删除失败"); } } } 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 String getPress() { return press; } public void setPress(String press) { this.press = press; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getBookISBN() { return bookISBN; } public void setBookISBN(String bookISBN) { this.bookISBN = bookISBN; } }
主函数:
package com.test2; import java.util.ArrayList; import java.util.Scanner; public class test { public static void main(String[] args) { ArrayList<Mybook> books = new ArrayList<Mybook>(200);//类数组 大小为200 存储mybook变量 Mybook mybook = new Mybook();//new mybook对象 调用成员函数 String name = null; while (true) { System.out.println("/* 请输入数字 */"); System.out.println("/* 1.录入图书信息 */"); System.out.println("/* 2.打印图书信息 */"); System.out.println("/* 3.查找图书 */"); System.out.println("/* 4.删除图书 */"); System.out.println("/* 5.退出 */"); Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); switch (num) { case 1:mybook.inputData( books);break; case 2:mybook.print(books);break; case 3:mybook.searchName(books, name);break; case 4:mybook.deleteName(books,name);break; case 5:return; default: break; } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。