Results 1 to 16 of 16
Thread: Java Cupcake Counter Help
- 02-16-2013, 08:22 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 13
- Rep Power
- 0
Java Cupcake Counter Help
Hello everyone,
I am in need of help in creating loop for the following scenario. I am new to java and have I am currently stuck on this one.
1 - one individual cupcake sold (for $2)
3 - a "package of 3" sold (for $5)
1 - one individual cupcake sold (for $2)
6 - a "package of 6" sold (for $8)
Build a program that will accept the input of each sale. Use a loop that will keep
accepting input until a sentinel value of 0 (zero) is entered. Ignore any input that is not 1 or 3 or 6; use
an if or select to accomplish this. While in the loop, you will accumulate the following three values:
* Total amount of dollar sales. For the input (what package size was sold), use an if or select to
determine the appropriate dollar sales to add. The prices are above; for example, if "3" is
entered, add $5 to the Total amount of dollar sales.
* Total number of cupcakes sold. Add the appropriate number; for example, if "6" is entered, add
6 to the Total number of cupcakes sold.
* Number of multi-cupcake packages sold. If the input package size sold is larger than 1, add one
to this counter. (This will accumulate "we sold 2 multi-cupcake packages", not count how many
cupcakes were in those packages.) Use an if or select to accomplish this.
Here is what I currently have
Java Code:public static void main(String[] args) { //Initialize Variables int totalDollarSales = 0; int numberCupcakesSold = 0; int countMultiCupcakePackages = 0; int oneCupcake = 3; int threeCupcake = 5; int sixCupcake = 8; //Create a Scanner for Input Scanner input = new Scanner(System.in); //Loop cup cake counter //Read an initial input System.out.println("Enter package size sold: 1, 3, or 6: "); int data = input.nextInt(); //Keep reading data until the input is 0 int sum = 0; while (data !=0) { sum += data; //Read the next input System.out.println("Enter package size sold - 1, 3, or 6: "); data = input.nextInt(); }
- 02-16-2013, 08:33 PM #2
Godlike
- Join Date
- Nov 2012
- Posts
- 195
- Rep Power
- 1
Re: Java Cupcake Counter Help
We see the assignment and your code, kudos for that. The only thing that is missing is your question. Does it compile? Does it run? Does it give the output expected? If not, please post the output or stacktrace and we can help. Now we're just in the dark about your problem.
- 02-16-2013, 08:36 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 13
- Rep Power
- 0
Re: Java Cupcake Counter Help
I am trying to figure out, What is the total Dollar Sales, Number of cupcakes sold, and Multi Packages sold. I would like those numbers given to me in the output when I answer the question ''Enter package size sold: 1, 3, or 6:"
Thanks
- 02-16-2013, 08:43 PM #4
Godlike
- Join Date
- Nov 2012
- Posts
- 195
- Rep Power
- 1
Re: Java Cupcake Counter Help
In your loop from line 24 to 30, you should do something with variable data. So, for example in plain English, aka pseudocode:
All you need to do now is translate this into Java.Java Code:if data is 3 then add 3 to numberCupcakesSold, add 5 to totalDollarSales and add 1 to countMultiCupcakePackages.
- 02-16-2013, 08:47 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 13
- Rep Power
- 0
Re: Java Cupcake Counter Help
That is my problem SurfMan, I am very new to Java. I get stuck on the part where I have to translate to Java.
- 02-16-2013, 08:53 PM #6
Godlike
- Join Date
- Nov 2012
- Posts
- 195
- Rep Power
- 1
Re: Java Cupcake Counter Help
Here's a helpful link: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics). After reading this you will understand. The answer is already there. To help just a little bit more, without spoonfeeding you the answer, I'll add linebreaks. Remember, this still pseudocode, but it should give you a peek of what the if should look like:
Java Code:if data is 3 { add 3 to numberCupcakesSold add 5 to totalDollarSales add 1 to countMultiCupcakePackages. }
- 02-16-2013, 09:21 PM #7
Member
- Join Date
- Feb 2013
- Posts
- 13
- Rep Power
- 0
Re: Java Cupcake Counter Help
Didnt really come out what i hoped for..
Java Code:public static void main(String[] args) { //Initialize Variables int totalDollarSales = 0; int numberCupcakesSold = 0; int countMultiCupcakePackages = 0; int oneCupcake = 3; int threeCupcake = 5; int sixCupcake = 8; //Create a Scanner for Input Scanner input = new Scanner(System.in); //Loop cup cake counter if(numberCupcakesSold == 1) { totalDollarSales = 3; countMultiCupcakePackages = 0; } else if(numberCupcakesSold == 3) { totalDollarSales = 5; countMultiCupcakePackages = 1; } else if(numberCupcakesSold == 6) { totalDollarSales = 8; countMultiCupcakePackages = 1; } //Read an initial input System.out.println("Enter package size sold: 1, 3, or 6: "); int data = input.nextInt(); //Keep reading data until the input is 0 int sum = 0; while (data !=0) { sum += data; //Read the next input System.out.println("Enter package size sold - 1, 3, or 6: "); data = input.nextInt(); } System.out.println("Total Dollar Sales is " + totalDollarSales); System.out.println("Total Cupcakes Sold is " + sum); System.out.println("Multi Cupcake Packages is " + countMultiCupcakePackages); } }
I still get 0 in both Sales and Package output
- 02-16-2013, 09:38 PM #8
Godlike
- Join Date
- Nov 2012
- Posts
- 195
- Rep Power
- 1
Re: Java Cupcake Counter Help
You're very close. First of all, the if and else ifs should be inside the loop, because the user input has to be checked every time. Second, you should check variable data not numberCupcakesSold, since data is what the user's input was. Variable numberCupcakesSold hold the grand total. So here's what it should look like:
Now, all that remains is what to do with variable totalDollarSales and the other two. Remember that the value you want to assign to totalDollarSales is the *old* value of totalDollarSales *plus* what you just earned. So setting totalDollarSales = 3 would not work since it then *always* will be 3. Add the old value as well to sum it up.Java Code:System.out.println("Enter package size sold: 1, 3, or 6: "); int data = input.nextInt(); //Keep reading data until the input is 0 while (data !=0) { if ( data == 1 ) { totalDollarSales = ?; numberCupcakesSold = ?; countMultiCupcakePackages = ?; } else if ( data == 3 ) { totalDollarSales = ?; numberCupcakesSold = ?; countMultiCupcakePackages = ?; } etc... //Read the next input System.out.println("Enter package size sold - 1, 3, or 6: "); data = input.nextInt(); }
- 02-16-2013, 10:21 PM #9
Member
- Join Date
- Feb 2013
- Posts
- 13
- Rep Power
- 0
Re: Java Cupcake Counter Help
Thank you so much for that first part. For the second part, do you mean
Or something like that?Java Code:totalDollarSales=totalDollarSales + data
- 02-16-2013, 10:39 PM #10
Godlike
- Join Date
- Nov 2012
- Posts
- 195
- Rep Power
- 1
Re: Java Cupcake Counter Help
You are so close to nailing this! But you are adding the wrong value to the sales.
Think about it... What do you add to the total dollar sales variable? The price of whatever the customer bought! So if they bought one cupcake, the price is 3 dollars, so you should add 3. You actually already have three variables that contain these values:
int oneCupcake = 3;
int threeCupcake = 5;
int sixCupcake = 8;
Same goes for variable numberCupcakesSold: how much do you add when they buy 3 cupcakes? Variable countMultiCupcakePackages: when they buy a single cupcake, do I add 1? When they buy 6, do I add one?
- 02-16-2013, 10:52 PM #11
Member
- Join Date
- Feb 2013
- Posts
- 13
- Rep Power
- 0
Re: Java Cupcake Counter Help
Would I have to create another loop for this part? Or where would this data go?
- 02-16-2013, 11:10 PM #12
Godlike
- Join Date
- Nov 2012
- Posts
- 195
- Rep Power
- 1
Re: Java Cupcake Counter Help
You don't need another loop. This check is done when the user has given input. So this goes inside the existing loop, because you want to recalculate the totals every time th user choses some cupcake amount. And think about the numbers you add to each variable. Let me clarify this with some pseudocode:
You see what I did there?Java Code:get user input while the user choses a number if the user chose 1 then add 1 to number cupcakes sold add 3 to total dollars sales add nothing to countMultiCupcakePackages (since this is only one cupcake) else if the user chose 3 then add 3 to number cupcakes sold add 5 to total dollars sales add 1 to countMultiCupcakePackages else if the user chose 6 then add 6 to number cupcakes sold add 8 to total dollars sales add 1 to countMultiCupcakePackages end if get user input end while
- 02-16-2013, 11:21 PM #13
Member
- Join Date
- Feb 2013
- Posts
- 13
- Rep Power
- 0
Re: Java Cupcake Counter Help
I am not sure what you mean. I have this currently.
Java Code://Keep reading data until the input is 0 while (data !=0){ if ( data == 1 ) { totalDollarSales = 3; numberCupcakesSold = 1; countMultiCupcakePackages = 0; } else if ( data == 3 ) { totalDollarSales = 5; numberCupcakesSold = 3; countMultiCupcakePackages = 1; } else if ( data == 6 ) { totalDollarSales = 8; numberCupcakesSold = 6; countMultiCupcakePackages = 1; } //Get user input while() //Read the next input System.out.println("Enter package size sold - 1, 3, or 6: "); data = input.nextInt();
Do I add the commands under the "//Get user input'' ?
- 02-17-2013, 12:08 AM #14
Member
- Join Date
- Feb 2013
- Posts
- 13
- Rep Power
- 0
Re: Java Cupcake Counter Help
Thank you for the help, I finally figured it out!
- 02-17-2013, 12:13 AM #15
Godlike
- Join Date
- Nov 2012
- Posts
- 195
- Rep Power
- 1
- 02-17-2013, 11:02 PM #16
Godlike
- Join Date
- Nov 2012
- Posts
- 195
- Rep Power
- 1
Similar Threads
-
Help with constructor counter.
By lannie1980 in forum New To JavaReplies: 3Last Post: 05-05-2012, 10:33 PM -
Score Counter In Java GUI.
By UnAccomplishedJavaPerson in forum New To JavaReplies: 9Last Post: 11-18-2011, 01:47 PM -
Counter problem
By Razpet22 in forum NetBeansReplies: 5Last Post: 06-14-2011, 08:27 PM -
Writing a counter
By Pojahn_M in forum New To JavaReplies: 4Last Post: 05-03-2011, 03:56 PM -
Counter
By ks1615 in forum New To JavaReplies: 6Last Post: 02-20-2009, 03:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks