手记

java第三季—模拟图书系统

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人推荐
随时随地看视频
慕课网APP