A Loop statement and a switch statement issue
I was given the following code by my prof.
Code Java:
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
Code Java:
// 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
Code Java:
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.
Re: A Loop statement and a switch statement issue
Is there a need to create the newC object? Can the code use a local variable to hold the data read from the Scanner object?
The method is supposed to return a Transaction object. It should ask the user for all the data needed to create a Transaction object, then call the Transaction class's constructor using a new statement and return that newly created object to the caller.
Re: A Loop statement and a switch statement issue
Quote:
Originally Posted by
Norm
Is there a need to create the newC object? No, I do not think there is a need to create the newC object.Can the code use a local variable to hold the data read from the Scanner object?Yes, I can create a local variable to hold the information from the scanner.
The method is supposed to return a Transaction object. It should ask the user for all the data needed to create a Transaction object, then call the Transaction class's constructor using a new statement and return that newly created object to the caller.
This is the part that is tripping me up. In addition I have the following helper method. Could I use it in this situation?
Code Java:
// Helper method to determine if a customer exists
public static Customer findCustomer(long customerID){
for (Customer c : customers) {
if (c.getCustomerID() == customerID)
return c;
}
return null;
}
thank you for your help...it is really appreciated...sorry, I am having a difficult time understanding.
Re: A Loop statement and a switch statement issue
Quote:
Could I use it in this situation?
What is the problem you are trying to solve in "this situation"?
Does the findCustomer() method solve that problem for you?
Re: A Loop statement and a switch statement issue
The problem that i am trying to solve in this situation is the following:
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 additional information. the problem I am running into is with the Transaction recordTransaction();
Code Java:
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;
}
now every entry I submit returns customerID not found.
Re: A Loop statement and a switch statement issue
why is this statement used?
Where does the method create an instance of the Transaction class that it is supposed to return?
Re: A Loop statement and a switch statement issue
ok if I remove the if (trans != null) error
Code Java:
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;
}
I still either get all valid responses or all invalid responses.
Re: A Loop statement and a switch statement issue
Quote:
I still either get all valid responses or all invalid responses.
Can you explain when the responses are valid
and when they are invalid?
What is different from when the response is valid
and when it is invalid?
Please copy the contents of the console from when you execute the program what shows everything that happened when the program executed?
Re: A Loop statement and a switch statement issue
So the following is correct because account 123 does not exist.
*** ***
*** Welcome to Cash for Metals Calculator!!! ***
*** ***
1. Create Personal Customer
2. Create Commercial Customer
3. Record Transaction
4. Make Withdrawal
5. Display Customer
6. Display Customer Summary
7. Display Grand Summary
8. Exit
Please input your choice (1-8): 3
Enter the customer ID to create the transaction >
123
The ID could not be found.
*** ***
*** Welcome to Cash for Metals Calculator!!! ***
*** ***
1. Create Personal Customer
2. Create Commercial Customer
3. Record Transaction
4. Make Withdrawal
5. Display Customer
6. Display Customer Summary
7. Display Grand Summary
8. Exit
Please input your choice (1-8):
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++
the following is not correct because the customer for 1362672823 was just entered in the same session.
*** ***
*** Welcome to Cash for Metals Calculator!!! ***
*** ***
1. Create Personal Customer
2. Create Commercial Customer
3. Record Transaction
4. Make Withdrawal
5. Display Customer
6. Display Customer Summary
7. Display Grand Summary
8. Exit
Please input your choice (1-8): 3
Enter the customer ID to create the transaction >
1362672823
The ID could not be found.
*** ***
*** Welcome to Cash for Metals Calculator!!! ***
*** ***
1. Create Personal Customer
2. Create Commercial Customer
3. Record Transaction
4. Make Withdrawal
5. Display Customer
6. Display Customer Summary
7. Display Grand Summary
8. Exit
Please input your choice (1-8):
Re: A Loop statement and a switch statement issue
Quote:
1362672823 was just entered in the same session.
Where? I don't see it in the post.
Re: A Loop statement and a switch statement issue
Here posting the entire process:
*** ***
*** Welcome to Cash for Metals Calculator!!! ***
*** ***
1. Create Personal Customer
2. Create Commercial Customer
3. Record Transaction
4. Make Withdrawal
5. Display Customer
6. Display Customer Summary
7. Display Grand Summary
8. Exit
Please input your choice (1-8): 1
Enter the customer name >
John Doe
Enter the customer address >
123 Grove Ave
Enter the customer home phone >
123-456-7890
Enter the customer work phone >
132-456-7890
The Personal customer has been created:
Customer Name: John Doe
Customer ID: 1362673167
Customer Address: 123 Grove Ave
Account Number: 1362673167
Balance: $0.00
Interest Rate: 0.03
Date Opened: Thu Mar 07 08:19:10 PST 2013
Home Phone: 123-456-7890
Work Phone: 132-456-7890
*** ***
*** Welcome to Cash for Metals Calculator!!! ***
*** ***
1. Create Personal Customer
2. Create Commercial Customer
3. Record Transaction
4. Make Withdrawal
5. Display Customer
6. Display Customer Summary
7. Display Grand Summary
8. Exit
Please input your choice (1-8): 3
Enter the customer ID to create the transaction >
123
The ID could not be found.
*** ***
*** Welcome to Cash for Metals Calculator!!! ***
*** ***
1. Create Personal Customer
2. Create Commercial Customer
3. Record Transaction
4. Make Withdrawal
5. Display Customer
6. Display Customer Summary
7. Display Grand Summary
8. Exit
Please input your choice (1-8): 3
Enter the customer ID to create the transaction >
1362673167
The ID could not be found.
*** ***
*** Welcome to Cash for Metals Calculator!!! ***
*** ***
1. Create Personal Customer
2. Create Commercial Customer
3. Record Transaction
4. Make Withdrawal
5. Display Customer
6. Display Customer Summary
7. Display Grand Summary
8. Exit
Please input your choice (1-8):
Re: A Loop statement and a switch statement issue
You need to do some debugging to show what the code is doing.
Add lots of printlns to print out the values of the variables used it the code.
For example:
Print out the value of c inside the for loop each time the loop gets a new value in c.
Print out the contents of the customers collection so that you see what it contains.
Make sure the Customer class has a toString() method that returns a nice String to show the contents of the Customer class.
Re: A Loop statement and a switch statement issue
Okay, I discovered that the issue is with the following:
we are asked to use the following code to generate a unique customerID number.
Code Java:
long customerID = Calendar.getInstance().getTimeInMillis()/1000;
this does not allow me to lookup the customerID.
However, if I set the customerID = 4 (or any other number), the programs works like it is supposed to; it brings up the right information.
How can I fix the issue with the random customerID number in order to get it to work as intended?
Re: A Loop statement and a switch statement issue
Quote:
does not allow me to lookup the customerID.
Please explain what the problem is. As long as each object has a unique id, there shouldn't be a problem.
For debugging add a println statement in the Customer class's constructor that prints out the value of customerID when the object is created. Once it is set, should it remain the same for that customer?