某超市门口的自动寄存柜有n个寄存箱,并且有一个投币控制器,顾客想要寄存小件物品时,只要在投币控制器中投入一个1元硬币,如果此时有空闲的箱子,寄存柜就会自动打开一个空的箱子,并且打印输出一张小小的密码纸条;如果没有空闲的箱子,则提示“本柜已满”。当顾客离开超市时,用密码纸条上指定的数字密码依次输入到开箱控制器,则顾客所存包的箱子门就会自动打开,顾客取走物品后,关上门。
输入数据时,可先输入寄存箱总数n, 再由用户选择是“投硬币”还是“输密码”。
如果选择“投硬币”,则只有硬币值是1时才开箱。如果有空闲的箱子,则输出箱子编号及密码(4位数字); 如果无空闲的箱子,则提示“本柜已满”。
如果选择“输密码”,若输入的密码与某一箱子密码相符,则显示打开的箱子编号,否则输出提示“密码错误”。
万能密码:假定有一个密码000086,该密码用于营业结束后整理寄存柜。先在菜单中输入隐藏的选项5,然后提示请输入万能密码,密码输入后,打开所有的箱子,如有箱子中客户没有取走东西,则提示:请重点检查某编号的箱子是否有物留存。
请编写开箱控制程序实现上述过程。
输入输出示例:
寄存箱总数:10
1.投硬币 2.输密码 0.退出 请选择:1
投币值:1
寄存箱编号:1 密码:9342
1.投硬币 2.输密码 0.退出 请选择:2
输入密码:9342
1号寄存箱已打开
1.投硬币 2.输密码 0.退出 请选择:5
请输入万能密码:000086
箱子已经全部打开,请重点检查编号1,编号7的箱子是否有物留存
1.投硬币 2.输密码 0.退出 请选择:0
结束
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
public class p4 {
public p4() {
}
public static void main(String[] args) {
System.out.print("寄存柜总数:");
Scanner in = new Scanner(System.in);
int sum = in.nextInt();
ArrayList list1 = new ArrayList();
while(list1.size() < sum) {
int password = (int)(Math.random() * 9000.0D) + 1000;
if (!list1.contains(password)) {
list1.add(password);
}
}
Map<Integer, String> map1 = new HashMap();
for(int i = 1; i <= sum && i != sum + 1; ++i) {
map1.put(i, String.valueOf(list1.get(i - 1)));
}
HashMap map2 = new HashMap();
while(true) {
while(true) {
System.out.print("1.投硬币 2.输密码 0.退出");
System.out.print(" 请选择:");
int n = in.nextInt();
if (n == 0) {
System.out.print("结束");
return;
}
int inputpassword;
if (n == 1) {
System.out.print("投币值:");
inputpassword = in.nextInt();
if (inputpassword == 1) {
Random random1 = new Random();
Integer[] keys_1 = (Integer[])map1.keySet().toArray(new Integer[0]);
boolean res1 = map1.isEmpty();
if (!res1) {
Integer randomKey1 = keys_1[random1.nextInt(keys_1.length)];
String randomValue1 = (String)map1.get(randomKey1);
int Value1 = Integer.parseInt(randomValue1);
System.out.println("寄存箱编号:" + randomKey1 + " 密码:" + randomValue1);
map1.remove(randomKey1);
map2.put(Value1, String.valueOf(randomKey1));
} else if (res1) {
System.out.println("本柜已满");
}
} else if (inputpassword != 1) {
System.out.println("硬币已归还,请只投入一个硬币");
}
} else if (n == 2) {
System.out.print("输入密码:");
inputpassword = in.nextInt();
boolean res2 = map2.containsKey(inputpassword);
if (res2) {
int refer = Integer.parseInt((String)map2.get(inputpassword));
System.out.println(refer + "号寄存箱已打开");
map2.remove(inputpassword);
map1.put(refer, String.valueOf(inputpassword));
}
if (!res2) {
System.out.println("密码错误");
}
} else if (n == 5) {
System.out.print("请输入万能密码:");
String masterpassword = in.next();
String x = String.format("%6d", 86).replace(" ", "0");
if (!masterpassword.equals(x)) {
if (!masterpassword.equals(x)) {
System.out.println("密码错误");
}
} else {
Collection values = map2.values();
System.out.print("箱子已经全部打开,请重点检查");
Iterator var10 = values.iterator();
while(var10.hasNext()) {
Object value = var10.next();
System.out.print("编号" + value + " ");
}
System.out.println("的箱子是否有物留存");
}
}
}
}
}
}