题目用例很坑

自动售货系统

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

依据题目的意思,已经设计出系统代码,经过提交测试,来到了用例7,最后报错,没有通过,经过筛选,发现官网提供的答案错误了。

7/10 组用例通过

指令如下:

r 1-1-10-16-11-12 2-15-26-9;p 5;q1;c;b A1;p 10;p 10;c;c;c;p 10;q1;p 1;c;b A3;q1;c;p 5;c;b A1;p 5;c;p 5;q0;c;p 10;q0;p 2;b A5;q1;b A5;b A6;p 10;c;p 2;c;q1;b A2;q0;
r 8-19-29-18-25-20 30-23-1-6;c;p 5;c;b A1;b A6;c;b A5;b A2;q0;b A5;q0;p 10;q0;p 2;b A1;q1;p 1;c;q0;q1;c;p 10;b A1;b A3;b A5;p 10;q1;q1;c;q1;q0;p 1;p 2;q0;

图片说明

预期输出如下:

S001:Initialization is successful
S002:Pay success,balance=5
E010:Parameter error
1 yuan coin number=0
2 yuan coin number=0
5 yuan coin number=1
10 yuan coin number=0
E008:Lack of balance
S002:Pay success,balance=10
S002:Pay success,balance=20
1 yuan coin number=0
2 yuan coin number=0
5 yuan coin number=0
10 yuan coin number=2
E009:Work failure
E009:Work failure
S002:Pay success,balance=10
E010:Parameter error
S002:Pay success,balance=11
1 yuan coin number=1
2 yuan coin number=0
5 yuan coin number=0
10 yuan coin number=1
E008:Lack of balance
E010:Parameter error
E009:Work failure
S002:Pay success,balance=5
1 yuan coin number=0
2 yuan coin number=0
5 yuan coin number=1
10 yuan coin number=0
E008:Lack of balance
S002:Pay success,balance=5
1 yuan coin number=0
2 yuan coin number=0
5 yuan coin number=1
10 yuan coin number=0
S002:Pay success,balance=5
E010:Parameter error
1 yuan coin number=0
2 yuan coin number=0
5 yuan coin number=1
10 yuan coin number=0
S002:Pay success,balance=10
E010:Parameter error
S002:Pay success,balance=12
S003:Buy success,balance=4
E010:Parameter error
E008:Lack of balance
E008:Lack of balance
S002:Pay success,balance=14
1 yuan coin number=0
2 yuan coin number=2
5 yuan coin number=0
10 yuan coin number=1
S002:Pay success,balance=2
1 yuan coin number=0
2 yuan coin number=1
5 yuan coin number=0
10 yuan coin number=0
E010:Parameter error
E008:Lack of balance
E010:Parameter error
S001:Initialization is successful
E009:Work failure
S002:Pay success,balance=5
1 yuan coin number=0
2 yuan coin number=0
5 yuan coin number=1
10 yuan coin number=0
E008:Lack of balance
E008:Lack of balance
E009:Work failure
E008:Lack of balance
E008:Lack of balance
E010:Parameter erro
rE008:Lack of balance
E010:Parameter error
S002:Pay success,balance=10
E010:Parameter error
S002:Pay success,balance=12
S003:Buy success,balance=10
E010:Parameter error
S002:Pay success,balance=11
1 yuan coin number=1
2 yuan coin number=0
5 yuan coin number=0
10 yuan coin number=1
E010:Parameter error
E010:Parameter error
E009:Work failure
S002:Pay success,balance=10
S003:Buy success,balance=8
S003:Buy success,balance=4
E008:Lack of balance
S002:Pay success,balance=14
E010:Parameter error
E010:Parameter error
1 yuan coin number=0
2 yuan coin number=2
5 yuan coin number=0
10 yuan coin number=1
E010:Parameter error
E010:Parameter error
S002:Pay success,balance=1
S002:Pay success,balance=3
E010:Parameter error

竟然是这样的错误,我觉得这已经超出了我的能力范围,预期输出错误点如下:

图片说明

我再怎么根据题目要求,也不可能弄出rE008:Lack of balance这样的提示,故就将整个设计思路论述如下:

第一,根据业务需求,进行面向对象设计,设计了商品类、存钱盒类、钱币类
第二,提供业务处理方法,进行初始化、投币、购买、退币、查询功能

关键点:
(1)理解业务需求,进行需求分析,然后,抽象模型,系统设计
(2)获取指令,提出指令

最终程序设计如下:

import java.util.*;
import java.util.regex.*;

public class Main{

    // 存储商品数据、存钱盒
    public static List<Goods> list;
    public static PigBank pigBank;
    // 正则表达式也不是万能
    //public static Pattern p = Pattern.compile("[10|1|2|5]");
    public static double balance = 0.0;

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            // 获取命令行,并且拆分
            String[] str = sc.nextLine().split(";");
            // 执行命令
            for(String s : str){
                exec(s);
            }
        }
    }

    // 执行命令
    public static void exec(String str){
        if(str.startsWith("r")){
            init(str);
        }else if(str.startsWith("p")){
            coin(str);
        }else if(str.startsWith("b")){
            purchase(str);
        }else if(str.startsWith("c")){
            cancel();
        }else if(str.startsWith("q")){
            query(str);
        }
    }

    // 初始化
    // r
    public static void init(String str){
        // 初始化
        if(list == null){
            list = new ArrayList<>();
            list.add(new Goods("A1", 2.0));
            list.add(new Goods("A2", 3.0));
            list.add(new Goods("A3", 4.0));
            list.add(new Goods("A4", 5.0));
            list.add(new Goods("A5", 8.0));
            list.add(new Goods("A6", 6.0));
        }
        if(pigBank == null){
            pigBank = new PigBank();
            pigBank.addCoin(new Coins("1元",1.0));
            pigBank.addCoin(new Coins("2元",2.0));
            pigBank.addCoin(new Coins("5元",5.0));
            pigBank.addCoin(new Coins("10元",10.0));
        }
        // 拆分
        String[] arr = str.split(" ");
        // 商品
        // 排序
        Collections.sort(list,new Comparator<Goods>(){
            public int compare(Goods g1, Goods g2){
                if(g1.name.hashCode() > g2.name.hashCode()){
                    return 1;
                }else if(g1.name.hashCode() < g2.name.hashCode()){
                    return -1;
                }
                return 0;
            }
        });
        String[] arrNum = arr[1].split("-");
        for(int i=0; i<arrNum.length; i++){
            list.get(i).setNum(Integer.parseInt(arrNum[i]));
        }
        // 钱币
        String[] arrCoin = arr[2].split("-");
        for(int i=0; i<arrCoin.length; i++){
            pigBank.getList().get(i).setNum(Integer.parseInt(arrCoin[i]));
        }
        // 展示
        System.out.println("S001:Initialization is successful");
        // 投币余额初始化
        balance = 0.0;
    }

    // 投币
    // p
    public static void coin(String str){
        // 拆分
        String s = str.split(" ")[1];
        if(!"1".equals(s) && !"2".equals(s) && 
           !"5".equals(s) && !"10".equals(s)){
            System.out.println("E002:Denomination error");
        }else if(pigBank.getTotalFor1And2() < Double.parseDouble(s)){
            System.out.println("E002:Denomination error");
        }else if(getGoodNums() == 0){
            System.out.println("E005:All the goods sold out");
        }
        // 存钱盒增加
        for(int i=0; i<pigBank.getList().size(); i++){
            Coins c = pigBank.getList().get(i);
            if(c.price == Double.parseDouble(s)){
                c.setNum(1);
                break;
            }
        }
        // 投币余额累加
        balance += Double.parseDouble(s);
        System.out.println("S002:Pay success,balance=" + (int)balance);
    }

    // 购买
    // b
    public static void purchase(String str){
        if(list == null || list.size() == 0){
            System.out.println("E006:Goods does not exist");
            return ;
        }
        // 遍历商品
        boolean flag = false;
        Goods goods = null;
        String s = str.split(" ")[1];
        for(Goods g : list){
            if(g.name.equals(s)){
                flag = true;
                goods = g;
                break;
            }
        }
        if(!flag){
            System.out.println("E006:Goods does not exist");
            return ;
        }
        if(getGoodNums() == 0){
            System.out.println("E007:The goods sold out");
            return ;
        }
        double b = balance;
        if(b < goods.price){
            System.out.println("E008:Lack of balance");
            return ;
        }
        // 商品数量
        goods.num--;
        // 投币余额
        balance -= goods.price;
        System.out.println("S003:Buy success,balance=" + (int) balance);
    }

    // 退币
    // c
    public static void cancel(){
        if(balance <= 0.0){
            System.out.println("E009:Work failure");
            return ;
        }
        // 退币原则找零
        // 如果因零钱不足导致不能退币,则尽最大可能退币,以减少用户损失
        // 根据系统存钱盒内钱币的信息,按钱币总张数最少的原则进行退币
        int size = pigBank.list.size();
        String res = "";
        for(int i=size - 1; i>=0; i--){
            Coins c = pigBank.list.get(i);
            int n = 0;
            for(int j=c.num; j>=1; j--){
                if(c.price * j <= balance){
                    n = j;
                    balance -= c.price * j;
                    break;
                }
            }
            res = c.name.substring(0,c.name.indexOf("元")) + 
                " yuan coin number=" + n + "\n" + res;
            c.num -= n;
        }
        System.out.print(res);
        balance = 0.0;
    }

    // 查询
    // q
    public static void query(String str){
        // 类型
        if(!"q 0".equals(str) && !"q 1".equals(str)){
            System.out.println("E010:Parameter error");
            return ;
        }
        String s = str.substring(2);
        if("0".equals(s)){
            // 排序
            Collections.sort(list,new Comparator<Goods>(){
            public int compare(Goods g1, Goods g2){
                if(g1.num > g2.num){
                    return -1;
                }else if(g1.num < g2.num){
                    return 1;
                }else if(g1.name.hashCode() > g2.name.hashCode()){
                    return 1;
                }else if(g1.name.hashCode() < g2.name.hashCode()){
                    return -1;
                }
                return 0;
                }
            });
            // 遍历展示
            for(Goods g : list){
                System.out.println(g.name + " " + g.price + " " + g.num);
            }
        }else {
            for(int i=0; i<pigBank.list.size(); i++){
                Coins c = pigBank.list.get(i);
                System.out.println(c.name.substring(0,c.name.indexOf("元")) + " yuan coin number=" + c.num);
            }
        }

    }

    // 商品
    public static int getGoodNums(){
        if(list == null || list.size() == 0){
            return 0;
        }
        int num = 0;
        for(Goods goods : list){
            num += goods.num;
        }
        return num;
    }

    // 商品总价
    public static double getTotalPrice(){
        if(list == null || list.size() == 0){
            return 0.0;
        }
        double sum = 0.0;
        for(Goods goods : list){
            sum += goods.price * goods.num;
        }
        return sum;
    }
}

class Goods{
    String name;
    double price;
    int num;
    Goods(String name, double price){
        this.name = name;
        this.price = price;
        this.num = 0;
    }
    public void setNum(int num){
        this.num += num;
    }
}

class PigBank{
    List<Coins> list;
    public PigBank(){
        list = new ArrayList<Coins>();
    }
    public void addCoin(Coins coin){
        list.add(coin);
    }

    public List<Coins> getList(){
        return list;
    }

    public double getTotalFor1And2(){
        double total = 0.0;
        for(Coins c : list){
            if(c.price == 1.0 || c.price == 2.0){
                total += c.price * c.num;
            }
        }
        return total;
    }

}

class Coins{
    String name;
    double price;
    int num;
    public Coins(String name, double price){
        this.name = name;
        this.price = price;
        this.num = 0;
    }

    public void setNum(int num){
        this.num += num;
    }
}

等系统修复成功了,到时候再试一下。

全部评论

相关推荐

Java面试先知:我也是和你一样的情况,hr 说等开奖就行了
点赞 评论 收藏
分享
09-29 00:03
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务