Online Shopping System

This program models an online shopping system.

Problem Statement

Write a Java program to model Product and Cart classes with association.

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 showCart(){
9 System.out.println("Cart: "+product.name+", Quantity: "+quantity+", Total: "+(product.price*quantity));
10 }
11 }
12 public class OnlineShopping {
13 public static void main(String[] args) {
14 System.out.println("Eduinq Online Shopping");
15 Product p=new Product("Mobile",15000);
16 Cart c=new Cart(p,2);
17 c.showCart();
18 }
19 }

Program Output

Eduinq Online Shopping
Cart: Mobile, Quantity: 2, Total: 30000.0

Explanation

Product class models item with name and price. - Cart associates product with quantity, calculating total. - This demonstrates how OOP can model real world e commerce systems with encapsulation and association. - Cohesion and loose coupling are maintained, making system extensible. Conclusion: Shows how to model online shopping system using OOP.

Quick Links to Explore