Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 37 of 37

Thread: Default Write a Java program to simulate an online shopping cart.

  1. #26
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    How do I change it to use one of the existing CartItem constructors?

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Look at the code for the CartItem class and find its constructors. Then write the new statement to use one of them.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Here is the code for the CartItem class. In the image, you can see the constructors. I have tried using them but it is not working. I think i am writing the statement wrong. How would I write a new statement to use one of them?

    5.PNG

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    I have tried using them but it is not working.
    Please copy and post the code where you tried and the error messages you get.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    product cannot be resolved to a variable
    The constructor CartItem(String) is undefined

    at Shopping.main(Shopping.java:59)


    5.PNG

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Copy the constructor definition
    and the new statement you are trying to use and paste them one above the other. That will allow you to compare the required arguments with the ones you are using and see what is wrong. For example:
      public SomeClass(int anInt) { //  constructor definition statement
     
      = new SomeClass("aString");  // invalid call - arg should be int not String
     
      = new SomeClass(123); // valid  - arg is int
    If you don't understand my answer, don't ignore it, ask a question.

  7. #32
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    So all three lines of that code should go in the highlighted section:

    5.PNG

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    all three lines of that code should go in
    No. Those lines of code where an example, not part of your code.

    Copy all the constructor definition statements
    and the new statement you are trying to use
    and paste them here in the thread, one above the other.

    Please copy and paste the lines here, no images.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #34
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Here is what I did for case 1 and this seemed to work:
    item = new CartItem(product, quantity, price);

    However for case 2, it gives me an error saying:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The constructor CartItem(String) is undefined

    at Shopping.main(Shopping.java:71)

  10. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Change case 2 so its new statement uses the same type of arguments as for the new statement in case 1.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #36
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Ok I did that but here is what happens when I run the program and choose option 1:


    Menu - Managing a List
    1 Add an item to your cart
    2 Remove an item from your cart
    3 View the items in your cart
    4 Exit and add up the total
    5 Empty your cart
    6 Exit
    Select a menu option
    1
    Enter an item:
    Exception in thread "main" java.lang.NullPointerException
    at java.lang.String.<init>(Unknown Source)
    at CartItem.<init>(CartItem.java:16)
    at Shopping.main(Shopping.java:64)

  12. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Exception in thread "main" java.lang.NullPointerException
    at java.lang.String.<init>(Unknown Source)
    at CartItem.<init>(CartItem.java:16)
    There is a variable with a null value on line 16. Look at line 16 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 16 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Write a Java program to simulate an online shopping cart.
    By astjeanos in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 11th, 2013, 04:32 PM
  2. simple shopping cart
    By dodda911 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2013, 08:25 AM
  3. Shopping cart application
    By ashkrish in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2012, 01:40 PM
  4. Shopping cart application
    By ashkrish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 28th, 2012, 12:08 PM
  5. Replies: 1
    Last Post: February 8th, 2012, 05:16 PM