Java Warehouse Inventory Program:turn()方法错误地改变了属性

时间:2014-06-26 18:35:30

标签: java

我的程序是Sprocket制造公司的仓库库存系统。链轮具有方向性(0-359度,int)。有两种类型的链轮:口袋(顺时针转动)和Nocket(逆时针转动),有3种变化:红色(转动5度),木质(转动10度)和钛合金(转动20度)。我有Sprocket,Nocket,Pocket,RedNocket,RedPocket,WoodenNocket,WoodenPocket,TitaniumNocket和TitaniumPocket等类。

我有一个VirtualWarehouse对象,其中包含一个可调整大小的Sprocket对象数组。我可以通过键盘输入将Sprockets添加到仓库,用户可以在其中提供他们希望添加的Sprockets的类型,变化和数量。我需要一种方法,根据它们的Sprocket类型将所有Sprocket对象转换为适当的度数(例如:WoodenPocket将顺时针旋转10度)。我的问题是每当我“添加”给定数量的Sprockets,然后告诉程序转动所有Sprockets时,程序每次进入循环时都会转动每个Sprocket。例如,如果我添加了三个WoodenPockets,则所有WoodenPocket对象的方向都将提前10(应该是)三次(因为添加了三个项目)。我不明白为什么会发生这种情况,所以我完全无法尝试修复它。我的代码的重要部分如下。

public class Main {
    public static void main(String[] args) { 
        ...
        switch(userInput.toUpperCase()){
            ...
           case "ADD":
                Sprocket newSprocket = getSprocketTypeToAdd();
                System.out.print("How many of this type of sprocket would you like to
                                    add: ");
                if (input.hasNextInt()) {
                    numOfSprockets = input.nextInt();
                } else {
                    System.out.println("Error. You must enter an integer.");
                }
                myWarehouse.add(newSprocket, numOfSprockets);
                doMore = checkMoreInput();
                validInput = true;
                break;
            case "TURN":
                if (myWarehouse.isEmpty()) {
                    System.out.print("Error: warehouse is empty. ");
                } else {
                   myWarehouse.turnAll();
                }
                doMore = checkMoreInput();//checks if user wants to continue
                validInput = true;//ends loop that catches invalid input
                break; 
            ...
        }//end switch 
        ... 
    }//end main method 
}//end Main class 


public class VirtualWarehouse {
    ...
    //properties
    private Sprocket[] warehouseContents;
    private int numberOfEntries;
    ...
    //(constructors, getters/setters, other methods)
    ... 
    public boolean add(Sprocket newEntry, int quantity) {
        int counter = 0;
        for (int i = 0; i < quantity; i++) {
            ensureCapacity(); //resizes array as necessary
            warehouseContents[numberOfEntries] = newEntry;
            numberOfEntries++;
            counter++;
        }
        return (counter == quantity);
    }//end add method
    ...
    public void turnAll() {
        for (int i = 0; i < numberOfEntries; i++) {
            warehouseContents[i].turn();
        }
    }//end turnAll method
    ...
}//end VirtualWarehouse class 


public class WoodenPocket extends Pocket {
    ...
    @Override // overrides empty methods in Sprocket and Pocket
    public void turn() {
        if (getOrientation() > 359) {//modulate orientation so it remains between 0-359
            setOrientation(getOrientation() - 360 + 10);
        } else {
            setOrientation(getOrientation() + 10);
        }
    }//end turn method 
    ...
}//end WoodenPocket class 

如果我在仓库中运行带有4个WoodenPockets并且起始方向为0的turn()方法,结果将是每个WoodenPocket对象的方向为40.我猜测属性的方式存在问题与调用方法有关,但我不明白是什么。

我之前从未遇到类似这样的事情(授予这只是我的第二个学期),并且我没有成功尝试各种方法来欺骗代码工作(使对象成为转弯的参数,使转弯返回需要转动的度数等)。我还用调试器检查了代码,我只是迷失了。

BTW这是一项家庭作业。我真的只是想了解为什么代码正在做它正在做的事情。

谢谢!

编辑: 我将349改为359。 这是我的程序的一些输入/输出的复制/粘贴。大括号中的数字是当前Sprockets的方向。

Enter would you like to do (ADD, REMOVE, TURN, RESET, REPORT, SAVE, or LOAD): add 
Enter what type of Sprocket you would like to add (POCKET or NOCKET): pocket  
Enter what variation of POCKET you would like to add (RED, WOODEN, or TITANIUM): wooden 
What are these sprockets' current orientation (between 0-359 degrees): 0
How many of this type of sprocket would you like to add: 5

Would you like to do more (YES or NO): yes
Enter would you like to do (ADD, REMOVE, TURN, RESET, REPORT, SAVE, or LOAD): report

This warehouse contains sprockets with an orientation between: 
0-59: 5  { 0 0 0 0 0 }
60-119: 0  { }
120-179: 0  { }
180-239: 0  { }
240-299: 0  { }
300-359: 0  { }

Would you like to do more (YES or NO): yes
Enter would you like to do (ADD, REMOVE, TURN, RESET, REPORT, SAVE, or LOAD): turn

Would you like to do more (YES or NO): yes
Enter would you like to do (ADD, REMOVE, TURN, RESET, REPORT, SAVE, or LOAD): report

This warehouse contains sprockets with an orientation between:
0-59: 5  { 50 50 50 50 50 }
60-119: 0  { }
120-179: 0  { }
180-239: 0  { }
240-299: 0  { }
300-359: 0  { }

1 个答案:

答案 0 :(得分:0)

首先,我相信你的转弯方法应该检查359而不是349,尽管我可能会错误解释你的代码。

你的转弯方法看起来很好,所以你认为每次添加链轮或类似东西时你可能会调用你的主类吗?我认为最可能的罪魁祸首是你不止一次调用turnAll方法。

我有一个问题。您是否已将链轮添加到阵列中,然后检查了它们的所有方向,然后在不使用主类的情况下调用阵列上的turnAll方法?如果没有,请告诉我结果。