赞
踩
图书馆需要引进新书,我们需要知道图书的基本属性,因此我们需要创建图书对象,并对其加以引用,并为了实现代码的工程化,可为各项操作创建不同的类,并实现相对应的方法,从而实现图书的增删查改。
1.新进图书基本信息的录入
2.图书基本信息的查询
3.查阅图书
4.归还图书**
如图所示,根据需求实现代码工程化
1.添加图书
public class addBook { public void work(Bookshelf bookshelf){ Scanner sc=new Scanner(System.in); System.out.println("新增图书:"); System.out.println("请输入图书书号:"); int ISBN=sc.nextInt(); sc.nextLine(); System.out.println("请输入您要添加的图书书名:"); String name=sc.nextLine(); System.out.println("请输入图书的作者:"); String author=sc.nextLine(); System.out.println("请输入图书的状态:"); String state=sc.nextLine(); int cnt1=bookshelf.getCurrCount(); int cnt2=bookshelf.getTotalCount(); Book book=new Book(ISBN,name,author,state); bookshelf.setBook(book); bookshelf.setCurrCount(cnt1+1); bookshelf.setTotalCount(cnt2+1); //return bookshelf; } }
2.借书操作
public class borrowBook { public void work(Bookshelf bookshelf){ System.out.println("请输入您想借的书:"); Scanner sc=new Scanner(System.in); int flag=0; String name=sc.nextLine(); for (int i = 0; i < bookshelf.getCurrCount(); i++) { if(name.equals(bookshelf.books[i].getName())){ flag=1; System.out.println("请输入归还时间:"); String returnTime= sc.nextLine(); bookshelf.books[i].setReturnTime(returnTime); bookshelf.books[i].setState("已借出"); break; } } if(flag==0) System.out.println("对不起,没有这本书!"); } }
3.展示图书
public class displayBook {
public void work(Bookshelf bookshelf){
System.out.println("显示所有图书");
for (int i = 0; i < bookshelf.getCurrCount(); i++) {
System.out.print("书号:"+bookshelf.books[i].getISBN());
System.out.print(" 书名:"+bookshelf.books[i].getName());
System.out.print(" 作者:"+bookshelf.books[i].getAuthor());
System.out.print(" 状态:"+bookshelf.books[i].getState());
if(bookshelf.books[i].getState()=="已借出"){
System.out.print(" 归还时间:"+bookshelf.books[i].getReturnTime());
}
System.out.println();
}
}
}
4.查找图书
public class searchBook { public void work(Bookshelf bookshelf){ System.out.println("请选择您查找的方式:"); System.out.println("1.按书名查询"); System.out.println("2.按作者查询"); System.out.println("3.按书号查询"); Scanner sc=new Scanner(System.in); int choice=sc.nextInt(); sc.nextLine(); int flag=0; if(choice==1){ System.out.println("请输入书名:"); String name=sc.nextLine(); for (int i = 0; i < bookshelf.getCurrCount(); i++) { if(name.equals(bookshelf.books[i].getName())){ flag=1; System.out.print("书号:"+bookshelf.books[i].getISBN()); System.out.print(" 书名:"+bookshelf.books[i].getName()); System.out.print(" 作者:"+bookshelf.books[i].getAuthor()); System.out.print(" 状态:"+bookshelf.books[i].getState()); System.out.println(); break; } } if(flag==0) System.out.println("对不起,没有这本书!"); } else if(choice==2){ System.out.println("请输入作者名:"); String author=sc.nextLine(); for (int i = 0; i < bookshelf.getCurrCount(); i++) { if(author.equals(bookshelf.books[i].getAuthor())){ flag=1; System.out.print("书号:"+bookshelf.books[i].getISBN()); System.out.print(" 书名:"+bookshelf.books[i].getName()); System.out.print(" 作者:"+bookshelf.books[i].getAuthor()); System.out.print(" 状态:"+bookshelf.books[i].getState()); System.out.println(); break; } } if(flag==0) System.out.println("对不起,没有这本书!"); } else{ System.out.println("请输入书号:"); int ISBN=sc.nextInt(); for (int i = 0; i < bookshelf.getCurrCount(); i++) { if(ISBN==bookshelf.books[i].getISBN()){ flag=1; System.out.print("书号:"+bookshelf.books[i].getISBN()); System.out.print(" 书名:"+bookshelf.books[i].getName()); System.out.print(" 作者:"+bookshelf.books[i].getAuthor()); System.out.print(" 状态:"+bookshelf.books[i].getState()); System.out.println(); break; } } if(flag==0) System.out.println("对不起,没有这本书!"); } } }
5.归还图书
public class returnBook { public void work(Bookshelf bookshelf){ System.out.println("请输入您想还的书:"); Scanner sc=new Scanner(System.in); int flag=0; String name=sc.nextLine(); for (int i = 0; i < bookshelf.getCurrCount(); i++) { if(name.equals(bookshelf.books[i].getName())){ if(bookshelf.books[i].getState()=="未借出"){ System.out.println("该书未被借出"); } flag=1; bookshelf.books[i].setState("未借出"); break; } } if(flag==0) System.out.println("对不起,没有这本书!"); } }
6.退出系统
public class exitSystem {
public void exitSystem(){
System.out.println("退出成功!");
System.exit(0);
}
}
Java初学者首次实例,如有错误还请谅解!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。