题解 | #自动售货系统#

自动售货系统

http://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf

把不同的操作封装成5个函数,根据指令的不同调用这些函数进行对应的处理
import java.util.Scanner;

import static java.lang.Integer.parseInt;
import static java.lang.System.in;
import static java.lang.System.out;


/**
 * @author IronSid
 * @create 2021-07-02 23:09
 */
public class Main {
    Commodity[] commodities = new Commodity[6];

    /**
     * 余额
     */
    int balance;

    /**
     * 钱盒中不同面额钱的数量
     * {1元张数, 2元张数, 5元张数, 10元张数}
     */
    int[] cashs = new int[4];

    /**
     * 面额数字,与cashes对应
     */
    int[] denomination = {1, 2, 5, 10};

    public static void main(String[] args) {
        Scanner sc = new Scanner(in);
        Main main = new Main();
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            if (line.length() == 0) continue;
            String[] commands = line.split(";");
            for (String command : commands) {
                if (command.length() == 0) continue;
                //读入指令,进行操作
                switch (command.charAt(0)) {
                    case 'r':
                        main.initializate(command.substring(2));
                        //初始化余额
                        main.balance = 0;
                        break;
                    case 'p':
                        main.pay(command.substring(2));
                        break;
                    case 'b':
                        main.buy(command.substring(2));
                        break;
                    case 'c':
                        main.refund();
                        break;
                    case 'q':
                        main.quiry(command);
                        break;
                }
            }
        }
    }
    //1.初始化
    void initializate(String command) {
        String[] number = command.split("[- ]");
        int[] cost = {2, 3, 4, 5, 8, 6};
        for (int i = 0; i < 6; i++) {
            commodities[i] = new Commodity("A" + (i + 1), parseInt(number[i]), cost[i]);
        }
        for (int i = 6; i < 10; i++) {
            cashs[i - 6] = parseInt(number[i]);
        }
        out.println("S001:Initialization is successful");
    }

    //2.投币
    void pay(String command) {
        int paid = parseInt(command);
        int index = 0;
        for (; index < denomination.length; index++) {
            if (denomination[index] == paid)
                break;
        }
        //没有匹配的面额
        if (index == denomination.length) {
            out.println("E002:Denomination error");
            return;
        }
        //零钱不足
        if (cashs[0] + cashs[1] * 2 < paid) {
            out.println("E003:Change is not enough, pay fail");
            return;
        }
        //无剩余商品
        if (isSoldOut(commodities)) {
            out.println("E005:All the goods sold out");
            return;
        }
        //投币成功
        balance += paid;
        cashs[index]++;
        out.println("S002:Pay success,balance=" + balance);
    }

    //3.购买商品
    void buy(String name) {
        Commodity c = getCommdity(name, commodities);
        if (c == null) {
            out.println("E006:Goods does not exist");
            return;
        }
        if (c.number == 0) {
            out.println("E007:The goods sold out");
            return;
        }
        if (balance < c.cost) {
            out.println("E008:Lack of balance");
            return;
        }
        //购买成功,余额减价格,数量减一
        balance -= c.cost;
        c.number--;
        out.println("S003:Buy success,balance=" + balance);
    }


    //4.退币
    void refund() {
        if (balance == 0) {
            out.println("E009:Work failure");
            return;
        }
        //退币张数
        int[] refundCashs = new int[cashs.length];
        //先退面额大的币
        for (int i = cashs.length - 1; i >= 0; i--) {
            //钱盒该面额的钱没了就跳过
            if(cashs[i]==0){
                break;
            }
            //若有,则尽量多得用这种面额退币
            while(cashs[i]>0&&balance>=denomination[i]){
                cashs[i]--;
                balance-=denomination[i];
                refundCashs[i]++;
            }
        }
        //余额归零,输出找零
        balance = 0;
        for (int i = 0; i < 4; i++) {
            out.println(denomination[i] + " yuan coin number=" + refundCashs[i]);
        }
    }
    //5.查询
    void quiry(String command) {
        switch (command) {
            case "q 0":
                //商品排序
                sort(commodities);
                for (Commodity commodity : commodities) {
                    out.println(commodity.toString());
                }
                break;
            case "q 1":
                for (int i = 0; i < 4; i++) {
                    out.println(denomination[i] + " yuan coin number=" + cashs[i]);
                }
                break;
            default:
                out.println("E010:Parameter error");
        }
    }

    //商品排序
    void sort(Commodity[] c) {
        for (int i = 0; i < c.length - 1; i++) {
            boolean flag = true;
            for (int j = 0; j < c.length - 1 - i; j++) {
                if (compare(c[j], c[j + 1]) > 0) {
                    flag = false;
                    Commodity temp = c[j];
                    c[j] = c[j + 1];
                    c[j + 1] = temp;
                }
            }
            if (flag) break;
        }
    }

    //比较器
    int compare(Commodity a, Commodity b) {
        //比较number,若number相等,则比较商品的字典顺序
        if (a.number != b.number)
            //number大的应该排在前
            return b.number - a.number;
        return a.name.compareTo(b.name);
    }

    //若所有商品都卖空,则返回true
    boolean isSoldOut(Commodity[] c) {
        for (Commodity commodity : c) {
            if (commodity.number != 0)
                return false;
        }
        return true;
    }

    /**
     * 根据指定的商品名从商品数组中获取商品
     *
     * @param name 指定商品的名字
     * @param c    商品数组
     * @return 名字对应的商品,若没找到则返回null
     */
    Commodity getCommdity(String name, Commodity[] c) {
        for (Commodity commodity : c) {
            if (name.equals(commodity.name))
                return commodity;
        }
        return null;
    }
}

//商品类
class Commodity {
    String name;
    int number;
    int cost;

    public Commodity(String name, int number, int cost) {
        this.name = name;
        this.number = number;
        this.cost = cost;
    }

    @Override
    public String toString() {
        return this.name + " " + this.cost + " " + this.number;
    }
}



全部评论

相关推荐

海螺很能干:每次看到这种简历都没工作我就觉得离谱
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客企业服务