我的教授给了我以下代码。
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,则产生case 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;
}
任何人,我已经以多种方式运行此程序,并且我的代码将接受无效和有效的客户 ID,或者它不会接受无效或有效的客户 ID。我知道我可能忽略了一些东西,这就是为什么我拼命请求论坛的帮助。在编程方面,我有强迫症倾向,这是我的第一个 Java 入门课程,所以我不太精通这门语言。在过去的两天里,我一直被困在这个问题上。请帮忙。