In this short article we are going to learn how to implement basic shopping cart operations, to be precise we will mainly implement five operations which are:
- Get list of items
- Get items total price
- Add an item
- Remove one item
- Remove all items
Now we know the number operations we are going to implement, let’s also identify different data structure we shall need in order to achieve that task. For storing items, we will be using a list which is one of the four types of collection offered by Dart, it is equivalent to array and is an ordered collection of items, starting with index 0. In addition, we shall need two classes, one will be used as model class defining the shape of item and another one will encapsulate the five methods that will implement the five operations.
That was enough of theories, let get our hands dirty 😎
Let’s start by defining our model class called Item which will have four instance variables and constructor where our instance variables value will be set.
Now we have our model class in place, time to implement the Cart class, basically this class will contain the implementation of our five operations as mentioned above. Create a class called Cart and declare an empty list of type Item referring to our model class created earlier. We are also going to add a getter for returning the list of items directly.
Note that, we are using underscore(_) at the beginning of variable name _items to mark it private member of the Cart class. We have also used UnmodifiableListView() method on the getter items in order to make it immutable or read only. We have imported Dart collection package so that we can use UnmodifiableListView() method.
So far, we have our first operation implemented, the next method now will take care of the second operation which is get total Price, to achieve that we are going to declare a getter called totalPrice of type integer, the totalPrice getter will then call the private method called _totalPrice(). The _totalPrice method will have a local variable called total that will have an initial value of 0. Inside that same method we will iterate through the list of items with the help of forEach loop and then compute the total price, after the calculation we will then store the total price inside total variable and return it in the end.
Two operations implemented so far, time for the adding method now, before we can implement that, we shall first need to code a helper method which will assist us to check whether the item is already added in the list or not. Let us create a private method called _checkItem, the method will be returning a Boolean, taking item as parameter and will also have a local variable called isExist that will have false as initial value. In order to perform the checking process, we shall loop through the list of items and start comparing the value of each item id to the one we are trying to add, in case the item already exist, we return true otherwise we return the initial value false.
The item will be passed as parameter to the addItem method and call the checkItem method before adding the item, in case the helper method return false, we add the item in our list using one of the inbuilt method of list called add() otherwise we print “item added already”.
After the addItem method, we have removeOneItem method that removes a single item in the list, this method will be taking the item id as parameter and we will leverage again another inbuilt function of list called removeWhere(), which takes a callback function where we are going to implement the logic for removing a specific item, given our method is getting already the item id, we will be just using that id to find the exact item we want to remove inside the callback function of removeWhere() and clear it.
Time to implement of our last operation, let us create a method called removeAllItem, this method will basically clear everything in the list, to accomplish that, we will take advantage of the clear() method which is another inbuilt method of list.
Finally we have implemented the five operations, let’s initialize our Cart class inside the Dart main method and start calling different methods we created for testing.
You can find the full codes of this article by clicking here.
We have reached the end of our tutorial, if you have found it useful, feel free to clap or comment your thoughts. Any suggestions or feedback will be welcomed. You can also connect with me on Twitter, LinkedIn, GitHub.