package BookSystem;
public class Books
{
public int bid;
public String bname;
public Books()
{ }
public Books(int id,String name)
{
bid=id;
bname=name;
}
}```
package BookSystem;
import java.util.Scanner;
public class Test
{
static Books [] book={new Books(0,"计算机应用数学"),
new Books(1,"大学英语"),
new Books(2,"程序设计")};
private static Scanner input;
public static void main(String[] args)
{
input = new Scanner(System.in);
Test test1=new Test(); //用来调用对象
Books tempbook=new Books(); //用来保存从方法中返回的图书信息
System.out.println("欢迎使用图书管理系统");
while(true)
{
System.out.println("请选择图书查询方式(0-退出):1-按照书名查找图书;2-按照序号查找图书");
try {
int sel=input.nextInt();
if(0==sel)
{
System.out.println("谢谢您的使用,再见!");
return ;
}
switch(sel)
{
case 1:
System.out.print("请输入书名:");
tempbook=test1.bookname(book);
System.out.println("已找到book:序号 "+tempbook.bid+" 书名 "+tempbook.bname);
break;
case 2:
System.out.print("请输入序号:");
tempbook=test1.booknum(book);
System.out.println("已找到book:序号 "+tempbook.bid+" 书名 "+tempbook.bname);
break;
default:System.out.println("输入错误");
}
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
public Books bookname (Books[]book) throws Exception
{
String name=input.next();
for (int i=0;i<book.length;i++)
{
if(name.equals(book[i].bname))
{
return book[i];
}
}
throw new Exception("您输入的书籍不存在!");
}
public Books booknum(Books[]book) throws Exception
{
input = new Scanner(System.in);
int id=input.nextInt();
if (id>=0&&id<book.length)
{
return book[id];
}
throw new Exception ("输入的序号无匹配值");
}
}
点击查看更多内容
2人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦