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
Before adding any more code, fix the current code so it compiles and executes without any errors.
When it works, then move on to adding a loop.
Re: Java Cupcake Counter Help
The way you have it written in that snippet will not compile without errors, although the fix is very minor and may have just been from not copying and pasting all of your code. There is also an error in that your code doesn't accurately model the situation you were given. Take a look at the value for int oneCupcake. Once these fixes are done, the rest of your code has to go in your loop with just a few if-else statements and updating the 3 field variables to be changed. Optionally, to make the function of the field variables a bit clearer, think which ones will never have their value changed and make them a constant using the keyword, "final" with the appropriate name changes.
Re: Java Cupcake Counter Help
I currently have this
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);
//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
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;
}
//Read the next input
System.out.println("Enter package size sold - 1, 3, or 6: ");
data = input.nextInt();
//Variables
}
System.out.println("Total Dollar Sales is " + );
System.out.println("Total Cupcakes Sold is " + );
System.out.println("Multi Cupcake Packages is " + );
}
}
Re: Java Cupcake Counter Help
What you have posted won't compile. Its missing a class statement and the import statements.
Re: Java Cupcake Counter Help
I know it doesn't work. I am new to java... thats the reason I posted here for tips and help.
Thanks
Re: Java Cupcake Counter Help
Quote:
I know it doesn't work
please copy and paste here the full text of the compiler's error messages.
Re: Java Cupcake Counter Help
Norm I am getting the message:
"Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at cupcakecounter.java.CupCakeCounterJava.main(CupCak eCounterJava.java:72)
Java Result: 1"
It is because I have not entered variables for the:
System.out.println("Total Dollar Sales is " + );
System.out.println("Multi Cupcake Packages is " + );
That said here is my current code.
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);
//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){
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
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 " + );
System.out.println("Total Cupcakes Sold is " + sum);
System.out.println("Multi Cupcake Packages is " + );
}
}
I believe that "System.out.println("Total Cupcakes Sold is " + sum);" is correct and will give me the correct outcome. I just need help on the other two.
Thanks
Re: Java Cupcake Counter Help
Can you post the errors from the compiler. What you posted looks like it is from trying to execute the code.
You need to find and fix the compiler errors before trying to execute the program.
The posted code is missing the import statement(s)
and the class statement. What is posted will not compile.
Re: Java Cupcake Counter Help
72 and 74 state "illegal state of expression" if that is what you are asking.
My problem is I do not know how to express the correct variables together for example to find out the total Sales...
Re: Java Cupcake Counter Help
You need to find out how to get a complete list of the compiler's error messages so you can post them.
A one line message doesn't help much.
Here is a sample of an error message from the javac compiler program:
Code :
TestSorts.java:138: cannot find symbol
symbol : variable var
location: class TestSorts
var = 2;
^
Did you see this? This is one problem:
The posted code is missing the import statement(s)
and the class statement. What is posted will not compile.
Re: Java Cupcake Counter Help
Thank you for the help, I figured it out finally.
Re: Java Cupcake Counter Help
Quote:
Originally Posted by
Hawks
Thank you for the help, I figured it out finally.
Can you post your final code? I need help with this problem as well and I would like to compare.