我必须实施USSD流程:
应具有以下结构:
a
/
a-- b
/ \
c
/
a---b
/ \
/ c
/ a
/ /
Hi-----b---b
\
c
无法清楚地绘制它但有点像用户按下Hi然后他会得到3个选项a,b,c每个都与某个值相关联,如果他选择其中一个然后再次获得更多带有标签的值a,b,c ......在选定的选项的基础上,树就像这样生长,我们可以假设它是一种通用树结构。
So I think about it and then come to solution sharing the same in the below steps:
1). Stored the static data/sub options into the map.
2). Then Stored those maps into map of map to link sub options with options
3). So I have created map of map is this structure is good please share ur inputs??
4). and for processing considering one map into the memory for further actions
Below is there my implementations, can you please share your inputs, what can be best practice or best way to write this code.
public class USSDImpl {
public static void main(String args[])
{
Map<String,String> hiSubOptions = new HashMap<String,String>();
hiSubOptions.put("a","Balance");
hiSubOptions.put("b","Prepaid");
hiSubOptions.put("c","Last3Transactions");
Map<String,String> balanceSubOptions = new HashMap<String,String>();
balanceSubOptions.put("a","Saving");
balanceSubOptions.put("b","Current");
balanceSubOptions.put("c","Other");
Map<String,String> prepaidSubOptions = new HashMap<String,String>();
prepaidSubOptions.put("a","Idea");
prepaidSubOptions.put("b","Airtel");
prepaidSubOptions.put("c","Tata");
Map<String,String> sp1SubOptions = new HashMap<String,String>();
sp1SubOptions.put("a","10");
sp1SubOptions.put("b","100");
sp1SubOptions.put("c","1000");
Map<String,String> sp2SubOptions = new HashMap<String,String>();
sp2SubOptions.put("a","15");
sp2SubOptions.put("b","150");
sp2SubOptions.put("c","1500");
Map<String,String> sp3SubOptions = new HashMap<String,String>();
sp3SubOptions.put("a","20");
sp3SubOptions.put("b","200");
sp3SubOptions.put("c","2000");
Map<String,HashMap<String,String>> optionToSubOptions = new HashMap<String,HashMap<String,String>>();
optionToSubOptions.put("hi",(HashMap<String, String>) hiSubOptions);
optionToSubOptions.put("balance",(HashMap<String, String>) balanceSubOptions);
optionToSubOptions.put("prepaid",(HashMap<String, String>) prepaidSubOptions);
optionToSubOptions.put("idea",(HashMap<String, String>) sp1SubOptions);
optionToSubOptions.put("airtelt",(HashMap<String, String>) sp2SubOptions);
optionToSubOptions.put("tata",(HashMap<String, String>) sp3SubOptions);
Scanner scanner = new Scanner(System.in);
Map<String,String> currentOptions = new HashMap<String, String>();
//String previousMessage = "hi";
while(scanner.hasNext())
{ String userInput = scanner.next();
if(userInput.equalsIgnoreCase("Hi"))
{
currentOptions = optionToSubOptions.get(userInput.toLowerCase());
System.out.println(currentOptions);
}
else
{
//validateOption(userInput);
currentOptions = optionToSubOptions.get(currentOptions.get(userInput.toLowerCase()).toLowerCase());
System.out.println(currentOptions);
}
}
}
}
此外,我想以并发的方式为多个用户扩展这个,所以为了适应它,我必须再创建一个地图地图,其密钥将是该用户的凭证,值将是当前选项。
请分享您的意见