亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

Java控制臺圖書查詢

標簽:
Java
package com.library;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
public class FindBook {
    private static Scanner in = new Scanner(System.in);
    private static HashMap<String,String> bMap = new HashMap<String,String>();
    public static void main(String[] args) {
        createBook();
        try {
            do {
            find(findType());
            }while(isGoingOn().equals("1"));
        } catch (CommandException e) {
            System.out.println("请输入正确命令");
            new FindBook();
            FindBook.main(args);
        }
    }

    public static void createBook() {
        bMap.put("1", "Java");
        bMap.put("2", "C++");
        bMap.put("3", "Python");
        System.out.println("图书馆共有"+bMap.size()+"本书。");
    }

    public static String findType() throws CommandException{
        System.out.println("请输入查询方式:1-按书名查询 2-按书号查询");
        String flag = in.next();
        if(flag.equals("1")|flag.equals("2")) return flag;
        throw new CommandException();
    }

    public static void find(String type) {
        try {
            if(type.equals("1")) {
                System.out.println("请输入书名:");
                String v = in.next();
                System.out.println("找到图书 书号:"+getBook(type,bMap,v)+" 书名:"+v);
            }else {
                System.out.println("请输入书号:");
                String v = in.next();
                System.out.println("找到图书 书号:"+v+" 书名:"+getBook(type,bMap,v));
            }
        } catch (NoBookException e) {
            System.out.println("查无此书");
        } catch (CommandException e) {
            System.out.println("请输入正确命令");
        }
    }

    public static String isGoingOn() throws CommandException{
        System.out.println("是否继续查找:1-查找 2-结束");
        String flag = in.next();
        if(flag.equals("1")|flag.equals("2")) return flag;
        throw new CommandException();
    }

    public static Object getBook(String type,HashMap<String,String> map,String v) throws NoBookException,CommandException{
        if(type.equals("1")) {
            Iterator<String> it = map.keySet().iterator();
            while (it.hasNext()) {
            String key = it.next().toString();
            if (map.get(key).equals(v)) return key;
        }
            throw new NoBookException("该图书不存在");
        }
        else {
            Integer it = new Integer(v);
            int n = it.intValue();
            if(n>map.size()) throw new NoBookException("该图书不存在");
            return map.get(v);
        }
    }
}
package com.library;
public class NoBookException extends Exception{
    private static final long serialVersionUID = 1L;
    public NoBookException() {
    }
    public NoBookException(String message) {
    super(message);
    }
}
package com.library;
public class CommandException extends Exception{
    private static final long serialVersionUID = 1L;
    public CommandException() {
    }
    public CommandException(String message) {
    super(message);
    }
}


點擊查看更多內容
4人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消