//类
package com.book;
import java.util.*;
public class BookSystem
{
private String bookName;
private int bookId;
public BookSystem(int bookId, String bookName) throws Exception
{
this.bookId=bookId;
this.bookName=bookName;
}
public String getBookName()
{
return bookName;
}
public int getBookId()
{
return bookId;
}
//main 函数
public static void main(String[] args)
{
try
{
BookSystem [] Lib={new BookSystem(1, "Acient Chinese"),
new BookSystem(2, "Math"),
new BookSystem(3, "English"),
new BookSystem(4, "Politics"),
new BookSystem(5, "History"),
new BookSystem(6, "Music"),
new BookSystem(7, "Physics")};
System.out.println("1-find your book using bookId; 2-find your book using bookName");
Scanner input0=new Scanner(System.in);
String option=input0.next();
if(option.equals("1"))
{
System.out.println("Please input the bookId");
Scanner input=new Scanner(System.in);
int id=input.nextInt();
for(int i=0;i<Lib.length-1;i++)
{
if(id==Lib[i].getBookId())
{
System.out.println("book:"+Lib[i].bookName);
}
}
{
Exception e=new Exception();
throw new Exception("The book does not exist!");
}
}
else if(option.equals("2"))
{ System.out.println("Please input the bookName");
Scanner scanner=new Scanner(System.in);
String name=scanner.next();
for(int i=0;i<Lib.length-1;i++)
{
if(name.equals(Lib[i].getBookName()))
{
System.out.println("book:"+Lib[i].bookName);
}
}
{
Exception e=new Exception();
throw new Exception("This book does not exist");
}
}
else
{
throw new Exception("Please input number as indicated!!");
}
} catch (Exception e)
{
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦