Results 1 to 4 of 4
  1. #1
    kevinkhan is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Simple question about this class

    When a user clicks 1 in the do while loop. it will enter case 1 and method option1(pl) will execute. After this method has exectued how does it get back into the loop and display the menu again???

    Java Code:
    public class PaymentListTester {
        public static void main(String[] args) {
            char choice;
            int total;
            // declare PaymentList object to test
            PaymentList pl;
            // get size of list
            System.out.print("\nMaximum number of payments? ");
            total = EasyIn.getInt();
            //create PaymentList object to test
            pl = new PaymentList(total);
    
            do {
                System.out.println("\n[1] Add a payment");
                System.out.println("[2] List all payments");
                System.out.println("[3] Get number of payments made");
                System.out.println("[4] Get total payments made");
                System.out.println("[5] Quit");
                System.out.println("\nEnter a choice [1,2,3,4,5]: ");
    
                //get choice
                choice = EasyIn.getChar();
    
                switch(choice){
                    case '1': option1(pl); break;
                    case '2': option2(pl); break;
                    case '3': option3(pl); break;
                    case '4': option4(pl); break;
                    case '5': System.out.println("\n\nBYE"); break;
                    default: System.out.print("\n1-5 only");
                }
    
            } while(choice !='5');
    
    
    
        }
       // Static Worker Methods
    
        // add payment
        private static void option1(PaymentList listIn){
           if(!listIn.isFull()){
              System.out.print("\nEnter Month: \t");
              String month = EasyIn.getString();
              System.out.print("Enter amount: \t");
              double amount = EasyIn.getDouble();
              listIn.add(new Payment(month, amount));
           }
           else{
              System.out.println("\n!!! SORRY, LIST IS FULL!!!");
           }
       }
    
        // display payment
        private static void option2(PaymentList listIn){
            System.out.print("\nMONTH \tAMOUNT\n"); // header
            // loop through payments in list
            for(int i =1; i<=listIn.getTotal();i++){
                Payment p = listIn.getPayment(i);
                System.out.print(p.getMonth());
                System.out.println("\t" + p.getAmount());
            }
        }
    
        // Get total number of payments
        private static void option3(PaymentList listIn){
            System.out.print("\nTotal number of payments made: ");
            System.out.println(listIn.getTotal());
        }
    
        // get total of payments made
        private static void option4(PaymentList listIn){
            System.out.print("\ntotal sum of payments made: ");
            System.out.println(listIn.calculateTotalPaid());
        }
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Simple question about this class

    It never leaves the loop, so it doesn't need to get back into it...
    Or have I misunderstood your question?

  3. #3
    knightwriter is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Simple question about this class

    I would change out the switch for a loop and then add a control variable. Or add a loop after after the switch to get back into the display menu again. Something like asking if they are done with their transaction..."y" or "n."

  4. #4
    kevinkhan is offline Member
    Join Date
    Nov 2011
    Posts
    13
    Rep Power
    0

    Default Re: Simple question about this class

    Quote Originally Posted by Tolls View Post
    It never leaves the loop, so it doesn't need to get back into it...
    Or have I misunderstood your question?
    Oh sorry , so the break; is only to get out of the switch statement. at first i taught it was to break out of the loop.

Similar Threads

  1. A simple question
    By chris.bos in forum New To Java
    Replies: 11
    Last Post: 12-02-2011, 05:37 PM
  2. simple question
    By agater in forum New To Java
    Replies: 5
    Last Post: 11-07-2011, 02:22 AM
  3. Please help. Simple question
    By owencain in forum New To Java
    Replies: 14
    Last Post: 06-16-2011, 01:07 AM
  4. some simple question?
    By jperson in forum New To Java
    Replies: 4
    Last Post: 05-03-2010, 05:32 PM
  5. Simple Question
    By barusk in forum Networking
    Replies: 13
    Last Post: 03-04-2009, 07:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •