• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

Simple Shopping Cart Project in JAVA using Collections

September 9, 2016 by varunshrivastava 13 Comments

If you have read my previous article which solely focuses on getting you started with the JAVA Collections framework then it’s time to dig a little deeper and create a Shopping Cart application to demonstrate the use of Collections in a real-world application.

I have chosen Shopping Cart application for this purpose because I found it perfect for implementing collections in this application.

Shopping Cart application will help you in modelling real-world entities into code.

Table of Contents

  • Project Structure
  • Classes Used
    • Product.java
    • Products.java
    • Cart.java
    • UI.java
    • Main.java
  • Conclusion

Project Structure

  1. 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 it.

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.

  • equals()method is used to compare two objects based on their properties.
  • hashCode()is a unique hash/number attached to every object whenever the object is created.

So whenever two objects are compared. Their hash code and properties are compared. If both (hash code & properties value) are same then the object is considered equal otherwise not equal.

Therefore, it is very important to override hascode() and equals() method of an object.

Following is a very well explained answer that tells you the importance of overriding hashcode and equals for object comparison.

  • Why do I need to Override equals and hashCode method in Java?

Products.java

Go through the code below,

The role of this class in your Shopping Cart Application is 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. In real-world application, the items will be retrieved from user session or database. For simplicity, I’m using Products class to initialize objects.

As you can see, theinitStoreItems()method is used to add the products into a new ArrayList of 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.

Cool.

You are half way there.

Cart.java

This is a concrete class which act as a virtual cart to store the items temporarily.

It provides all the required operations a cart should have such as:

  • addToCart()
  • removeFromCart()
  • getProduct(), etc…

It also maintains a cartItems list with a type Product, similar to the products list in products class.

The sole purpose of cartItemslist is to store the purchased item in the cart or remove the item from the cart. It simply maintains the list of items which are added to the cart by the user.

UI.java

In the web application, you will have a nice looking HTML page with Javascript support. So the user will simply look for the product and click on the Add To Cart button.

In the console application (like this one), UI parts become a bit trickier. But, I’ve got a really cool way to make it easy.

Look:

In the console, a user can input only one input at a time. This is the key concept and could be used to make the UI easy.

People often take different input variables for different inputs. That could become a headache if there are tons of input to process.

That is why the simplest approach is to go with a single input variable and process it as soon as it gets populated.

In this application, I have used a single class variable to take user input. And based on the input value respective process will be executed.

Go through the code below and understand the concept.

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 you made earlier to perform the required task.

Take a look at the code and come back if you have any question. I’m always here to answer.

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 in time.

I hope the use of the collection is 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.

Let me know if you need to learn any other application from real world. I would be more than happy to do it.

Related

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

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

Affordable Hosting (4) algorithms (4) amazon (3) aoc-2020 (7) believe in yourself (4) best (4) database (4) earn money blogging (5) education (4) elementary sorting algorithms (4) experience (3) fashion (4) finance (6) Financial Freedom (7) food (7) friends (3) goals (5) google (5) india (10) indian cuisine (5) indian education system (4) java (16) life (16) life changing (4) love (4) make money (3) microservices (9) motivation (4) oops (4) podcast (6) poor education system (4) principles of microservices (5) problem-solving (7) programmer (5) programming (28) python (5) reality (3) seo (6) spring (3) success (10) success factor (4) technology (4) top 5 (7) typescript (3) wordpress (7)

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version