-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathECommercePlatform.java
149 lines (123 loc) · 3.49 KB
/
ECommercePlatform.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.util.*;
abstract class Product{
private int productId;
private String name;
private double price;
public Product(int productId, String name, double price) {
this.productId = productId;
this.name = name;
this.price = price;
}
public int getProductId() {
return productId;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public abstract double calculateDiscount();
public void displayDetails() {
System.out.println("Product ID: " + productId);
System.out.println("Name: " + name);
System.out.println("Price: " + price);
}
}
class Electronics extends Product implements Taxable{
private double discountRate = 0.10; // 10% discount
private double taxRate = 0.15; // 15% tax
public Electronics(int productId, String name, double price) {
super(productId, name, price);
}
@Override
public double calculateDiscount() {
return getPrice() * discountRate;
}
@Override
public double calculateTax() {
return getPrice() * taxRate;
}
@Override
public String getTaxDetails() {
return "Tax Rate: " + (taxRate * 100) + "%";
}
}
class Clothing extends Product implements Taxable{
private double discountRate = 0.20; // 20% discount
private double taxRate = 0.08; // 8% tax
public Clothing(int productId, String name, double price) {
super(productId, name, price);
}
@Override
public double calculateDiscount() {
return getPrice() * discountRate;
}
@Override
public double calculateTax() {
return getPrice() * taxRate;
}
@Override
public String getTaxDetails() {
return "Tax Rate: " + (taxRate * 100) + "%";
}
}
class Groceries extends Product{
private double discountRate = 0.05; // 5% discount
public Groceries(int productId, String name, double price) {
super(productId, name, price);
}
@Override
public double calculateDiscount() {
return getPrice() * discountRate;
}
}
interface Taxable {
double calculateTax();
String getTaxDetails();
}
public class ECommercePlatform {
public static void main(String[] args) {
List<Product> products = new ArrayList<>();
Electronics laptop = new Electronics(101, "Laptop", 50000);
Clothing tshirt = new Clothing(102, "T-Shirt", 500);
Groceries apples = new Groceries(103, "Apples", 30);
products.add(laptop);
products.add(tshirt);
products.add(apples);
for (Product product : products) {
product.displayDetails();
double discount = product.calculateDiscount();
double tax = (product instanceof Taxable) ? ((Taxable) product).calculateTax() : 0;
double finalPrice = product.getPrice() + tax - discount;
System.out.println("Discount: " + discount);
System.out.println("Tax: " + tax);
System.out.println("Final Price: " + finalPrice);
System.out.println();
}
}
}
//SampleOutput
//Product ID: 101
//Name: Laptop
//Price: 50000.0
//Discount: 5000.0
//Tax: 7500.0
//Final Price: 52500.0
//
//Product ID: 102
//Name: T-Shirt
//Price: 500.0
//Discount: 100.0
//Tax: 40.0
//Final Price: 440.0
//
//Product ID: 103
//Name: Apples
//Price: 30.0
//Discount: 1.5
//Tax: 0.0
//Final Price: 28.5