Shopping cart is software/application used by product owners, sellers to help there costumer purchases and pay online.
This software is providing products online where buyers can choose
the product and add to cart (a virtual basket) and make online payment.
The software captures client payment information (usually credit card
number) and process the transaction using available secure payment
gateway.
My goal here is to create a very basic shopping cart program using
core java to get a better understanding of how shopping cart works.
So let’s start by writing an
IOrder interface. This interface is having method exposed to client.
|
|
public interface IOrder {
boolean addProduct(Product p);
boolean removeProduct(String pid) throws ProductNotFoundException;
Collection getCartDetails();
Product getProductFromCart(String pid) throws ProductNotFoundException;
int productCount();
double getCartPrice();
}
|
Here is over customized exception class
ProductNotFoundException, used to throw when any product is not found in cart while retrieving from Cart.
|
|
public class ProductNotFoundException extends Exception {
public ProductNotFoundException(){}
public ProductNotFoundException(String msg){
super(msg);
}
}
|
In a shopping cart, Product is a base entity, so we need a class representing Product.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Product implements java.io.Serializable {
public String pid;
public String pname;
public int qty;
public double price;
public Product(){}
public Product(String pid, String pname, int qty, double price) {
this.pid = pid;
this.pname = pname;
this.qty = qty;
this.price = price;
}
//getter/setter abbreviated.
}//class
|
Now lets create a class which implements
IOrder interface and do all the core work for us.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
public class Order implements IOrder {
public String uid;
private Map map;
public Order(){}
public Order(String uid){
this.uid=uid;
map = new HashMap();
}//Order
public boolean addProduct(Product p){
if(map.containsKey(p.getPid())){
Product p1 = map.get(p.getPid());
p1.setPrice(p1.getPrice()+p.getPrice());
p1.setQty(p1.getQty()+p.getQty());
return true;
}
map.put(p.getPid(),p);
return false;
}//addProduct
public boolean removeProduct(String pid)
throws ProductNotFoundException {
if(map.containsKey(pid)){
map.remove(pid);
return true;
}else throw new ProductNotFoundException(
"Product with ID "+pid+" is not Found.");
}
public Collection getCartDetails(){
return map.values();
}
public Product getProductFromCart(String pid)
throws ProductNotFoundException {
if(map.containsKey(pid)){
return map.get(pid);
}else {
throw new ProductNotFoundException(
"Product with ID "+pid+" is not Found.");
}
}
public int productCount(){
return map.size();
}
public double getCartPrice() {
double price = 0.0d;
Iterator iterator = getCartDetails().iterator();
while(iterator.hasNext()){
price+= iterator.next().getPrice();
}
return price;
}
}
|
Now time to test what we wrote. Create a class which Create get the order from customer, add product, get cart price etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class OrderTest {
public static void main (String[] s) throws Exception {
Order o = new Order("Tousif Khan");
o.addProduct(new Product("p101","Lux Soap",12,120));
o.addProduct(new Product("p102","Olive Oil",4,140));
System.out.println("\nNo. of Product : "+o.productCount());
System.out.println ("Order Places By: "+o.uid);
// code to remove product from Cart
// o.removeProduct("p103");
// Get Product By its Id
// Product p = o.getProductFromCart("p102");
System.out.println (o.getCartPrice());
final Iterator it = o.getCartDetails().iterator();
while(it.hasNext()){
System.out.println (it.next());
}
}
}
|
Download Sourcecode
ConversionConversion EmoticonEmoticon