如何编写所需的totalCost方法?

时间:2015-10-14 13:32:08

标签: java methods

我有一个大学的任务来做一个程序。我在用java写作。我已经完成了它,除了使用totalCost()方法的一个步骤,以便从订购系统计算产品的总成本。

private static double totalCost(int number, double cost, double salesTaxRate)

这是我必须使用的totalCost()方法,这是我到目前为止的代码。

import javax.swing.JOptionPane;

/**
 * @author john
 */
@SuppressWarnings("null")
public class EdenOfGamingPhase1 {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // declare variables
        String openingMsg, nameInputMsg, customerName, nameOutputMsg, 
               returnInputMsg, customerReturn, returnOutputMsg, 
               greetingOutputMsg, outputMsg, colorInputMsg, customerColor, colorOutputMsg, featureInputMsg, featureSelection, featureOutputMsg, productAmt, productInputMsg, productOutputMsg, totalPriceAmt;

        // display opening message
        openingMsg = "*** Welcome to the Eden of Gaming Online Ordering System ***\n"
                   + "                     Thank you for choosing the Nosy Entertainment Station 4 !";
        JOptionPane.showMessageDialog(null, openingMsg);

        // get required input using dialogs
        nameInputMsg   = "Please enter your name: ";
        customerName   = getstringInput(nameInputMsg);
        returnInputMsg = "Are you a returning customer (yes or no)? ";
        customerReturn = getstringInput(returnInputMsg);
        colorInputMsg = "Please enter what color you would like you product to be (red,blue,green,etc).";
        customerColor = getstringInput(colorInputMsg);
        featureInputMsg = "Please select which system model you would like (Brownray or Internet";
        featureSelection = getstringInput(featureInputMsg);
        productInputMsg = "Please select how many systems you wish to order.";
        productAmt = getstringInput(productInputMsg);


        // build output strings
        nameOutputMsg     = "Welcome " + customerName + ".\n\n";
        returnOutputMsg   = "Your return customer status is " + customerReturn + ".\n\n";
        colorOutputMsg = "Your Nosy Entertainment Station 4 is color " + customerColor + ".\n";
        featureOutputMsg ="You have selected the system model. " + featureSelection + ".\n";
        productOutputMsg = "You have chosen number of systems " + productAmt + ".\n";
        totalPriceAmt ="The total cost for your product is $. " + "\n";
        greetingOutputMsg = "Thank you for shopping at The Eden of Gaming!" + "\n\n"
                          + "You should be transferred to the checkout screen in less than 10 seconds.\n";

        // create and display output string
        outputMsg = nameOutputMsg + returnOutputMsg + colorOutputMsg + featureOutputMsg + productOutputMsg + totalPriceAmt + greetingOutputMsg;
        JOptionPane.showMessageDialog(null, outputMsg);

        System.exit(0);
    } // end main()

    private static String getstringInput(String prompt){
        int count = 0;
        String input;
        input = JOptionPane.showInputDialog( prompt );
        while ((input != null  && input.length() == 0) && (count <2)){
            input = JOptionPane.showInputDialog("Error: You must make a selection. \n" + prompt);
            count++;
        }
        if (count==2){
            JOptionPane.showMessageDialog(null, "We did not recieve a selection please try again to complete your order. PROGRAM TERMINATED.");
            System.exit(0);
        }
        return input;

    }
} // end class PizzasRUsPhase1

我只是不确定从哪里开始totalCost()方法,甚至不知道如何使用它。

1 个答案:

答案 0 :(得分:0)

我不知道你想要做什么,但基本上你应该使用对象来做到这一点。给对象(用户可以购买的产品)一个价格,这样的话可能是:

public class Product(){
    public Product(int price, String name){ 
        this.name = name;
        this.price = price;
    }    
}

然后你可以有一个getPrice()类:

public int getPrice(){
    return this.price;
}

你可以打电话来计算价格,如下所示:

int totalCost = product1.getPrice() + product2.getPrice() ...;

或者如果列表中包含所有产品:

int totalCost = 0;
for (Product product : productList){
    totalCost += product.getPrice();
}

这些只是简单的想法,因为我不知道你应该做什么......