Purchasing cart is software program/software utilized by product house owners, sellers to assist there costumer purchases and pay on-line.
This software program is offering merchandise on-line the place patrons can select the product and add to cart (a digital basket) and make on-line cost. The software program captures consumer cost info (normally bank card quantity) and course of the transaction utilizing accessible safe cost gateway.
My purpose right here is to create a really fundamental buying cart program utilizing core java to get a greater understanding of how buying cart works.
So let’s begin by writing an IOrder
interface. This interface is having methodology uncovered to consumer.
1
2
3
4
5
6
7
8
|
public interface IOrder {
boolean addProduct(Product p);
boolean removeProduct(String pid) throws ProductNotFoundException;
Assortment getCartDetails();
Product getProductFromCart(String pid) throws ProductNotFoundException;
int productCount();
double getCartPrice();
}
|
Right here is over personalized exception class ProductNotFoundException
, used to throw when any product is just not present in cart whereas retrieving from Cart.
1
2
3
4
5
6
|
public class ProductNotFoundException extends Exception {
public ProductNotFoundException(){}
public ProductNotFoundException(String msg){
tremendous(msg);
}
}
|
In a buying cart, Product is a base entity, so we’d like a category 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 value;
public Product(){}
public Product(String pid, String pname, int qty, double value) {
this.pid = pid;
this.pname = pname;
this.qty = qty;
this.value = value;
}
//getter/setter abbreviated.
}//class
|
Now lets create a category which implements IOrder
interface and do all of 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;
non-public 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.take away(pid);
return true;
}else throw new ProductNotFoundException(
“Product with ID “+pid+” is just not Discovered.”);
}
public Assortment 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 just not Discovered.”);
}
}
public int productCount(){
return map.dimension();
}
public double getCartPrice() {
double value = 0.0d;
Iterator iterator = getCartDetails().iterator();
whereas(iterator.hasNext()){
value+= iterator.subsequent().getPrice();
}
return value;
}
}
|
Now time to check what we wrote. Create a category which Create get the order from buyer, add product, get cart value and many others.
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 principal (String[] s) throws Exception {
Order o = new Order(“Tousif Khan”);
o.addProduct(new Product(“p101”,“Lux Cleaning soap”,12,120));
o.addProduct(new Product(“p102”,“Olive Oil”,4,140));
System.out.println(“nNo. of Product : “+o.productCount());
System.out.println (“Order Locations By: “+o.uid);
// code to take away product from Cart
// o.removeProduct(“p103”);
// Get Product By its Id
// Product p = o.getProductFromCart(“p102”);
System.out.println (o.getCartPrice());
last Iterator it = o.getCartDetails().iterator();
whereas(it.hasNext()){
System.out.println (it.subsequent());
}
}
}
|
Supply techzoo.org