I was given the following code by my prof.
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 }
I am supposed take the following code
// Create a new Transaction public static Transaction recordTransaction(){ }
and make a loop that works in the following scenario:
the customer id is entered, and if the customer id is not matched in the array, the error read out in case 3 is generated and the main menu is displayed. If the customer id is valid, the user enters the input info below.
Below is my code
public static Transaction recordTransaction(){ System.out.println("Enter the customer ID to create the transaction > "); newC.customerID = scan.nextLong(); for(int i = 0; i < customers.size(); i++) { if(customers.get(i).getCustomerID() == newC.getCustomerID()) { 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; }
newC refers to the Customer object I created.
Anywho, I've run this a number of ways and either my code will accept an invalid and valid customer ID, or it will not accept an invalid or valid customer id.
I know I am probably overlooking something and that is why I am desperately requesting the help of the forum. I have OCD tendencies when it comes to programming and this is my first intro java class so I am not well versed in the language. I have been stuck on this issue for the last two days. Please help.