I have to make a program that simulates a shopping cart, I have it done, it's just that the shopping cart wont print. Please help me in fixing this, this is due for me tomorrow and is worth 40% of this chapter grade.
The files are attached below. The main one is ShoppingSim.java

Recommended Answers

All 13 Replies

You didn't ask a question or explain what was wrong with the program. Surely if you've worked on it you know an area where your code doesn't work.

just post the code you want us to review here, not in attachment.
as far as I understand from your description, you've got an array of some sort, containing objects of a class (for instance, Product)

to print those: iterate over the elements of the array, and print each element. make sure you have a toString() method in your Product class, though

From a quick look at your code you have this:

case 2:
                mart.print();
                // Call break to avoid falling through to the default case.
                break;

It is suppose to print the shopping cart. Are you having problems with that?

From a quick look at your code you have this:

case 2:
                mart.print();
                // Call break to avoid falling through to the default case.
                break;

It is suppose to print the shopping cart. Are you having problems with that?

Yes, that is exactly the problem, thankfully our teacher gave us one more day. Can you explain to me why it wont print the cart? I'm confused.

Oh, I think it's printing the cart just fine.

The problem is that your menu choice handler creates a new cart every time it's called.

How do I fix that? Then where do I put the cart creator?

I only see one logical place for it, given the structure of your ShoppingSim driver program. Where do you think it should go? Where would it make sense to create a new cart?

I only see one logical place for it, given the structure of your ShoppingSim driver program. Where do you think it should go? Where would it make sense to create a new cart?

YES!!! I put the new operator outside of the main method, and it worked!!! I'll get a 100 now! Thank you so much for identifying the prob
But one more problem, this one worth 60/300 points. I have to do the SAME thing, but with Arraylists.

You shouldn't have any problems doing that. The API for ArrayList is pretty straightforward. You don't have to manage the size of it yourself and you have methods to add(), get(), and remove() items. If you run into difficulties, post the code here along with your questions or error messages.

So I just change the names around using the ArrayList class?

No, you replace your array with an instance of an ArrayList and use it's methods to manage the items.

No, you replace your array with an instance of an ArrayList and use it's methods to manage the items.

I have a prob with original, whenever item quantity is more than 1, it mulitiplys the whole thing by that num

No, you replace your array with an instance of an ArrayList and use it's methods to manage the items.

Ok, that wasn;t the prob, the prob is that whenever I use option 2 it doubles the total. I have no idea why

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.