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 4 of 4

Thread: Customer counting program - while loop

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Customer counting program - while loop

    I'm also creating a program that's meant to be a customer counter, in that it'll add 1 to a customer variable every time a customer enters the store, so to speak. I don't know how to make the loop stop, though. When it's run, it'll just loop repeatedly until it gets to 100, but I want it to only add 1 if I hit 'yes' in the confirmation dialog box. How do I go about doing that?

    Here's my code:

    import javax.swing.JOptionPane;
     
    public class CustomerCounter {
     public static void main(String[] args) {
     
     int customer = 0;
     int storeLimit = 100;
     
      int answer = JOptionPane.showConfirmDialog(null, "Has a customer entered the store?");
       if (answer == JOptionPane.YES_OPTION){
     
    	  do {System.out.println("There are " + customer + " customers in the store.");
    	      customer++;
    	  } while (customer < storeLimit);
    	}
    	if (customer == 100){
    	 JOptionPane.showMessageDialog(null, "This customer wins a free book! Yay!");
    	}
     
     }
    }


  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: Customer counting program - while loop

    Can you write out the steps and events you want your code to do when it executes?

    The posted code does this:
    ask user a question
    if user answers yes: loop 100 times
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Customer counting program - while loop

    Quote Originally Posted by Shenaniganizer View Post
    ...how to make the loop stop, though. When it's run, it'll just loop repeatedly until it gets to 100, but I want it to only add 1 if I hit 'yes' in the confirmation dialog box. How do I go about doing that?
    If you want to wait again, then call the method again each time.
    Pseudo-code could go something like the following:

    Declare integer variables numCustomers and maxCustomers.
    Initialize customer to zero and maxCustomers to whatever...
     
    Repeat the following loop as long as numCustomers is less than maxCustomers.
    BEGIN LOOP
       Prompt user with showConfirmDialog
     
       IF (The answer from showConfirmDialog is equal to CANCEL_OPTION) THEN
          Break out of the loop.
       END IF
     
       IF (The answer from showConfirmDialog is equal to YES_OPTION) THEN
          Increment numCustomers.
          Do whatever else you need to do when user clicks "Yes."
       END IF
     
    END LOOP


    Cheers!

    Z

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Customer counting program - while loop

    Sorry!

    I'd like the code to do this:

    ask user a question
    user answers yes, 1 is added to customer count (customer variable)
    user has to answer yes each time for 1 to be added to count
    once customer reaches 100, message dialog comes up saying that customer won a book

    You know what I'm sayin'?

    --- Update ---

    Quote Originally Posted by Zaphod_b View Post
    If you want to wait again, then call the method again each time.
    Pseudo-code could go something like the following:

    Declare integer variables numCustomers and maxCustomers.
    Initialize customer to zero and maxCustomers to whatever...
     
    Repeat the following loop as long as numCustomers is less than maxCustomers.
    BEGIN LOOP
       Prompt user with showConfirmDialog
     
       IF (The answer from showConfirmDialog is equal to CANCEL_OPTION) THEN
          Break out of the loop.
       END IF
     
       IF (The answer from showConfirmDialog is equal to YES_OPTION) THEN
          Increment numCustomers.
          Do whatever else you need to do when user clicks "Yes."
       END IF
     
    END LOOP


    Cheers!

    Z
    Ahh.. like this?

    import javax.swing.JOptionPane;
     
    public class CustomerCounter {
     public static void main(String[] args) {
     
     int numCustomers = 0;
     int maxCustomers = 100;
     
      while (numCustomers < maxCustomers){
     
       int answer = JOptionPane.showConfirmDialog(null, "Has a customer entered the store?");
     
    	if (answer == JOptionPane.CANCEL_OPTION){
        System.exit(0);
    	}
    	if (answer == JOptionPane.YES_OPTION){
    	 numCustomers++;
    	}
     
    	System.out.println("There are " + numCustomers + " customers in the store.");
     
    	if (numCustomers == 100){
    	 System.out.println("This customer wins a free book! Yay!");
    	}
     
      }
     }
    }


    Here's a good question.. let's say, instead of System.out.println("This customer wins a free book! etc etc) I'd like a message dialog to come up displaying a picture of a book. How would I do that?


    UPDATE: How do I "break" the loop? Is there a way to keep the program running, so that even if the user clicks on Cancel, the customer count will remain intact? As in, I won't have to run the program again and start the customer count back at 0?

Similar Threads

  1. LOOping for my Customer ....
    By LogicBrix in forum Java Theory & Questions
    Replies: 3
    Last Post: October 11th, 2011, 06:44 PM
  2. Counting Words in a File with a Loop
    By bengregg in forum Loops & Control Statements
    Replies: 17
    Last Post: February 11th, 2011, 10:11 AM
  3. Counting How Many Times Program Was Excecuted
    By Override in forum Loops & Control Statements
    Replies: 2
    Last Post: October 30th, 2010, 05:11 PM
  4. Need help.. Counting Prime #'s up to 50 w/while loop
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 6th, 2010, 05:40 PM
  5. Program help with counting even and odd integers in input value.
    By fueledbyrick in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 6th, 2010, 07:02 PM