Java 如何编写所需的totalCost方法?
我有一个大学作业要做一个项目。我在用java写东西。除了使用Java 如何编写所需的totalCost方法?,java,methods,Java,Methods,我有一个大学作业要做一个项目。我在用java写东西。除了使用totalCost() private static double totalCost(int number, double cost, double salesTaxRate) 这是我必须使用的totalCost()方法,这是我到目前为止使用的代码 import javax.swing.JOptionPane; /** * @author john */ @SuppressWarnings("null") public cla
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
import javax.swing.JOptionPane;
/**
*@作者约翰
*/
@抑制警告(“空”)
公共级别的网格相位1{
/**
*@param args
*/
公共静态void main(字符串[]args){
//声明变量
字符串openingMsg、nameInputMsg、customerName、nameOutputMsg、,
returnInputMsg、customerReturn、returnOutputMsg、,
欢迎outputMsg、outputMsg、colorInputMsg、customerColor、colorOutputMsg、featureInputMsg、featureSelection、featureOutputMsg、productAmt、productInputMsg、productOutputMsg、totalPriceAmt;
//显示开场白
openingMsg=“***欢迎使用游戏伊甸园在线订购系统***\n”
+“感谢您选择爱管闲事的娱乐台4!”;
showMessageDialog(null,openingMsg);
//使用对话框获取所需的输入
nameInputMsg=“请输入您的姓名:”;
customerName=getstringInput(nameInputMsg);
returnInputMsg=“您是退货客户(是或否)?”;
customerReturn=getstringInput(returnInputMsg);
colorInputMsg=“请输入您希望产品的颜色(红色、蓝色、绿色等)。”;
customerColor=getstringInput(colorInputMsg);
featureInputMsg=“请选择您想要的系统型号(Brownray或Internet”;
featureSelection=getstringInput(featureInputMsg);
productInputMsg=“请选择您希望订购的系统数量。”;
productAmt=getstringInput(productInputMsg);
//生成输出字符串
nameOutputMsg=“欢迎”+customerName+”\n\n;
returnOutputMsg=“您的退货客户状态为“+customerReturn+”\n\n”;
colorOutputMsg=“您的爱管闲事的娱乐台4是color”+customerColor+”\n”;
featureOutputMsg=“您已选择系统模型。”+featureSelection+”\n”;
productOutputMsg=“您已选择系统数”+productAmt+“\n”;
totalPriceAmt=“您的产品的总成本为$。”+“\n”;
greetingOutputMsg=“感谢您在游戏伊甸园购物!”+“\n\n”
+“您应在10秒内转到签出屏幕。\n”;
//创建并显示输出字符串
outputMsg=nameOutputMsg+returnOutputMsg+colorOutputMsg+featureOutputMsg+productOutputMsg+totalPriceAmt+greetingOutputMsg;
showMessageDialog(null,outputMsg);
系统出口(0);
}//结束main()
私有静态字符串getstringInput(字符串提示){
整数计数=0;
字符串输入;
input=JOptionPane.showInputDialog(提示);
虽然((input!=null&&input.length()==0)&(count我不知道你到底想做什么,但基本上你应该使用对象来做这件事。给对象(用户可以购买的产品)一个价格,类似这样的可能:
public class Product(){
public Product(int price, String name){
this.name = name;
this.price = price;
}
}
然后您可以拥有一个getPrice()类:
你可以打电话来计算价格,比如:
int totalCost = product1.getPrice() + product2.getPrice() ...;
或者,如果列表中包含所有产品:
int totalCost = 0;
for (Product product : productList){
totalCost += product.getPrice();
}
这些只是简单的想法,因为我不知道在收到用户的选择后,您应该做什么…您的方法在哪里?在哪个类中?注意:将您的进程放在一个类中,并从主方法调用它来使用OO编码!如果不知道任务是什么,很难判断。实际上,您的程序只接收输入并显示它,它没有太多实际逻辑。这是实际分配4。创建一个名为totalCost()的方法,用于计算订单的总成本。使用以下标题:private static double totalCost(整数、双成本、双销售税率)number是项目数(数量),cost是单位成本(单个项目的成本),salesTax是销售税率。您应该为单位成本选择一个合理的值(例如$10.00)。您应该为销售税率选择一个合理的值(例如7%)。使用totalCost()方法计算订单的总成本。
int totalCost = 0;
for (Product product : productList){
totalCost += product.getPrice();
}