-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMart.java
More file actions
175 lines (138 loc) · 3.62 KB
/
Mart.java
File metadata and controls
175 lines (138 loc) · 3.62 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.util.ArrayList;
public class Mart {
public static void main(String[] args) {
Electronics e=new Electronics(101, "wire", 20,2,10);
Cloths c=new Cloths(201, "T-Shirt", 299, "M",5,50);
e.display();
c.display();
user u=new user(200, "rag ");
try {
u.addtocart(c);
u.addtocart(e);
}
catch (outofstockException h) {
System.out.println(h);
}
u.calculatetotal();
u.checkout( new Credit() );
}
}
interface Discount{
double calculatediscount(double percentage);
}
abstract class Product implements Discount{
private int id;
private String name;
private double price;
int stock;
Product(int id, String name, double price, int stock){
this.id=id;
this.name=name;
this.price=price;
this.stock=stock;
}
abstract void display();
int id(){
return id;
}
String name(){
return name;
}
double price(){
return price;
}
double price(double percentage){
return calculatediscount(percentage);
}
@Override
public double calculatediscount(double percentage) {
return price-(price*percentage/100);
}
}
class Electronics extends Product{
int Warrenty;
int percentage;
Electronics(int id,String name,double price,int Warrenty,int stock){
super(id, name, price,stock);
this.Warrenty=Warrenty;
}
@Override
double price() {
return calculatediscount(percentage);
}
@Override
public void display(){
System.out.println("Name : "+name());
System.out.println("Price : "+price());
System.out.println("Warrenty(months) : "+Warrenty);
}
}
class Cloths extends Product{
String size;
double percentage;
Cloths(int id,String name,double price,String size,double percentage,int stock){
super(id,name,price,stock);
this.size=size;
this.percentage=percentage;
}
// Inside class Cloths
@Override
double price() {
return calculatediscount(percentage);
}
@Override
void display() {
System.out.println("Name : "+name());
System.out.println("Price : "+price());
System.out.println("Size : "+size);
}
}
class user{
int userid;
String username;
ArrayList<Product> cart=new ArrayList<>();
user(int userid,String username){
this.userid=userid;
this.username=username;
}
void addtocart(Product p) throws outofstockException{
if(p.stock>0){
cart.add(p);
p.stock=p.stock-1;
}
else{
throw new outofstockException();
}
System.out.println("product Added "+p);
}
double calculatetotal(){
double sum=0;
for(Product p: cart){
sum=p.price()+sum;
}
return sum;
}
void checkout(paymentmethod pm){
double Total=calculatetotal();
System.out.println("Total amount :"+Total);
pm.pay(Total);
cart.clear();
System.out.println("Order placed");
}
}
class outofstockException extends Exception{}
interface paymentmethod{
void pay(double amount);
}
class Credit implements paymentmethod{
@Override
public void pay(double amount) {
System.out.println("amount : "+ amount+"paid via credid card");
}
}
class UPI implements paymentmethod{
@Override
public void pay(double amount) {
System.out.println("amount :"+amount+" paid via UPI");
}
}