• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

Varun Shrivastava's Blog

Be My Aficionado

  • home
  • sitemaps
You are here: Home / Programming / Simple Shopping Cart in JAVA using Collections

Simple Shopping Cart in JAVA using Collections

September 9, 2016 by Varun Shrivastava 8 Comments

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,
– collections.shoppingcart
3. For simplicity, all of the code will reside inside this package.

Classes Used

  • Product.java
  • Products.java
  • Cart.java
  • UI.java
  • Main.java

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.

Product.java

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.

Products.java

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.

Cart.java

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.

UI.java

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.

Main.java

 

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.

Conclusion

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 🙂

Sharing is Loving...

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on Telegram (Opens in new window)

Like this:

Like Loading...

Related

Filed Under: Programming Tagged With: cart, collections, java, java programming, kiss, program, shopping cart

Reader Interactions

Comments

  1. Mani says

    June 24, 2021 at 11:01 am

    Informative article

    Reply
    • Varun Shrivastava says

      June 30, 2021 at 12:46 pm

      Thanks 🙂

      Reply
  2. Amit Gautam says

    December 23, 2021 at 7:39 am

    what is pid

    Reply
    • Varun Shrivastava says

      December 23, 2021 at 1:22 pm

      Product ID

      Reply
    • Srinivas says

      July 2, 2023 at 8:36 pm

      ProductId

      Reply
  3. Gauransh says

    March 24, 2022 at 8:51 am

    How to use proper relationship between the database tables

    Reply
  4. KAKUMANI PAVAN KALYAN says

    April 14, 2022 at 9:37 am

    in output, it shows
    we cannot able to add products

    Reply

Leave a ReplyCancel reply

Primary Sidebar

Trending

  • Your Emotional State Affects Your Physical Appearance
  • 7 Most Successful Women In the World
  • Being Selfish Is Good But Not At The Cost Of Someone's Happiness

Featured Posts

“It’s My Choice” – The Most Selfish Lie of Our Generation

Life Update: Moved to NewCastle a Year Ago – Very Late Post

Embracing the Power of Serverless with AWS Lambda

The Paradox Of Control In A Relationship

The Dual Nature of Uncertainty: Excitement and Paralysis

Categories

  • Blogging
    • AI Talking
  • Cooking
  • Fashion
  • My Journal
  • Programming
  • Technology
  • Travelling

RSS feed RSS - Posts

Copyright © 2025 · Magazine Pro on Genesis Framework · WordPress · Log in

%d