自动售货系统(初始化,投币,购买,退币,查询)Java

import java.util.*;
import java.util.stream.Collectors;

/**
 * 自动售货系统
 *
 * @author Green.Gee
 * @date 2022/12/27 20:47
 * @email green.gee.lu@gmail.com
 */
public class AtomicSaleSys {
    /**
     * 1. 商品:每种商品包含商品名称、单价、数量三种属性,其中商品名不重复。考生不能修改商品名称和单价,初始化命令设置商品数量。这些信息在考试框架中进行定义,考生在实现功能代码时可直接使用。
     * 2. 存钱盒信息:钱币面额、张数两种属性。初始化命令设置各种面额钱币张数。这些信息在考试框架中进行定义,考生在实现功能代码时可直接使用。
     * 3. 退币原则 :
     * 1) 根据系统存钱盒内钱币的 信息 ,按钱币总张数最少的原则进行退币。
     * 2) 如果因零钱不足导致不能退币,则尽最大可能退币,以减少用户损失。
     * 例如:假设存钱盒内只有4张2元,无其它面额钱币。如果需要退币7元,系统因零钱不足无法退币,则继续尝试退币6元,最终系统成功退币3张2元,用户损失1元钱币。
     * 4. 投币操作说明:每次投币成功,投入的钱币面额累加到投币余额;同时,本次投入的钱币放入存钱盒中,存钱盒相应面额钱币增加。
     * 5. 投币余额:指当前自动售货机中用户剩余的可购买商品的钱币总额;例如:投入2元面额的钱币,投币余额增加2元;购买一件价格2元的商品,投币余额减少2元;
     * 6. 退币操作说明:退币操作需要遵守 退币原则 ;退币成功后,投币余额清零,同时扣除存钱盒相应的金额。
     * 7. 购买商品操作说明:一次仅允许购买一件商品;购买商品成功后,自动售货机中对应商品数量减1,投币余额扣除本次购买商品的价格。
     */


    static final String S001 = "S001:Initialization is successful";
    static final String S002 = "S002:Pay success,balance=";
    static final String S003 = "S003:Buy success,balance=";

    static final String E001 = "E001:Initialization is successful";
    static final String E002 = "E002:Denomination error";
    static final String E003 = "E003:Change is not enough, pay fail";

    static final String E005 = "E005:All the goods sold out";

    static final String E006 = "E006:Goods does not exist";
    static final String E007 = "E007:The goods sold out";
    static final String E008 = "E008:Lack of balance";

    static final String E009 = "E009:Work failure";
    static final String E010 = "E010:Parameter error";

    static void showMoney(int a, int b, int c, int d) {
        System.out.println("1 yuan coin number=" + a);
        System.out.println("2 yuan coin number=" + b);
        System.out.println("5 yuan coin number=" + c);
        System.out.println("10 yuan coin number=" + d);
    }

    static void showMoney() {
        int i = 0;
        System.out.println("1 yuan coin number=" + monies.get(i++).getCount());
        System.out.println("2 yuan coin number=" + monies.get(i++).getCount());
        System.out.println("5 yuan coin number=" + monies.get(i++).getCount());
        System.out.println("10 yuan coin number=" + monies.get(i++).getCount());
    }

    static void showGoods() {
        List<Goods> goods = goodsList.stream()
                .sorted(Comparator.comparing(Goods::getCount).reversed()
                        .thenComparing(Goods::getName))
                .collect(Collectors.toList());
        int i = 0;
        Goods goodsItem = goods.get(i++);
        System.out.println(goodsItem.getName() + " " + (int) goodsItem.getPrice() + " " + goodsItem.getCount());
        goodsItem = goods.get(i++);
        System.out.println(goodsItem.getName() + " " + (int) goodsItem.getPrice() + " " + goodsItem.getCount());
        goodsItem = goods.get(i++);
        System.out.println(goodsItem.getName() + " " + (int) goodsItem.getPrice() + " " + goodsItem.getCount());
        goodsItem = goods.get(i++);
        System.out.println(goodsItem.getName() + " " + (int) goodsItem.getPrice() + " " + goodsItem.getCount());
        goodsItem = goods.get(i++);
        System.out.println(goodsItem.getName() + " " + (int) goodsItem.getPrice() + " " + goodsItem.getCount());
        goodsItem = goods.get(i++);
        System.out.println(goodsItem.getName() + " " + (int) goodsItem.getPrice() + " " + goodsItem.getCount());
    }

    static List<Goods> goodsList = new ArrayList<>();
    static List<Money> monies = new ArrayList<>();
    static int totalMoney = 0;

    /**
     * 多线程程序处理能力
     * <p>
     * 计算自动售货机
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            String[] commands = s.split(";");
            for (String cmd : commands) {
                if (cmd != null && !"".equals(cmd)) {
                    String[] things = cmd.split(" ");
                    if (things[0].equals("r")) {// init
                        // goods
                        goodsList = initGoods(things[1]);
                        // money
                        monies = initMoney(things[2]);
                        // print show something
                        System.out.println(S001);

                    } else if ("p".equals(things[0])) {// pay money
                        int payMoney = Integer.parseInt(things[1]);
                        payMoney(payMoney);

                    } else if ("c".equals(things[0])) {// cycle money
                        if (totalMoney == 0) {
                            System.out.println(E009);
                        } else {
                            /**
                             * 1) 根据系统存钱盒内钱币的 信息 ,按钱币总张数最少的原则进行退币。
                             * 2) 如果因零钱不足导致不能退币,则尽最大可能退币,以减少用户损失。
                             */
                            List<Money> list = monies.stream().sorted(Comparator.comparing(Money::getCount)).collect(Collectors.toList());
                            cycleMoney(list);

                        }
                    } else if ("b".equals(things[0])) {// buy something
                        if (things.length == 2) {
                            String item = things[1];
                            List<Goods> list = isExstsGoods(item);
                            if (list.size() == 0) {
                                System.out.println(E006);
                            } else {
                                Goods goods = list.get(0);
                                if (goods.getCount() == 0) {
                                    System.out.println(E007);
                                } else {
                                    if (totalMoney < goods.getPrice()) {
                                        System.out.println(E008);
                                    } else {
                                        totalMoney = totalMoney - (int) goods.getPrice();
                                        goodsList.forEach(it -> {
                                            if (it.getName().equals(goods.getName())) {
                                                it.setCount(it.getCount() - 1);
                                            }
                                        });
                                        System.out.println(S003 + totalMoney);
                                    }
                                }
                            }
                        } else {
                            System.out.println(E010);
                        }
                    } else if ("q".equals(things[0])) {// query something
                        if (things.length == 2) {
                            if (things[1].equals("0")) {// goods
                                showGoods();
                            } else if (things[1].equals("1")) {// money
                                showMoney();
                            } else {
                                System.out.println(E010);
                            }
                        } else {
                            System.out.println(E010);
                        }
                    } else {
                        System.out.println(E010);
                    }
                }
            }
        }
    }

    // come back money
    private static void cycleMoney(List<Money> list) {

        Money o1 = list.get(0);
        Money o2 = list.get(1);
        Money o3 = list.get(2);
        Money o4 = list.get(3);

        List<List<Integer>> l = new ArrayList<>();
        for (int i1 = 0; i1 <= o1.getCount(); i1++) {
            int i1s = o1.getSize() * i1;

            for (int i2 = 0; i2 <= o2.getCount(); i2++) {
                int i2s = o2.getSize() * i2;

                for (int i3 = 0; i3 <= o3.getCount(); i3++) {
                    int i3s = o3.getSize() * i3;

                    for (int i4 = 0; i4 <= o4.getCount(); i4++) {
                        int i4s = o4.getSize() * i4;
                        List<Integer> tempL = new ArrayList<>();
                        tempL.add(i1);
                        tempL.add(i2);
                        tempL.add(i3);
                        tempL.add(i4);
                        l.add(tempL);
                    }
                }
            }
        }

        List<List<Integer>> l1 = l.stream()
                .filter(item -> {
                    int sum = (item.get(0)) + (item.get(1) * 2) + (item.get(2) * 5) + (item.get(3) * 10);
                    return sum == totalMoney;
                }).collect(Collectors.toList());
        if (l1.size() > 0) {
            List<Integer> ld = l1.stream().sorted(Comparator.comparing(item -> item.stream().mapToInt(i -> i).sum()))
                    .collect(Collectors.toList()).get(0);
            showMoney(ld.get(0), ld.get(1), ld.get(2), ld.get(3));
        } else {
            List<List<Integer>> ld = l.stream()
                    .filter(item -> {
                        int sum = (item.get(0)) + (item.get(1) * 2) + (item.get(2) * 5) + (item.get(3) * 10);
                        return sum < totalMoney;
                    })
                    .collect(Collectors.toList());
            int max = 0;
            List<Integer> sd = new ArrayList<>();
            for (List<Integer> item : ld) {
                int sum = (item.get(0)) + (item.get(1) * 2) + (item.get(2) * 5) + (item.get(3) * 10);
                if(sum > max){
                    max = sum;
                    sd = item;
                }
            }
            showMoney(sd.get(0), sd.get(1), sd.get(2), sd.get(3));
        }
        totalMoney = 0;
    }

    private static List<Goods> isExstsGoods(String goods) {
        return goodsList.stream()
                .filter(item -> goods.equals(item.getName()))
                .collect(Collectors.toList());
    }

    // pay [p x]
    private static void payMoney(int payMoney) {
        if (payMoney == 1) {
            if (!goodsAreEmpty()) {
                System.out.println(E005);
            } else {
                monies.get(0).setCount(monies.get(0).getCount() + 1);
                totalMoney += monies.get(0).getSize();
                System.out.println(S002 + totalMoney);
            }
        } else if (payMoney == 2) {
            if (!goodsAreEmpty()) {
                System.out.println(E005);
            } else {
                monies.get(1).setCount(monies.get(1).getCount() + 1);
                totalMoney += monies.get(1).getSize();
                System.out.println(S002 + totalMoney);
            }
        } else if (payMoney == 5) {
            // limit total money
            int size = sumOneAndTwoSize();
            if (size <= payMoney) {
                System.out.println(E003);
            } else {
                if (!goodsAreEmpty()) {
                    System.out.println(E005);
                } else {
                    monies.get(2).setCount(monies.get(2).getCount() + 1);
                    totalMoney += monies.get(2).getSize();
                    System.out.println(S002 + totalMoney);
                }
            }
        } else if (payMoney == 10) {
            // limit total money
            int size = sumOneAndTwoSize();
            if (size <= payMoney) {
                System.out.println(E003);
            } else {
                if (!goodsAreEmpty()) {
                    System.out.println(E005);
                } else {
                    monies.get(3).setCount(monies.get(3).getCount() + 1);
                    totalMoney += monies.get(3).getSize();
                    System.out.println(S002 + totalMoney);
                }
            }
        } else {
            System.out.println(E002);
        }
    }

    // goods are empty
    public static boolean goodsAreEmpty() {
        return goodsList.stream().map(Goods::getCount).mapToInt(i -> i).sum() > 0;
    }

    public static int sumMoney() {
        return monies.stream().map(item -> item.getSize() * item.getCount())
                .mapToInt(i -> i).sum();
    }


    private static int sumOneAndTwoSize() {
        int oneSize = monies.get(0).getCount() * monies.get(0).getSize();
        int twoSize = monies.get(1).getCount() * monies.get(1).getSize();
        return oneSize + twoSize;
    }

    private static List<Money> initMoney(String money) {
        String[] moneys = money.split("-");
        int i = 1;
        Money money1 = new Money(1, Integer.parseInt(moneys[0]));
        Money money2 = new Money(2, Integer.parseInt(moneys[1]));
        Money money3 = new Money(5, Integer.parseInt(moneys[2]));
        Money money4 = new Money(10, Integer.parseInt(moneys[3]));

        List<Money> list = new ArrayList<>();
        list.add(money1);
        list.add(money2);
        list.add(money3);
        list.add(money4);

        return list;

    }

    static List<Goods> initGoods(String things) {
        String[] goodsCmd = things.split("-");
        int i = 1;
        Goods goods1 = new Goods("A" + (i++), 2d, Integer.parseInt(goodsCmd[0]));
        Goods goods2 = new Goods("A" + (i++), 3d, Integer.parseInt(goodsCmd[1]));
        Goods goods3 = new Goods("A" + (i++), 4d, Integer.parseInt(goodsCmd[2]));
        Goods goods4 = new Goods("A" + (i++), 5d, Integer.parseInt(goodsCmd[3]));
        Goods goods5 = new Goods("A" + (i++), 8d, Integer.parseInt(goodsCmd[4]));
        Goods goods6 = new Goods("A" + (i++), 6d, Integer.parseInt(goodsCmd[5]));

        List<Goods> goodsList = new ArrayList<>();
        goodsList.add(goods1);
        goodsList.add(goods2);
        goodsList.add(goods3);
        goodsList.add(goods4);
        goodsList.add(goods5);
        goodsList.add(goods6);

        return goodsList;

    }


    static class Money {
        public Money() {
        }

        public int size;
        public int count;

        public int getSize() {
            return size;
        }

        public int getCount() {
            return count;
        }

        public void setSize(int size) {
            this.size = size;
        }

        public void setCount(int count) {
            this.count = count;
        }

        public Money(int size, int count) {
            this.size = size;
            this.count = count;
        }
    }


    static class Goods {

        public Goods() {
        }

        String name;
        double price;
        int count;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public double getPrice() {
            return price;
        }

        public void setCount(int count) {
            this.count = count;
        }

        public int getCount() {
            return count;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        public Goods(String name, double price, int count) {
            this.name = name;
            this.price = price;
            this.count = count;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Goods goods = (Goods) o;
            return Double.compare(goods.price, price) == 0 && count == goods.count && Objects.equals(name, goods.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(name, price, count);
        }

        @Override
        public String toString() {
            return "Goods{" +
                    "name='" + name + '\'' +
                    ", price=" + price +
                    ", count=" + count +
                    '}';
        }
    }
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

P("Struggler") ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值