Java控制台图书查询
2020/4/2 8:01:30
本文主要是介绍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); } }
点击查看更多内容
这篇关于Java控制台图书查询的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?