Learn Intermediate Java | Codecademy
I need help with Intermediate Java project Ice Cream Inventory. I have step 1 done and the begining of 2. I need help in general with it all.
Learn Intermediate Java | Codecademy
I need help with Intermediate Java project Ice Cream Inventory. I have step 1 done and the begining of 2. I need help in general with it all.
Hi, there!
What exactly are you having trouble with? Perhaps I can help explain.
Hi, pretty much everything in that project I need help with.
Well, the first thing I would suggest if youâre having trouble with the concepts in the entire project, would be to go back and review previous lessons on what is being asked to be done.
But letâs take a look at this together.
You said you have step one done, and the beginning of step 2. So, as of right now your code should look something like this:
class ShopItems {
int totalCount;
class NonVegan {
int iceCreamCount;
int shakeCount;
int totalCount;
void increaseCount(int type, int count) {
if (type == 1) {
this.iceCreamCount += count;
} else if (type == 2) {
this.shakeCount += count;
}
}
}
}
The second part of step two is asking you to increase the totalCount variable of NonVegan and ShopItems
Essentially, increasing totalCount by the parameter count
The hint below step 2 should be able to guide you the correct direction.
Thank you, for step 3, do I need to create âShopping Itemsâ?
3.
Inside ShoppingItems
, create a class named Vegan
that contains the following fields:
Well, if you look at the instructions for steps 3 and 4, youâll recognize that they are exactly the same as steps 1 and 2. (Other than making the class name and parameters different).
Is a typo and is supposed to be ShopItems as in the first step.
There is another typo in the instructions here:
Where it is supposed to be inside the main of IceCreamShop
Ok, thank you, I was stressing myself out because of the typo. I got through step 4 and 5. Now Iâm on step 6.
Complete the if
-statement within the while
loop in the main()
method. Call the proper increaseCount()
method within each if
or else if
block based on the type of order and whether it is vegan or non-vegan. Use the following table as a guide:
type number | kind of treat | class |
---|---|---|
1 | ice cream | non-vegan |
2 | shake | non-vegan |
3 | smoothie | vegan |
4 | slushie | vegan |
Hint
Here is an example of how you may call the function increaseCount()
for shakes:
nonVegan.increaseCount(2,itemCount);
So for this step, what it is asking you to do inside the ifâŚelse statement is to call the increaseCount() method with the appropriate class just as the hint is telling you to do so.
For instance, if you were to do what was explained in the example, it would look like this:
You would only need to do the same for the three other options.
So would I put the new 'else ifâs under the âifâ statements I created earlier or create new âifâ statements? or something like this?
Wait, no. You put that inside the already created block above.
OH, i didnât scroll down far enough.
I will not be available for the next hour or so, could you post the entirety of your code? If you have not figured it out by the time I get back I will take a look at it for you.
Thank you! I really appreciate your help!
import java.util.Scanner;
class ShopItems {
int totalCount;
class NonVegan {
int iceCreamCount;
int shakeCount;
int totalCount;
ShopItems items = new ShopItems();
ShopItems.NonVegan vegan = items.new NonVegan();
ShopItems.Vegan nonVegan = items.new Vegan();
void increaseCount(int type, int count) {
if (type == 1) {
this.iceCreamCount += count;
} else if (type == 2) {
this.shakeCount += count;
}
}
}
class Vegan {
int smoothieCount;
int slushieCount;
int totalCount;
void increaseCount(int type, int count) {
if (type == 3) {
this.smoothieCount += count;
} else if (type == 4) {
this.slushieCount += count;
}
}
}
public class IceCreamShop {
public static void main(String args) {
int orderType = 0;
Scanner input = new Scanner(System.in);
int itemCount = 0;
String itemName;
System.out.println("Hello! Welcome to the ice cream shop. We are ready to take your order. For ice-cream type 1, for shakes type 2, for smoothies type 3, and for slushies type 4. If you are done ordering type 0.");
orderType = input.nextInt();
while(orderType != 0) {
System.out.println("How many items of this item would you like to order?");
itemCount = input.nextInt();
if(orderType == 1){
itemName = "ice cream";
nonVegan.increaseCount(1,itemcount);
} else if(orderType == 2) {
itemName = "shake";
nonVegan.increaseCount(2,itemcount);
} else if(orderType == 3) {
itemName = "smoothie";
vegan.increaseCount(3,itemcount);
} else if(orderType == 4) {
itemName = "slushie";
vegan.increaseCount(4,itemcount);
} else {
break;
}
System.out.println("Adding " + itemCount + " of the item "+ itemName + " to your order!");
System.out.println("Type a number for the next item in your order:\n1: Ice cream\n2: Shake\n3: Smoothie\n4: Slushie\n");
orderType = input.nextInt();
System.out.println(" Thank you for your order! Today we have sold " + items.totalCount + " orders of sweetness!");
System.out.println(nonVegan.totalCount + " items have been ice cream and shakes.");
System.out.println(vegan.totalCount + " items have been smoothies and slushies.");
}
}
}
So, there a few problems I see.
These class declarations you want inside the IceCreamShop main() method.
And if you look at these parameters, itemcount is not a valid variable. Looking at your code, do you see why?
I moved the
âShopItems items = new ShopItems();
ShopItems.NonVegan vegan = items.new NonVegan();
ShopItems.Vegan nonVegan = items.new Vegan();â
into IceCreamShop Main().
and for the (1,Itemcount); I did not capitalize itemCount.
The last thing I will mention (since your code should be working now) is that you may want to move the last three System prints outside of the while loop, otherwise those will print every single time an item is entered rather than after the final count.
while(orderType != 0) {
System.out.println("How many items of this item would you like to order?");
itemCount = input.nextInt();
if(orderType == 1){
itemName = "ice cream";
nonVegan.increaseCount(1,itemcount);
} else if(orderType == 2) {
itemName = "shake";
nonVegan.increaseCount(2,itemcount);
} else if(orderType == 3) {
itemName = "smoothie";
vegan.increaseCount(3,itemcount);
} else if(orderType == 4) {
itemName = "slushie";
vegan.increaseCount(4,itemcount);
} else {
break;
}
System.out.println("Adding " + itemCount + " of the item "+ itemName + " to your order!");
System.out.println("Type a number for the next item in your order:\n1: Ice cream\n2: Shake\n3: Smoothie\n4: Slushie\n");
orderType = input.nextInt();
}
System.out.println(" Thank you for your order! Today we have sold " + items.totalCount + " orders of sweetness!");
System.out.println(nonVegan.totalCount + " items have been ice cream and shakes.");
System.out.println(vegan.totalCount + " items have been smoothies and slushies.");
}
YAY it works! Thank you for your help! Another question I have is, what alternative learning source would you recommend to help me understand java?
Well, I donât feel too comfortable advertising competitor courses and such on Codecademy.
What I would recommend is just Googling âlearn Javaâ and see what resources come up. Itâs really easy to look at reviews to see what other people are saying. You could also look at top creatorâs on YouTube who offer tutorials on topics you want to learn.