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

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

}

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.

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

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:

Code:

`if data is 3 then add 3 to numberCupcakesSold, add 5 to totalDollarSales and add 1 to countMultiCupcakePackages.`

All you need to do now is translate this into Java.

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.

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:

Code:

`if data is 3 {`

add 3 to numberCupcakesSold

add 5 to totalDollarSales

add 1 to countMultiCupcakePackages.

}

Re: Java Cupcake Counter Help

:(sweat): Didnt really come out what i hoped for..

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

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:

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

}

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.

Re: Java Cupcake Counter Help

Thank you so much for that first part. For the second part, do you mean

Code:

`totalDollarSales=totalDollarSales + data`

Or something like that?

Re: Java Cupcake Counter Help

Quote:

Originally Posted by

**Hawks** Thank you so much for that first part. For the second part, do you mean

Code:

`totalDollarSales=totalDollarSales + data`

Or something like that?

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?

Re: Java Cupcake Counter Help

Would I have to create another loop for this part? Or where would this data go?

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:

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

You see what I did there?

Re: Java Cupcake Counter Help

I am not sure what you mean. I have this currently.

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'' ?

Re: Java Cupcake Counter Help

Thank you for the help, I finally figured it out!

Re: Java Cupcake Counter Help

Quote:

Originally Posted by

**Hawks** Thank you for the help, I finally figured it out!

No problem, I hope it was useful.

Re: Java Cupcake Counter Help

Without showing your code or the errors we are blind. Show us what you got so far, including any error messages, and you will get help.