要求
图书管理系统,适合新手学习 用户可根据书名、序号,查询图书信息
完整代码
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Book {
static Scanner scanner=new Scanner(System.in);
public static String byName(Map<Integer, String> map){
System.out.println("请输入图书名称:");
String name=scanner.next();
for (String s:map.values()) {
if (name.equals(s)){
return s;
}
}
return "图书不存在!";
}
public static String byId(Map<Integer, String> map){
System.out.println("请输入图书序号:");
Book fb=new Book();
int index = scanner.nextInt();
for (int i:map.keySet()) {
if (index == -1){
return "输入序号不正确";
}else if (i==index){
return map.get(i);
}
}
return "该序号不存在!";
}
public static void main(String[] args) {
Map<Integer,String> map= new HashMap<>();
map.put(1,"数据库");
map.put(2,"高等数学");
map.put(3,"需求分析");
map.put(4,"软件工程");
map.put(5,"java");
map.put(6,"c++");
map.put(7,"离散数学");
map.put(8,"javaweb");
map.put(9,"英语");
System.out.println("-----------欢迎使用我的图书系统-----------");
Book book = new Book();
while(true) {
System.out.println("1.按名称查找书籍");
System.out.println("2.按序号查找书籍");
System.out.println("请您输入选择:");
int in = scanner.nextInt();
switch(in) {
case 1:
System.out.println("书名:"+book.byName(map));
break;
case 2:
System.out.println("书名:"+book.byId(map));
break;
default : System.out.println("错误命令请按提示输入,请重新输入:");
continue;
}
break;
}
}
}
运行截图
|