Simple Shopping Cart in JAVA using Collections

If you have read my previous article which solely focused on getting you started with the JAVA Collections framework then it’s time to dig a little deeper and create an application to demonstrate the use of Collections in real world application. I have chosen Shopping Cart application for this purpose because I found it perfect for the situation and the use of collection in this application will help you better in modelling real world applications.

Project Structure

1. Firstly create a new Java Project with whatever name you like.
2. Create the following package under your src folder,
3. For simplicity, all of the code will reside inside this package.

Classes Used


For this example project, make sure all of the above-mentioned classes resides under the same package, i.e, collections.shoppingcart. I will take each class one by one and explain the significance of each class.

Go through the code below,

This is a concrete class which contains Product properties and provides setters and getters for it. I have also override the Hash and Equals method in this class which you will realise later as we move forward.

Go through the code below,

The role of this class in your shopping cart application is just to provide you with the store products. In other words, this class is used to initialize your store as soon as the application is started. As you can see, the initStoreItems() methods is used to add the products into a new ArrayList which is the type Product. If you have read my previous article on Understanding Collections in JAVA then you will be familiar with the Generic Collection. As we have provided the Product class inside the <> braces as new ArrayList(), this will tell the compiler that the list is of type Product and it can only contain an item which belongs to type Product.

Go through the code below,

This is a concrete class which act as a cart to store the items temporarily into the cart. It provides the user with all the required operations that a cart should have such as addToCart(), removeFromCart() etc… It also maintains a cartItems list with a type Product, similar to the products list in products class. Its sole purpose is to store the purchased item into the cart, remove the item from the cart. It simply maintains the list of items which are added to the cart by the user.

Go through the code below,

This is the class where the main interaction between the user and application takes place. It is the control point of the application. Everything that you see in the console is a result of this page. It takes the user input and calls the respective methods from the classes that we made earlier to perform the required task. Simply go through the code and if you find a problem anywhere just comment below and I will be at your help. You may also comment below the code in GitHub, that way it will be more organized.


This is simply the starting point for the application. It calls the constructor of UI() from the main method which takes care of bootstrapping the entire application.


Your Shopping Cart application is fully working at this point of time. I hope the use of the collection is very clear. If there is any doubt or problem then you may contact me directly by commenting below. For simplicity, I have only used ArrayList, however, you may use any other data structure as per the need of the application. I suggest you follow the KISS principle every time you are given a project.

And as always,

Be My Aficionado 🙂

1 thought on “Simple Shopping Cart in JAVA using Collections”

  1. Pingback: A Beginners Guide to Understand Collections in JAVA -

Leave a Reply

%d bloggers like this: