Reputation: 13
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 > ");
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, 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.
Upvotes: 1
Views: 172
Reputation: 13
The issue was being generated by the getter and setter method for the random customerID for each account being created.
Thank you Merci!
Upvotes: 0
Reputation: 39338
Some remarks about your code:
if (trans != null) {
What is "trans"? I guess it must refer to a Transaction instance.
Transaction.goldWt = ...
Shouldn't that be "trans.goldWt"? (and the same for plantinum and silver, of course). If it's really Transaction.goldWt
then you are always changing that single value.
return null;
you never return anything so the calling code always goes to "no ID". Shouldn't you "return trans
"?
Upvotes: 0
Reputation: 2848
you need to instantiate a new Transaction()
within the method recordTransaction()
and return it when appropriate.
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;
}
Upvotes: 1