Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Online shopping cart  RSS feed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to finish this, but i keep getting a couple of errors. This is the instructions for the assignment.

(1) Extend the ItemToPurchase class per the following specifications:

   Private fields
   string itemDescription - Initialized in default constructor to "none"
   Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)
   Public member methods
   setDescription() mutator & getDescription() accessor (2 pts)
   printItemCost() - Outputs the item name followed by the quantity, price, and subtotal
   printItemDescription() - Outputs the item name and description.

(2) Create two new files:

   ShoppingCart.java - Class definition
   ShoppingCartManager.java - Contains main() method

Build the ShoppingCart class with the following specifications. Note: Some can be method stubs (empty methods) initially, to be completed in later steps.

   Private fields

   String customerName - Initialized in default constructor to "none"

   String currentDate - Initialized in default constructor to "January 1, 2016"

   ArrayList cartItems

   Default constructor

   Parameterized constructor which takes the customer name and date as parameters (1 pt)

   Public member methods

   getCustomerName() accessor (1 pt)

   getDate() accessor (1 pt)

   addItem()
       Adds an item to cartItems array. Has parameter ItemToPurchase. Does not return anything.

   removeItem()
       Removes item from cartItems array. Has a string (an item's name) parameter. Does not return anything.
       If item name cannot be found, output this message: Item not found in cart. Nothing removed.

   modifyItem()
       Modifies an item's description, price, and/or quantity. Has parameter ItemToPurchase. Does not return anything.
       If item can be found (by name) in cart, check if parameter has default values for description, price, and quantity. If not, modify item in cart.
       If item cannot be found (by name) in cart, output this message: Item not found in cart. Nothing modified.

   getNumItemsInCart() (2 pts)
       Returns quantity of all items in cart. Has no parameters.

   getCostOfCart() (2 pts)
       Determines and returns the total cost of items in cart. Has no parameters.

   printTotal()
       Outputs total of objects in cart.
       If cart is empty, output this message: SHOPPING CART IS EMPTY

   printDescriptions()
       Outputs each item's description.


The 1st error I get is this: (Please see the attachments for images)Test that getCostOfCart() returns 9 (ShoppingCart)
Your output
getCostOfCart() incorrectly returned 3


The 2nd error I get seems to be a spacing error. (Images) I don't believe that it is though. I think it maybe something else causing the issue. But I can not figure it out.

Here is my code:







I hope i explained everything well enough. I apologize for any confusion.
 
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any attachments.
Cutting and pasting your session into a post would be preferable (UseCodeTags).
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 24 you are prompting for a choice but you don't actually get the choice until line 46, and in the mean time you may be executing who knows what logic for the last choice. Move line 46.
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't use 'cart' anywhere so you don't need a 'cart' to be passed in. Just remove this parameter.
 
Marshal
Posts: 63843
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

S. Eckstrom wrote:. . . Initialized in default constructor . . .  Default constructor . . . Parameterized constructor  . . . .

That entitles you to refer the question back because whoever set it doesn't know what a default constructor is.
 
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Campbell's point, a default constructor is a constructor that the Java compiler provides by default when a class has not been explicitly declared to have a constructor. That is, if the programmer does not write any constructor for the class, Java will provide one by default.

This has an explicit no-argument constructor:

The constructor here is a no-argument constructor but it's not a default constructor.

This:

will be given a default no-argument constructor by the Java compiler.
 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown.
"On line 24 you are prompting for a choice but you don't actually get the choice until line 46, and in the mean time you may be executing who knows what logic for the last choice. Move line 46."

Thanks for trying to help. I have tried moving line 46, "choice = input.nextLine().toLowerCase().charAt(0);" but I can't seem to get it to work.
 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the responses. I have new code, but I still get errors. How should I post my new code for people to look at? Post it in the same post as this? or start a new post?
 
Junilu Lacar
Sheriff
Posts: 13391
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's the same problem just post here.
 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eclipse only gives me one warning: Resource leak: 'sc' is never closed. I'm not finding the problem to fix this. Here are the errors I get on Zybooks: (This seems to me to be a scanner issue, but I don't know what. I have removed "import java.util.Scanner;" it from the Shopping Cart Class, but it did not help.)
Compilation failed
java:8: error: cannot find symbol if(testItem.getName() == "Bottled Water" && testItem.getDescription() == "Deer Park, 12 oz." &&
                                                                                                                                          ^
symbol: method getName() location: variable testItem of type ItemToPurchase
java:9: error: cannot find symbol testItem.getPrice() == 1 && testItem.getQuantity() == 10)
                                                                                                               ^
symbol: method getPrice() location: variable testItem of type ItemToPurchase
java:9: error: cannot find symbol testItem.getPrice() == 1 && testItem.getQuantity() == 10) {
                                                                                                              ^
symbol: method getQuantity() location: variable testItem of type ItemToPurchase
java:17: error: cannot find symbol System.out.println("getName() returns " + testItem.getName());
                                                                                                                                       ^
symbol: method getName() location: variable testItem of type ItemToPurchase
java:19: error: cannot find symbol System.out.println("getPrice() returns " + testItem.getPrice());
                                                                                                                                      ^
symbol: method getPrice() location: variable testItem of type ItemToPurchase
java:20: error: cannot find symbol System.out.println("getQuantity() returns " + testItem.getQuantity());
                                                                                                                                            ^
symbol: method getQuantity() location: variable testItem of type ItemToPurchase

Shopping Cart Class Sorry, the post is too long to put all code in one comment.  See next comments for rest of code.
 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Item to Purchase ClassSorry, the post is too long to put all code in one comment.  See next comments for rest of code


Shopping Cart Manager ClassSorry, the post is too long to put all code in one comment
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not related to your current issue but you should only ever have ONE INSTANCE of a Scanner created using System.in in your PROJECT. The best way to achieve this is by having (only) one of your classes contain a public constant for this that can be accessed from any class in the project. Example:
Now if you need keyboard input use KEYBOARD as the instance name, as in
Other classes would require something a bit more verbose:
Also, never, never, close KEYBOARD regardless of what Eclipse's warnings tell you.
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you post errors, exceptions, stack traces, etc., enclose them in code tags as well.
 
Campbell Ritchie
Marshal
Posts: 63843
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

S. Eckstrom wrote:Eclipse only gives me one warning: Resource leak: 'sc' is never closed. I'm not finding the problem to fix this.  . . .

Eclipse does in fact give the correct solution to that warning if you find the appropriate dropdown list. Hover your mouse on the yellow triangle and choose the option @SuppressWarnings("resource") from the list. This is the only situation where you are allowed to keep a Scanner or anything like that open for the duration of the program. And one of the efw instances where you may use @SuppressWarnings. Only use it when you are absolutely sure the code is correct.

Please avoid coloured text (=coloured text), which some people find very difficult to read.
 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 Thank you Carey Brown and Campbell Ritchie for the information. I was only using the color to separate the text, to try to make it easier to see, but I do understand your point. I will also include my errors in code tags.

 I did as suggested, an ran the "suppressed warnings" in Eclipse, but it did not help. I can't get Eclipse to run anything farther than "Enter Customer's Name:" it should be able to get the whole thing to run. I don't understand what is missing. I've also tried "sc.close" in last line of main method in Shopping Cart Manager Class, without success.

 I did add, (before I posted the code on this site), at the top of the Shopping Cart Manager Class page, because I was getting a 'Quick-fix Error' message and the program would not run at all. (Compilation).

 After adding the to the Shopping Cart Manager Class, is when I started getting the 'Quick-fix error warning: Resource leak: 'sc' is never closed'.

I don't remember if i posted before, but on the Shopping Cart page, I get a What am I missing with this? Is this the cause of my errors? I did try the @SuppressWarning it suggested, without success. I also tried the "Remove unused Scanner" as someone suggested in my earlier post. This is why I added it to the Shopping Cart Manager Class. It does not help with the overall problem however.

Another question unrelated to this problem is, on the Item to Purchase Class, under the "Default Constructor" comment lines 8-11, do I need the "this." in front? or should I remove it? It doesn't seem to make a difference that I can tell.

Thank you for your time and help.

 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried the "Launch" Shopping Cart Class vs. using "Run" and I get the following Error with red diamonds on the code pages.

If I "Launch" the Shopping Cart Printer(I don't know why it's called printer now, but I am new to using Eclipse too), I get this error,
Do either of these errors help with figuring out what is going on? I have searched for the red diamond symbol on the web for its meaning and appearently it means 'conflict'. So I guess I have some conflicts somewhere.
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a ShoppingCartPrinter class? If so, does it have a main() method?

Why were you attempting to launch ShoppingCart? It has no main() method.

How are you launching these?

Also, at this point I think you need to re-post ShoppingCartManager so that we can get back in sync.
 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for responding.
Do you have a ShoppingCartPrinter class? If so, does it have a main() method? Answer: I do not. I have only the Shopping Cart, Items to Purchase, and Shopping Cart Manager. I don't know why the ShoppingCartPrinter comes up in the launcher.  I tried the launcher just to try it. I don't know diddly about Eclipse. I was instructed by my "teacher" to use Eclipse for Java. I don't know what "launching" is supposed to do.

How are you launching these? Answer: I launched them by selecting the launch icon on the toolbar. Launch is next to the run. I used the drop down to select each one.

Also, at this point I think you need to re-post ShoppingCartManager so that we can get back in sync. Answer: Here is my Shopping Cart Manager. Let me know if you want the other codes again too. I get the "Resource leak: 'sc' is never closed' warnings on lines 5 and 19,

 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 5 and 19 both create an instance of Scanner from System.in. I've already warned you that this is a bug and gave you a solution.

Another way to launch a program in Eclipse is to locate the .java file in the left margin (Package Explorer tab), right-click on it and select "Run As...", and then "Java Application".

This only works with files containing main().

I don't see a menu option named "launch". There is one named "Run". Where are you finding "launch"?
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Not related to your current issue but you should only ever have ONE INSTANCE of a Scanner created using System.in in your PROJECT. The best way to achieve this is by having (only) one of your classes contain a public constant for this that can be accessed from any class in the project. Example:
Now if you need keyboard input use KEYBOARD as the instance name, as in
Other classes would require something a bit more verbose:
Also, never, never, close KEYBOARD regardless of what Eclipse's warnings tell you.

 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 5 and 19 both create an instance of Scanner from System.in. I've already warned you that this is a bug and gave you a solution. Answer: yes, I know I tried that and it didn't get me anywhere as far as running the code in Eclipse or on the ZyBooks lab for grading, I reverted my code back. Shall I put the exception back in?

Another way to launch a program in Eclipse is to locate the .java file in the left margin (Package Explorer tab), right-click on it and select "Run As...", and then "Java Application". Answer: I will try this method too.

This only works with files containing main(). Answer: Roger that.

I don't see a menu option named "launch". There is one named "Run". Where are you finding "launch"? Answer: It is in my quick launch toolbar under the tabs for File, Edit, Source, etc. These tools where already there after I installed Eclipse. I'm calling them the name that appears when I hover the mouse over the tool. Near by is Debug, run, launch, and external tools. If there is a way, I could screenshot it for you.

The Shopping Cart Printer label shows up while the Shopping Cart Class and the Item To Purchase Class tabs are selected and I hover the mouse over the run or launch tool. Shopping Cart Printer label does not show up for while the Manager Class is selected.
 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown, as far as the scanner on the Shopping Cart Class, is there another way I should code for the scanner on line 5 and 19?
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Carey Brown wrote:Not related to your current issue but you should only ever have ONE INSTANCE of a Scanner created using System.in in your PROJECT. The best way to achieve this is by having (only) one of your classes contain a public constant for this that can be accessed from any class in the project. Example:
Now if you need keyboard input use KEYBOARD as the instance name, as in
Other classes would require something a bit more verbose:
Also, never, never, close KEYBOARD regardless of what Eclipse's warnings tell you.


Delete 5 & 19 entirely and replace it with this.
 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Carey Brown. I've done the changes as you suggested, but obviously I suck at this way worse than even I expected. I have to be doing something wrong, because doing what you suggested, Eclipse still want the import scanner. I went ahead and did that, added the changes I thought would be correct, I got no further errors, but the program won't run past "Enter customer name:" and when I run it through ZyBooks, it still gives me errors. I don't know what else to say.
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do need to import Scanner.

Would you please cut and paste the complete error message into a post.
 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much, for using your time with me. I truly do appreciate this! The attachment is from Eclipse, when I run the Shopping Cart Class. When I run the Shopping Cart Manager Class, it stops at Enter Customer's Name: Please let me know if you want to see the images of the output from ZyBooks also. I can screenshot part of it too.
This is the errors from the ZyBooks:
EclipseShoppingCartClassRunError.PNG
[Thumbnail for EclipseShoppingCartClassRunError.PNG]
 
S. Eckstrom
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added the "sc.close();" on lines 16 & 96, which takes care of the warnings in Eclipse (or at least masks them), but still can't get ZyBooks to pass the testItem.get issue.

 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, you are still not doing the Scanner stuff correctly.

1) Get rid of any new Scanner(System.in) lines EXCEPT what I've just asked you to ADD.
2) Get rid of any sc.close() statements. I don't care what eclipse says, it is wrong in this particular instance.
3) Change all 'sc' variable names to 'KEYBOARD'.

I'm using the latest version of Eclipse: 2019-03 with update from two days ago. It no longer warns about not closing KEYBOARD.
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

S. Eckstrom wrote:... when I run the Shopping Cart Class. When I run the Shopping Cart Manager Class, it stops at Enter Customer's Name:
This is the errors from the ZyBooks:


This tells me that you have not implemented the getters in you ItemToPurchase class, or that you've implemented them incorrectly. Please post the code.

The error "main() not found" is because you are trying to run "ShoppingCart" which does not have a main(). You need to run "ShoppingCartManager".

Who told you to use Eclipse? Usually Eclipse is not recommended for beginners because of its learning curve. This is causing you grief here. Get the person who told you to use Eclipse to help you with running programs correctly.
 
Carey Brown
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A general comment:

Although your printMenu() would appear to work:
  • it is too long
  • it does not do what the method name says, it does much more than printing the menu
  • each menu item should be calling a method just for that one choice. Make new methods and move your current code into those.

  • Example:

     
    Carey Brown
    Saloon Keeper
    Posts: 5759
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I just noticed in your ItemToPurchase class you do not have a getName() method you have a getItemName() method. You are attempting to call getName() which does not exist.

    If it were me, I'd rename all your fields to eliminate the word 'item' from in front. E.g., change 'itemName' to just 'name'. Then rename your getters to also remove 'Item' in the name. So change getItemName() to getName().
     
    S. Eckstrom
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I apologize, I forgot I had changed it back last night and was messing with before I went to bed. I forgot about having done so this morning. It's back how you suggested (code included) Attachment is screenshot of ZyBook Errors.  

    Screenshot_2019-03-30-9-22-Ch-7-Program-Online-shopping-cart-(continued)-(Java).png
    [Thumbnail for Screenshot_2019-03-30-9-22-Ch-7-Program-Online-shopping-cart-(continued)-(Java).png]
     
    S. Eckstrom
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you, Than you, Thank you! Got the coding errors cleared up. Now I got to figure out why zybooks don't like my inputs. If you're willing to help, I got several things to clear up, but I think once I start getting a couple things done, most will clear up at the same time. (image)
    Capture.PNG
    [Thumbnail for Capture.PNG]
    SpecialCharacters.PNG
    [Thumbnail for SpecialCharacters.PNG]
     
    Carey Brown
    Saloon Keeper
    Posts: 5759
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It appears that they are expecting a very specific output format which you haven't matched.  This falls under the category of following instructions, you have to read all the fine print.

    Example:

    Your output:
    John Doe

    they expect:
    John Doe's
     
    S. Eckstrom
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for all your help Carey Brown!!!
     
    What are your superhero powers? Go ahead and try them on this tiny ad:
    global solutions you can do in your home or backyard
    https://coderanch.com/t/708587/global-solutions-home-backyard
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!