我的教授给了我以下代码。
public static void main(String[] args) {
Customer customer;
Transaction transaction;
double withdrawalAmount = 0;
boolean finished = false;
while (finished == false)
{
// Menu Display and Get user input
int inputInt = 0;
while (inputInt == 0)
{
inputInt = displayMenuAndGetInput();
// if the input is out of range
if ((inputInt < 1) || (inputInt > 8))
{
System.out.println("\nThe input is out of range!");
System.out.println();
inputInt = 0;
}
} //end while
// switch to correspondence function
switch (inputInt)
{
case 1:
customer = createPersonalCustomer();
System.out.println("\nThe Personal customer has been created: \n" + newPC.toString());
customers.add(customer);
break;
case 2:
customer = createCommercialCustomer();
System.out.println("\nThe Commercial customer has been created: \n" + newCC.toString());
customers.add(customer);
break;
case 3:
transaction = recordTransaction();
if(transaction != null)
System.out.println("\nThe Transaction has been created: \n" + trans.toString());
else
System.out.println("\nThe ID could not be found.");
break;
case 4:
withdrawalAmount = makeWithdrawal();
if(withdrawalAmount > 0)
System.out.println("\nAmount withdrawn from this account: " + moneyFormat.format(acct.getMakeWithdrawal()) + "\n");
else
System.out.println("\nThe ID could not be found.");
break;
case 5:
displayCustomer();
break;
case 6:
displayCustomerSummary();
break;
case 7:
displayGrandSummary();
break;
case 8:
// exit
finished = true;
break;
default:
System.out.println("Invalid Input!");
break;
} // end switch
} // end while
}
我应该采取以下代码
// Create a new Transaction
public static Transaction recordTransaction(){}
并创建一个适用于以下场景的循环:
输入客户ID,如果客户ID在阵列中不匹配,则会生成案例3中读出的错误并显示主菜单。如果客户ID有效,则用户输入以下输入信息。
以下是我的代码
public static Transaction recordTransaction(){
System.out.println("Enter the customer ID to create the transaction > ");
long customerID = scan.nextLong();
for (Customer c : customers) {
if (c.getCustomerID() == customerID) {
if (trans != null) {
System.out.println("\nEnter the weight of gold > ");
Transaction.goldWt = scan.nextDouble();
System.out.println("\nEnter the weight of platinum > ");
Transaction.platinumWt = scan.nextDouble();
System.out.println("\nEnter the weight of silver > ");
Transaction.silverWt = scan.nextDouble();
}
}
return null;
}
Anywho,我已经通过多种方式运行,我的代码将接受无效且有效的客户ID,或者它不会接受无效或有效的客户ID。 我知道我可能会忽略一些事情,这就是我迫切要求论坛帮助的原因。在编程时我有OCD倾向,这是我的第一个介绍java类,所以我不熟悉这门语言。过去两天我一直坚持这个问题。请帮忙。
答案 0 :(得分:1)
您需要在方法new Transaction()
中实例化recordTransaction()
并在适当的时候返回。
public static Transaction recordTransaction(){
System.out.println("Enter the customer ID to create the transaction > "); long customerID = scan.nextLong();
for (Customer c : customers) {
if (c.getCustomerID() == customerID) {
Transaction trans = new Transaction();
System.out.println("\nEnter the weight of gold > ");
trans.goldWt = scan.nextDouble();
System.out.println("\nEnter the weight of platinum > ");
trans.platinumWt = scan.nextDouble();
System.out.println("\nEnter the weight of silver > ");
trans.silverWt = scan.nextDouble();
return trans;
}
}
return null;
}
答案 1 :(得分:0)
关于您的代码的一些评论:
if (trans != null) {
什么是“反式”?我想它必须引用一个Transaction实例。
Transaction.goldWt = ...
不应该是“trans.goldWt”吗? (当然,植物和银也是一样的)。如果它真的是Transaction.goldWt
那么你总是改变那个单一的价值。
return null;
你永远不会返回任何东西,因此调用代码总是变为“无ID”。你不应该“return trans
”吗?
答案 2 :(得分:0)
问题是由正在创建的每个帐户的随机customerID的getter和setter方法生成的。
谢谢 的Merci!