//构造方法
public class bookborrow {
book[] books= {new book(1,"高数"),new book(2,"线性代数"),new book(3,"大学物理B")};
Scanner input=new Scanner(System.in);
public void xuhao() {
int a=input.nextInt();
int i;
try {
for(i=0;i<books.length;i++) {
if(a==books[i].getId()) {
System.out.println("图书名称:"+books[i].getName());
borrow();
break;
}
else {
System.out.println("你输入的序号不正确!");
borrow();
}
}
}
catch(Exception e) {
}
}
public void mingcheng() {
String b=input.next();
int i;
try {
for(i=0;i<books.length;i++) {
if(b.equals(books[i].getName())) {
System.out.println("图书序号:"+books[i].getId());
borrow();
break;
}
else {
System.out.println("输入错误!!!");
borrow();
}
}
}
catch(Exception e) {
}
}
public void borrow() {
System.out.println("输入命令:1.按名称查找图书 2.按序号查找图书:3.退出");
String c=input.next();
if(c.equals("1")) {
System.out.println("请输入图书名称:");
mingcheng();
}
else if(c.equals("2")){
System.out.println("请输入序号:");
xuhao();
}
else if(c.equals("3")){
input.close();
}
else {
System.out.println("输入错误,请重新输入!!!");
borrow();
}
}
//程序入口
public static void main(String[] args) {
// TODO Auto-generated method stub
bookborrow p=new bookborrow();
p.borrow();
}
}
//book类
public class book {
private int id;
private String name;
public book(int id,String name) {
this.id=id;
this.name=name;
}
public int getId() {
return id;
}
public void SetId(int id) {
this.id=id;
}
public String getName() {
return name;
}
public void SetName(String name) {
this.name=name;
}
}
功能实现如图