Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: A Loop statement and a switch statement issue

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    San Francisco
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default A Loop statement and a switch statement issue

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Location
    San Francisco
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: A Loop statement and a switch statement issue

    Quote Originally Posted by Norm View Post
    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?
     // 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: A Loop statement and a switch statement issue

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Location
    San Francisco
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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();
     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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: A Loop statement and a switch statement issue

    why is this statement used?
    if (trans != null) {

    Where does the method create an instance of the Transaction class that it is supposed to return?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Location
    San Francisco
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: A Loop statement and a switch statement issue

    ok if I remove the if (trans != null) error

     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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: A Loop statement and a switch statement issue

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Location
    San Francisco
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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):

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: A Loop statement and a switch statement issue

    1362672823 was just entered in the same session.
    Where? I don't see it in the post.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jan 2013
    Location
    San Francisco
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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):

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    sternfox (March 7th, 2013)

  14. #13
    Junior Member
    Join Date
    Jan 2013
    Location
    San Francisco
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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.

     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?

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: A Loop statement and a switch statement issue

    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?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How do I loop a switch Statement?
    By Arkeshen in forum Java Theory & Questions
    Replies: 10
    Last Post: August 2nd, 2018, 07:47 AM
  2. for-each loop and a switch statement
    By Grot in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 4th, 2013, 02:02 PM
  3. Replies: 9
    Last Post: February 24th, 2013, 06:51 PM
  4. Do you add objects to an array when using switch statement with a For loop?
    By TheWhopper858 in forum Collections and Generics
    Replies: 2
    Last Post: November 13th, 2011, 01:28 PM
  5. Switch statement inside a for loop need help.
    By TheWhopper858 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 12th, 2011, 11:50 PM