使用map集合添加菜单,运用反射,获取方法,执行程序

public class Menu {
static Scanner input=new Scanner(System.in);
public static void main(String[] args) {

//创建map集合
HashMap<String, String> map = new HashMap<String, String>();
//添加元素,值为方法名
map.put("C", "addProduct");
map.put("U", "editProduct");
map.put("D", "delete");
map.put("DA", "deleteAll");
map.put("I", "findByld");
map.put("FA", "findAll");
//System.out.println(map);
while(true){
//菜单
System.out.println("C:创建 U:修改 D:删除 DA:删除所有 I:通过id查询 FA:查询所有 Q:退出");
System.out.println("请选择");
String s=input.nextLine().toUpperCase();
if(s.equalsIgnoreCase("q")){
System.exit(0);
}
//通过输入的菜单项找到对应的方法
String value = map.get(s);
//通过反射执行键对应的方法
//System.out.println(value);
if(value==null){
System.out.println("输入有误");
}else{
try {
//获取类对象
Class menu = Menu.class;
//获取构造
Constructor con = menu.getConstructor();
//获取对象
Object object = con.newInstance();
//通过类对象,暴力获取诶类中的方法(传入方法名和参数类型)
Method method = menu.getDeclaredMethod(value);
//修改方法权限
method.setAccessible(true);
//启动方法,传入对象和参数
method.invoke(object);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//查询所有商品
private static void findAll() {
Service service = new Service();
List<Products> list = service.findAll();
printList(list);
}

private static void findByld() {
findAll();
System.out.println("请输入要查的商品id");
String pid=input.nextLine();
Service service = new Service();
List<Products> list = service.findById(pid);
if(list==null){
System.out.println("没有此商品");
return;
}
printList(list);
}
private static void deleteAll() {
//创建集合存储pid
LinkedList<String> list = new LinkedList<String>();
Service service = new Service();
while (true){
System.out.println("请输入需要删除的商品id(以-1结束)");
String pid=input.nextLine();
if(pid.equals("-1")){
break;
}
List<Products> p =service.findById(pid);
printList(p);
list.add(pid);
}
System.out.println("是否要删除(Y/N)");
if(input.nextLine().equalsIgnoreCase("y")){
service.deleteAll(list);
}
}
private static void delete() {
findAll();
//查询需要修改的商品
System.out.println("请输入需要删除的商品编号");
String pid=input.nextLine();
Service service = new Service();
List<Products> list = service.findById(pid);
printList(list);
System.out.println("是否要删除(Y/N)");
if(input.nextLine().equalsIgnoreCase("y")){
service.delete(pid);
}
}
//修改数据
private static void editProduct() {
findAll();
//查询需要修改的商品
System.out.println("请输入需要修改的商品编号");
String pid=input.nextLine();
System.out.println("请输入商品名");
String pname=input.nextLine();
System.out.println("请输入价格");
int price=Integer.parseInt((input.nextLine()));
System.out.println("请输入是否上架(1/0)");
String flag=input.nextLine();
System.out.println("请输入商品类名");
String category_id=input.nextLine();
Products p=new Products(pid,pname,price,flag,category_id);
Service service = new Service();
service.editProduct(p);
}
//创建数据
private static void addProduct() {
// TODO Auto-generated method stub
System.out.println("请输入商品名");
String pname=input.nextLine();
System.out.println("请输入价格");
int price=Integer.parseInt((input.nextLine()));
System.out.println("请输入是否上架(1/0)");
String flag=input.nextLine();
System.out.println("请输入商品类名");
String category_id=input.nextLine();
Products p=new Products("1",pname,price,flag,category_id);
Service service = new Service();
service.addProduct(p);
}
//遍历集合
public static void printList(List<Products> list) {
for (Products products : list) {
System.out.println(products);
}
}

}

原文地址:https://www.cnblogs.com/Flyrun/p/7999195.html