有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将对象添加到列表<object>w/Java时获取ClassExceptionError

我只想包括我有问题的函数-

private List<Object> chute = new ArrayList<Object>();

public void press(int value) {
    if(numOfButtons == -1) throw new IllegalArgumentException("No Construct Method Called");
    if (value >=0 && value < numOfButtons){
        if(productCosts.get(value)<= currentMoney && productStock.get(value) >= 1){
            change = currentMoney - productCosts.get(value);
            List<Integer> coinTypes_sorted = new ArrayList<Integer>(coinTypes);
            Collections.sort(coinTypes_sorted);
            getChange(change,coinTypes_sorted, coinTypes.size() -1);
            currentMoney -= productCosts.get(value);
            productStock.set(value, productStock.get(value)-1);
            chute.add(productNames.get(value));
            chute.add(change);

        }
    } else throw new IllegalArgumentException("Value is not in range of button selection");
}

   private void getChange(int money,List<Integer> coins, int iterator){
    if(iterator >= 0){
        int maxValue = Collections.max(coins);
        int indexOfMax = coinTypes.indexOf(maxValue);
        while (money >= maxValue && coinStock.get(indexOfMax) > 0){
            money -= maxValue;
            chute.add(new Coin(maxValue));
            coinStock.set(indexOfMax,coinStock.get(indexOfMax) -1);
        }
        iterator -= 1;
        coins.remove(Collections.max(coins));
        getChange(money, coins, iterator);
    } else return;
}

我遇到的问题是getChange方法中的chute.add(new Coin(maxValue))

我得到的例外是: *编辑添加的完整堆栈跟踪-

Exception in thread "main" java.lang.ClassCastException: ca.ucalgary.seng301.vendingmachine.Coin cannot be cast to ca.ucalgary.seng301.vendingmachine.Pop
at ca.ucalgary.seng301.vendingmachine.parser.Parser.checkDelivery(Parser.java:54)
at ca.ucalgary.seng301.vendingmachine.parser.Parser.CHECK_DELIVERY(Parser.java:370)
at ca.ucalgary.seng301.vendingmachine.parser.Parser.Command(Parser.java:226)
at ca.ucalgary.seng301.vendingmachine.parser.Parser.process(Parser.java:190)
at ca.ucalgary.seng301.vendingmachine.Driver.<init>(Driver.java:17)
at ca.ucalgary.seng301.myvendingmachine.VendingMachineFactory.<init>(VendingMachineFactory.java:467)
at ca.ucalgary.seng301.myvendingmachine.VendingMachineFactory.main(VendingMachineFactory.java:461)

考虑到它只是一个Object列表而不是一个Pop列表,这怎么可能呢

编辑*

 @Override
public List<Object> extract() {
    if(numOfButtons == -1) throw new IllegalArgumentException("No Construct Method Called");
    List<Object> temp = new LinkedList<Object>();
    while(chute.size() != 0 ){
        temp.add(chute.remove(0));
    }
    return temp;
}

好的-现在所有提到的chute对象都被添加了

编辑*

这是托运文件

 * "CHECK_DELIVERY" "(" <INTEGER> { "," <STRING> } ")"
 * 
    • 此命令用于检查自动售货机的行为是否与
  • 预料之中。此命令不与自动售货机通信,但
  • 检查是否已交付预期的内容
  • 目的:
  • 整数表示交付的所有硬币的预期总价值(例如
  • 例如,作为变更)。字符串的顺序指示了
  • 预计已交付

固定

所以本质上这不是一个修正,但是他说他的解析器是一个早期的模型,所以不是寻找硬币,而是寻找整数。对不起,登记时间太晚了


共 (0) 个答案