如何改进我的代码以使其更标准?

时间:2014-12-06 08:16:23

标签: java

public class Hw {
    public static void main(String[] args) {

        // For you to test
        int[][] device = {{10, 4, 05, 6, 7}, {07, 0, 12, 1, 3},
                {04, 9, 05, 0, 8}, {03, 2, 01, 5, 6}};

        // TO PRINT 2D ARRAY
        printArray(device);

        double[] cost = {10.95, 6.30, 14.74, 11.25, 5.0};

        // TO PRINT SUM OF COMPONENT 1
        System.out.println();
        double device1 = deviceOne(device, cost);
        System.out.println("Device 1 total: " + device1);

        // TO PRINT SUM OF COMPONENT 2
        double device2 = deviceTwo(device, cost);
        System.out.println("Device 2 total: " + device2);

        // TO PRINT SUM OF COMPONENT 3
        double device3 = deviceThree(device, cost);
        System.out.println("Device 3 total: " + device3);

        // TO PRINT SUM OF COMPONENT 4
        double device4 = deviceFour(device, cost);
        System.out.println("Device 4 total: " + device4);


        // TO GET 10%
        System.out.println(" ");
        double device1PlusX = deviceOnePlus(device, cost);
        System.out.println("Device 1 plus 10%: " + device1PlusX);

        double device2PlusX = deviceTwoPlus(device, cost);
        System.out.println("Device 2 plus 10%: " + device2PlusX);

        double device3PlusX = deviceThreePlus(device, cost);
        System.out.println("Device 3 plus 10%: " + device3PlusX);

        double device4PlusX = deviceFourPlus(device, cost);
        System.out.println("Device 4 plus 10%: " + device4PlusX);

    }// END MAIN METHOD


    // TO DISPLAY ARRAY
    private static void printArray(int[][] array) {
        for (int row = 0; row < array.length; row++) {
            for (int col = 0; col < array[row].length; col++) {
                System.out.print(array[row][col] + " ");
            }
            System.out.println();
        }
    }

    // CALCULATE THE TOTAL COST OF EACH DEVICE
    private static double deviceOne(int[][] device, double[] cost) {
        double h1 = device[0][0] * cost[0];
        double h2 = device[0][1] * cost[1];
        double h3 = device[0][2] * cost[2];
        double h4 = device[0][3] * cost[3];
        double h5 = device[0][4] * cost[4];
        double sumOfDevice1 = h1 + h2 + h3 + h4 + h5;
        return sumOfDevice1;
    }

    private static double deviceTwo(int[][] device, double[] cost) {
        double h1 = device[1][0] * cost[0];
        double h2 = device[1][1] * cost[1];
        double h3 = device[1][2] * cost[2];
        double h4 = device[1][3] * cost[3];
        double h5 = device[1][4] * cost[4];
        double sumOfDevice2 = h1 + h2 + h3 + h4 + h5;
        return sumOfDevice2;
    }

    private static double deviceThree(int[][] device, double[] cost) {
        double h1 = device[2][0] * cost[0];
        double h2 = device[2][1] * cost[1];
        double h3 = device[2][2] * cost[2];
        double h4 = device[2][3] * cost[3];
        double h5 = device[2][4] * cost[4];
        double sumOfDevice3 = h1 + h2 + h3 + h4 + h5;
        return sumOfDevice3;
    }

    private static double deviceFour(int[][] device, double[] cost) {
        double h1 = device[3][0] * cost[0];
        double h2 = device[3][1] * cost[1];
        double h3 = device[3][2] * cost[2];
        double h4 = device[3][3] * cost[3];
        double h5 = device[3][4] * cost[4];
        double sumOfDevice4 = h1 + h2 + h3 + h4 + h5;
        return sumOfDevice4;
    }
//END CALCULATING THE SUM


    private static double deviceOnePlus(int[][] device, double[] cost) {

        double deviceX = deviceOne(device, cost);
        double device1Plus = deviceX + deviceX * 0.10;
        return device1Plus;

    }

    private static double deviceTwoPlus(int[][] device, double[] cost) {

        double deviceX = deviceTwo(device, cost);
        double device2Plus = deviceX + deviceX * 0.10;
        return device2Plus;

    }

    private static double deviceThreePlus(int[][] device, double[] cost) {

        double deviceX = deviceThree(device, cost);
        double device3Plus = deviceX + deviceX * 0.10;
        return device3Plus;

    }

    private static double deviceFourPlus(int[][] device, double[] cost) {

        double deviceX = deviceFour(device, cost);
        double device4Plus = deviceX + deviceX * 0.10;
        return device4Plus;

    }
}
//

我觉得我硬编码了这个问题。有没有办法解决这个问题。我正在为设备使用2维数组。我按照这个enter image description here去做 我正在使用1维数组的价格。然后我获得所有组件并乘以价格,我添加所有组件,我得到设备成本。对于b部分,我将10%的成本加到总成本上。我认为这是正确的,但是我希望它能在不同的地方更高效。

1 个答案:

答案 0 :(得分:2)

您可以将1d数组传递给单个方法,而不是重复每个设备的方法:

double device1 = deviceValue(device[0], cost); //note use of [0]
System.out.println("Device 1 total: " + device1);

// CALCULATE THE TOTAL COST OF ANY DEVICE
private static double deviceValue(int[] device, double[] cost) {
    double sumOfDevice = 0;
    for(int i = 0; i < 5; i++) { //also use loop here
        sumOfDevice += device[i] * cost[i];
    }
    return sumOfDevice;
}

private static double deviceValuePlus(int[] device, double[] cost) {
    double deviceX = deviceValue(device, cost);
    double devicePlus= deviceX * 1.10;
    return devicePlus;
}

标准方法是将对象用于设备,但问题是要使用2D数组。您仍然可以删除所有重复项。见D.R.Y.