先看设计要求:
下面给出两个报告、几个程序的参考
报告如下:
再附一个可以参考的验优报告:
再附一个可以参考的程序:
package ZuoYe;
import java.util.*;
class Book {
private String name;
private int num;
private int ISBN;
private String author;
private String publisher;
private int publishYear;
private int price;
public Book(String name,int num,int ISBN,String author,String publisher,int publishYear,int price)
{
this.name=name;
this.num=num;
this.ISBN=ISBN;
this.author=author;
this.publisher=publisher;
this.publishYear=publishYear;
this.price=price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getISBN() {
return ISBN;
}
public void setISBN(int iSBN) {
ISBN = iSBN;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getPublishYear() {
return publishYear;
}
public void setPublishYear(int publishYear) {
this.publishYear = publishYear;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
class NoExistException extends Exception
{/**
*
*/
private static final long serialVersionUID = 1L;
public NoExistException(String message)
{
super(message);
}
}
public class bookinfo {
Book[] book = {new Book("样本图书",1,10001,"鲁迅","江苏出版社",2019,20)};//定义对象数组,先初始化一个实例
public List<Book> listbooks;
public bookinfo() {
this.listbooks = new ArrayList<Book>();//定义Book为一个ArrayList的集合
}
public void listBooksAdd() {
listbooks.addAll(Arrays.asList(book));}//用arrays.list将数组book转化为集合
public void printBooks() {//输出格式
System.out.println("---------- 欢迎使用图书管理系统 ----------");
System.out.println("当前图书列表如下:");
System.out.println("书名" + "\t" + "序号"+ "\t" + "ISBN号"+ "\t" + "作者"+ "\t" + "出版社"+ "\t" + "出版年"+ "\t" + "价格");
for (Book bk : listbooks) {//for each循环
System.out.println(bk.getName() + "\t" +bk.getNum() + "\t" + bk.getISBN()+ "\t" + bk.getAuthor()+ "\t" + bk.getPublisher()+ "\t" + bk.getPublishYear()+ "\t" + bk.getPrice());
}
int listSize = listbooks.size();//获取列表中元素的个数
System.out.println("当前图书总数:"+listSize);
}
public static void main(String[] args) {
bookinfo fb=new bookinfo();
fb.listBooksAdd();
fb.printBooks();//调用listBooksAdd和printBooks两个方法
System.out.println();
while(true){
Scanner input = new Scanner(System.in);
System.out.println("请输入您的命令:1.新增图书 2.删除图书 3.修改图书 4.按名称查找 5.按序号查找 ");
int in=input.nextInt();
switch(in){//用switch-case语句分类处理5个功能
case 1: {//新增图书
System.out.println("-----请输入图书相关信息----");
Scanner sc = new Scanner(System.in);
System.out.println("请输入图书名称:");
String name=sc.next();
System.out.println("请输入图书序号:");
int num=sc.nextInt();
System.out.println("请输入图书ISBN:");
int ISBN = sc.nextInt();
System.out.println("请输入图书作者:");
String author = sc.next();
System.out.println("请输入图书出版社:");
String publisher = sc.next();
System.out.println("请输入图书出版年:");
int publishYear = sc.nextInt();
System.out.println("请输入图书价格:");
int price = sc.nextInt();
Book[] booknew= {new Book(name,num,ISBN,author,publisher,publishYear,price)};
fb.listbooks.addAll(Arrays.asList(booknew));
fb.printBooks();
break;
}//新增图书是用户输入,不会发生异常,所以case1不需要异常处理
case 2://删除图书,按照编号进行删除
try{
System.out.println(fb.fbdd());//调用fbdd方法执行删除图书
fb.printBooks();
int listSize = fb.listbooks.size();//获取列表中元素的个数
System.out.println("当前图书总数:"+listSize);
break;//如果找到该图书编号则进行删除,直接break,找不到则会执行catch中的内容进行报错
}catch(Exception e){//如果输入的编号不存在,则会提示报错
System.out.println(e.getMessage());
continue;
}
case 3://修改图书,按照编号进行修改
try{
System.out.println(fb.fbedit());
fb.printBooks();
break;
}catch(Exception e){
System.out.println(e.getMessage());
continue;}
case 4://按照名称查找图书
try{
System.out.println("找到图书: "+fb.fbbn());
break;
}catch(Exception e){
System.out.println(e.getMessage());
continue;}
case 5://按序号查找图书
try{
System.out.println("请输入书的序号");
Scanner sc = new Scanner(System.in);
int id=sc.nextInt();
System.out.println("找到图书: "+fb.fbid(id));
break;
}catch(Exception e){
System.out.println(e.getMessage());
continue;}
}
}
}
//fbid方法判断输入的图书序号是否存在
public String fbid(int id)throws NoExistException{
for(int i = 0; i < listbooks.size(); i++){
if(id==listbooks.get(i).getNum()){
return "图书名称为:"+book[id-1].getName()+" 图书序号为:"+book[id-1].getNum()+" ISBN号为:"+book[id-1].getISBN()+" 图书作者为:"+book[id-1].getAuthor()+" 图书出版社为:"+book[id-1].getPublisher()+" 图书出版年为:"+book[id-1].getPublishYear()+" 图书价格为:"+book[id-1].getPrice();
}
}
throw new NoExistException("序号越界,此书籍不存在!!!");}
//fbbn方法判断输入的图书名称是否存在
public String fbbn()throws NoExistException{
System.out.println("请输入书的名称");
Scanner scnn = new Scanner(System.in);
String bname=scnn.next();
for(Book bk:listbooks){
if(bname.equals(bk.getName())){
return bk.getName();
}
}
throw new NoExistException("名称错误,此书籍不存在!!!");}
//fbdd方法:如果输入书的序号存在,则将其删除
public String fbdd()throws NoExistException{
System.out.println("请输入书的序号");
Scanner s = new Scanner(System.in);
int dd=s.nextInt();
for(int i = 0; i < listbooks.size(); i++){
if(dd==listbooks.get(i).getNum()){
listbooks.remove(i);
return "成功删除"+dd+"编号的图书";
}
}
throw new NoExistException("序号越界,此书籍不存在!!!");}
//fbedit方法:如果输入书的序号存在,则修改它的信息
public String fbedit()throws NoExistException{
System.out.println("请输入要修改图书的序号");
Scanner scn = new Scanner(System.in);
int e=scn.nextInt();
for(Book ed : listbooks){
if(e==ed.getNum()){
System.out.println("请输入新的图书名称:");
String newName=scn.next();
System.out.println("请输入图书新的序号:");
int num = scn.nextInt();
System.out.println("请输入图书新的ISBN:");
int ISBN = scn.nextInt();
System.out.println("请输入图书作者:");
String author = scn.next();
System.out.println("请输入图书出版社:");
String publisher = scn.next();
System.out.println("请输入图书出版年:");
int publishYear = scn.nextInt();
System.out.println("请输入图书出版年:");
int price = scn.nextInt();
ed.setName(newName);
ed.setNum(num);
ed.setISBN(ISBN);
ed.setAuthor(author);
ed.setPublisher(publisher);
ed.setPublishYear(publishYear);
ed.setPrice(price);
}
return "成功修改"+e+"编号的图书";
}
throw new NoExistException("序号越界,此书籍不存在!!!");}
}
再给一个参考程序,
按顺序从上往下放出来:
一、
package library;
import java.util.Scanner;
public class Application {
private Ibook ibook;
Scanner scn=new Scanner(System.in);
public Application() {
}
public Application(Ibook ibook) {
this.ibook = ibook;
}
public Ibook getIbook() {
return ibook;
}
public void setIbook(Ibook ibook) {
this.ibook = ibook;
}
}
二、
package library;
public class Book {
private String name;
private String ISBN;
private String author;
private String publish;
private int date;
private float price;
private String category;
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(String name, String iSBN, String author, String publish, int date, float price, String category) {
super();
this.name = name;
ISBN = iSBN;
this.author = author;
this.publish = publish;
this.date = date;
this.price = price;
this.category = category;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String iSBN) {
ISBN = iSBN;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
public int getDate() {
return date;
}
public void setDate(int date) {
this.date = date;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String toString() {
return "Book " + name +"\n"+ "ISBN: " + ISBN + "\n"+"writer: " +author +"\n"+ "publish: " + publish + "\n"+"date "
+ date +"\n"+ "price: " + price +"\n"+ " category: " + category ;
}
}
三、
package library;
public interface Ibook {
public boolean addBooks(Book books);
public boolean research(String ab);
public void changeBooks(Book book);
}
四、
package library;
public class IbookForArray implements Ibook {
private Book[] books=new Book[50];
private int count=0;
public IbookForArray() {
super();
// TODO Auto-generated constructor stub
}
public IbookForArray(Book[] books) {
this.books = books;
}
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
//新书录入
public boolean addBooks(Book book) {
if(count==books.length)
return false;
for(int i=0;i<count;i++)
if(books[i].getName().equals(book.getName()))
return false;
books[count]=book;
count++;
return true;
}
//查询是否有某本书
public boolean research(String ab) {
int i;
for(i=0;i<count;i++)
if(books[i].getName().equals(ab)||books[i].getISBN().equals(ab)) {
System.out.println(books[i].toString());
return true;
}
return false;
}
public void changeBooks(Book book) {
for(int i=0;i<count;i++)
if(books[i].getName().equals(book.getName())) {
books[i]=book;
System.out.println(books[i].toString());
}
}
}
五、
package library;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Ibook ac=new IbookForArray();
int j=1;
Application ad=new Application(ac);
Scanner scn=new Scanner(System.in);
while(j==1) {
System.out.println("选择你需要进行的操作:");
System.out.println("1.add 2.research 3.change");
int a=scn.nextInt();
String name;
String ISBN;
String author;
String publish;
int date;
float price;
String category;
if(a==1) {
System.out.println("请输入图书的名字");
name=scn.next();
System.out.println("请输入图书的ISBN");
ISBN=scn.next();
System.out.println("请输入图书的作者");
author=scn.next();
System.out.println("请输入图书的出版社");
publish=scn.next();
System.out.println("请输入图书的出版年");
date=scn.nextInt();
System.out.println("请输入图书的价格");
price=scn.nextFloat();
System.out.println("请输入图书的类别");
category=scn.next();
Book bo=new Book();
bo.setName(name);
bo.setISBN(ISBN);
bo.setAuthor(author);
bo.setPublish(publish);
bo.setDate(date);
bo.setPrice(price);
bo.setCategory(category);
ac.addBooks(bo);
}
if(a==2) {
System.out.println("请输入书名号或者ISBN");
String ab=scn.next();
ac.research(ab);
}
if(a==3) {
System.out.println("请输入要修改信息的图书名字");
String nam=scn.next();
if(ac.research(nam)){
System.out.println("请输入图书的ISBN");
ISBN=scn.next();
System.out.println("请输入图书的作者");
author=scn.next();
System.out.println("请输入图书的出版社");
publish=scn.next();
System.out.println("请输入图书的出版年");
date=scn.nextInt();
System.out.println("请输入图书的价格");
price=scn.nextFloat();
System.out.println("请输入图书的类别");
category=scn.next();
Book bo=new Book(nam,ISBN,author,publish,date,price,category);
ac.changeBooks(bo);
}
}
System.out.println("是否继续?"+"\t"+"1.继续 2.结束");
j=scn.nextInt();
}
}
}