whats wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

whats wrong?

how can i fix the seeCart() method? my goal is to add every item that i call the method for to the seeCart() method and then make another method called confirmPurchase() that tells me the cost.. but the problem is that i dont know how to use variables from one methos in another... can you help with that? code: https://code.sololearn.com/ct54U7KbBYX9

15th Aug 2020, 5:40 PM
Yahel
Yahel - avatar
12 Answers
+ 2
For some reason the viewCart function had a command to add a new item to cart. And the addItem to Cart in the viewCart function Is being passed a variable that is not available. Because the variable myItem is only available in the function AddToCart(). Not public.
15th Aug 2020, 6:05 PM
Steve Sajeev
Steve Sajeev - avatar
+ 2
Sorry for the delay I was offline for few days. Real the problem with the code is only that the myItem variable is not defined in the seeCart function.
17th Aug 2020, 4:08 PM
Steve Sajeev
Steve Sajeev - avatar
+ 1
I am not a java developer. But I understand it. I found out that myItem variable is not instantiated publicly or inside the show art function, thus causing an error "saying no variable found" HOPE IT HELPS https://code.sololearn.com/cRev1WGOvudY/?ref=app
15th Aug 2020, 5:57 PM
Steve Sajeev
Steve Sajeev - avatar
+ 1
1st thing is not everything should be static. inside shop class you can remove every static keyword next if you want to store a value than can be used later, declare it as instance variable like S-Coder did with myCurrentItem if you want multiple item to be stored try to use array instead of String, or anoter Map if you want to also store how many of each items added then in seeCart method you can loop through that array or map then print the result btw its also important to have more meaningful variable name. https://code.sololearn.com/ceTrz4AXkM5L/?ref=app
15th Aug 2020, 6:01 PM
Taste
Taste - avatar
0
import java.util.*; public class Main { public static void main(String[] args) { Shop shop = new Shop(); shop.addToCart("Shoes"); shop.addToCart("Hat"); shop.seeCart(); } } class Shop{ static HashMap <String , Integer> hm = new HashMap<>(); static String myCurrentItem; static{ hm.put ("Shoes" , 300); hm.put ("Hat" , 25); hm.put ("Shirt" , 90); } public static void addToCart(String myItem){ if (hm.containsKey(myItem)){ String Item = myItem + " , Price: " + hm.get(myItem); System.out.println(Item); myItem = myCurrentItem; }else{ System.out.println("Item Not Found"); } } public void seeCart(){ HashMap <Object , Object> hm2 = new HashMap<>(); hm2.put (myCurrentItem , hm.get(myCurrentItem )); System.out.println(hm2); } } Error fixed, but maybe incorrect.
15th Aug 2020, 5:50 PM
S-Coder
S-Coder - avatar
0
Steve Sajeev Yes, he should mark your answer as best.
15th Aug 2020, 5:59 PM
S-Coder
S-Coder - avatar
0
S-Coder , the output isnt right.. take a look...
15th Aug 2020, 6:00 PM
Yahel
Yahel - avatar
0
Steve , what do you mean? can you show me the right code?
15th Aug 2020, 6:02 PM
Yahel
Yahel - avatar
0
Taste , how? can you show me? i dont understand... how would i do that with array?
15th Aug 2020, 6:07 PM
Yahel
Yahel - avatar
0
yahel what actually seeCart() does? Is it shows the added items from cart? If yes, this will show you.. Just print hash map.. public void seeCart(){ /*HashMap <Object , Object> hm2 = new HashMap<>(); //hm2.put (myItem , hm.get(myItem)); */ for(HashMap.Entry<String, String> pair: hm.entrySet()) System.out.println(pair.getKey() + " : "+pair.getValue()); }
15th Aug 2020, 6:16 PM
Jayakrishna 🇮🇳
17th Aug 2020, 3:16 PM
Rizwan Ayyanabban
Rizwan Ayyanabban - avatar
- 1
Jayakrishna🇮🇳 , first of all you need to change <String , String> to <String , Integer> and then its printing the whole items that are in the shop, insted of printing what i added to the cart. how can i make it work?
17th Aug 2020, 11:29 AM
Yahel
Yahel - avatar