E Commerce Shopping Cart
This program models an e commerce shopping cart.
Problem Statement
Write a Java program to model Product and Cart classes with total calculation.
Source Code
| 1 | class Product { |
| 2 | String name; double price; |
| 3 | Product(String n,double p){ name=n; price=p; } |
| 4 | } |
| 5 | class Cart { |
| 6 | Product product; int quantity; |
| 7 | Cart(Product p,int q){ product=p; quantity=q; } |
| 8 | void show(){ System.out.println("Cart: "+product.name+", Qty: "+quantity+", Total: "+(product.price*quantity)); } |
| 9 | } |
| 10 | public class EcommerceCart { |
| 11 | public static void main(String[] args) { |
| 12 | System.out.println("Eduinq E Commerce Cart"); |
| 13 | Product p=new Product("Laptop",50000); |
| 14 | Cart c=new Cart(p,2); |
| 15 | c.show(); |
| 16 | } |
| 17 | } |
Program Output
Eduinq E Commerce Cart Cart: Laptop, Qty: 2, Total: 100000.0
Explanation
Product models item with price. - Cart associates product with quantity. - Total calculated via multiplication. - This demonstrates encapsulation and association in enterprise systems. Conclusion: Shows how to model e commerce shopping cart.