某模拟售货机模块功能描述如下:
(1)该售货机只可接收5元、10元、20元、50元、100元的纸钞。
(2)售货机中售卖的的饮料价格不等,有5元的、10元的、15元的。
(3)用户选择饮料,投入足够的钱,然后按确定按钮,售货机将送出饮料。
(4)若售货机没有零钱找,则一个显示[零钱找完]的红灯亮,投入的钱将会全额退出,饮料不会送出来。
(5)若有零钱找,则显示[零钱找完]的红灯灭,在送出饮料的同时退还多余的钱。
(6)当用户取消交易时,应最大可能还原到初始状态(注意:需要确定什么情况下,交易不可取消)
注意:
(1)此售货机的工作过程要求模拟实物实现,找零的过程不同于电子支付,该售货机需要根据实物货币判断是否有零钱找,若需要找零,应给出找零的方案。同样,投币的过程也是模拟实物的方式,而不同于电子支付。
(2)不合要求的货币将不予接纳。
(3)该售货机的使用规程是:
1.投币的过程可以设计为循环不断投币,或者:也可以设计为一次性投币。
2.先投币,然后选择饮料的种类;或者:先选择饮料,后投币;
3.每笔交易可以选择多个饮料; 或者:每笔交易只可选择一个饮料。
4.当售货机中无零钱的时候,可以投币和选择饮料吗?
5.当交易不成功的时候,退出的货币是用户刚投入的,还是零钱箱中的?
以上规程,你可以自选,但需要在算法描述中说明。
使用某种程序设计语言实现该模拟的售货机模块.
我的代码实现如下,可能有很多不足,但主要功能都能实现.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Vendingmachine {
static HashMap<String, Integer> drinkList;//饮料列表
static String product; //饮料的选择情况
static int productPrice; //产品价格
static int money; //投币口
static boolean light; //零钱状态灯,true代表亮着
//零钱储备情况,设置默认值
static int $5 = 5;
static int $10 = 5;
static int $20 = 5;
static int $50 = 5;
static int $100 = 5;
//找零情况,默认值为0(出钞口)
static int money_5 = 0;
static int money_10 = 0;
static int money_20 = 0;
static int money_50 = 0;
//设置饮料数量的初始值
static {
drinkList = new HashMap<String, Integer>();
drinkList.put("1-雪碧5元", 3);
drinkList.put("2-可乐10元", 3);
drinkList.put("3-橙汁15元", 10);
}
public static void main(String[] args) {
loop:
while (true) {
System.out.println("请选择你要购买的饮料编号");
//遍历map集合
Set<Map.Entry<String, Integer>> drink = drinkList.entrySet();
for (Map.Entry<String, Integer> drink_ : drink) {
//给出饮料列表
System.out.println(drink_.getKey() + ",还剩" + drink_.getValue() + "瓶");
}
Scanner scanner = new Scanner(System.in);
int drinkId = scanner.nextInt();
//循环至输入正确的编号或商品有货
while (getDrink(drinkId)) {
System.out.println("请重新输入饮料编号(按0退出购买):");
drinkId = scanner.nextInt();
if (drinkId == 0) {
continue loop;
}
}
System.out.println("请支付(仅支持5元、10元、20元、50元、100元纸钞:)");
money = scanner.nextInt();//支付的纸钞金额
//判断是否是符合要去的纸钞
while (isRightMoney(money)) {
System.out.println("您支付的纸钞不符合要求,请重新支付(按0退出购买):");
money = scanner.nextInt();
if (money == 0) {
continue loop;
}
}
//判断是否有足够的零钱找给顾客;
if (haveMoneyToCustomer(money)) {
//根据顾客投的钞票增加零钱储备
if (money == 100) {
$100++;
} else if (money == 50) {
$50++;
} else if (money == 20) {
$20++;
} else if (money == 10) {
$10++;
} else if (money == 5) {
$5++;
}
}
System.out.println("交易结束,欢迎下次光临!");
}
}
//判断是否有零钱可以找
public static boolean haveMoneyToCustomer(int money) {
int moneyToCustomer = money - productPrice; //得出需要找零的钱
//要找零的钱金额小于0,说明支付钞票不够
if (moneyToCustomer < 0) {
System.out.println("您支付的金额不足");
return false;
} else if (moneyToCustomer == 0) {
System.out.println("支付成功,正在出货!");
light = false;
Integer num = drinkList.get(product);
drinkList.put(product, num - 1);
return true;
} else {
while (true) {
//如果需要找钱,判断找钱方案
if (moneyToCustomer >= 50) {
if ($50 > 0) {
$50--;
money_50++;
moneyToCustomer = moneyToCustomer - 50;
continue;
}
}
if (moneyToCustomer >= 20) {
if ($20 > 0) {
$20--;
money_20++;
moneyToCustomer = moneyToCustomer - 20;
continue;
}
}
if (moneyToCustomer >= 10) {
if ($10 > 0) {
$10--;
money_10++;
moneyToCustomer = moneyToCustomer - 10;
continue;
}
}
if (moneyToCustomer >= 5) {
if ($5 > 0) {
$5--;
money_5++;
moneyToCustomer = moneyToCustomer - 5;
continue;
}
}
if (moneyToCustomer == 0) {
System.out.println("支付成功!正在出货!");
System.out.println("找您" + money_50 + "张50元;" + money_20 + "张20元;" + money_10 + "张10元;" + money_5 + "张5元;");
//找零完后,将找零参数的值都改回0
money_50 = 0;
money_20 = 0;
money_10 = 0;
money_5 = 0;
//将饮料的数量减一
Integer num = drinkList.get(product);
drinkList.put(product, num - 1);
return true;
} else {
light = true;
if (light == true) System.out.println("零钱找完状态灯亮(没有零钱支付给您,购买失败!)");
//将可能扣除的零钱数和找零情况复原
$50 += money_50;
$20 += money_20;
$10 += money_10;
$5 += money_5;
money_50 = 0;
money_20 = 0;
money_10 = 0;
money_5 = 0;
return false;
}
}
}
}
//判断是否是符合要求的纸钞
public static boolean isRightMoney(int money) {
if (money == 5 | money == 10 | money == 20 | money == 50 | money == 100) {
return false;
} else {
return true;
}
}
//根据饮料Id判断收费价格
public static boolean getDrink(int drinkId) {
if (drinkId == 1) {
if (drinkList.get("1-雪碧5元") > 0) {
System.out.println("请投入5元及以上钞票!");
productPrice = 5;
product = "1-雪碧5元";
return false;
} else {
System.out.println("该饮料已经售完!");
return true;
}
} else if (drinkId == 2) {
if (drinkList.get("2-可乐10元") > 0) {
System.out.println("请投入10元及以上钞票!");
productPrice = 10;
product = "2-可乐10元";
return false;
} else {
System.out.println("该饮料已经售完!");
return true;
}
} else if (drinkId == 3) {
if (drinkList.get("3-橙汁15元") > 0) {
System.out.println("请输入15元及以上钞票!");
productPrice = 15;
product = "3-橙汁15元";
return false;
} else {
System.out.println("该饮料已经售完!");
return true;
}
} else {
System.out.println("请输入正确的饮料编号!");
return true;
}
}
}