2015-10-14 75 views
0

我有一個大學任務來做一個程序。我正在用java編寫。除了使用totalCost()方法計算訂購系統產品的總成本的一個步驟外,我幾乎完成了它。如何編寫所需的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()方法,甚至如何使用它。

+0

當你收到用戶的選擇? –

+0

你的方法在哪裏?在哪個級別 ?注意:把你的進程放在一個類中,並從main方法調用它來使用OO編碼! – rlm

+0

如果不知道作業是什麼,很難說清楚。事實上,你的程序只是接收輸入並顯示它,它沒有太多實際的邏輯。 – RealSkeptic

回答

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(); 
} 

這些只是簡單的想法,因爲我不知道你應該怎麼做......