栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java控制台图书查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 bMap = new HashMap();
    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 map,String v) throws NoBookException,CommandException{
        if(type.equals("1")) {
            Iterator 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);
    }
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/238392.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号