[英]How do I write the required totalCost method?
我有一个大学任务来做一个程序。 我在用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()
方法,甚至如何使用它。
我不知道您到底想做什么,但是基本上您应该使用对象来做到这一点。 给对象(用户可以购买的产品)定价,如下所示:
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();
}
这些只是简单的想法,因为我不知道您应该怎么做...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.