-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecommerce.java
More file actions
225 lines (183 loc) · 6.25 KB
/
ecommerce.java
File metadata and controls
225 lines (183 loc) · 6.25 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import java.util.InputMismatchException;
import java.util.Scanner;
interface PaymentProvider {
void processPayment(double amount, PaymentMethod method);
}
class Paytm implements PaymentProvider {
public void processPayment(double amount, PaymentMethod method) {
System.out.println("Intializing" + amount + " INR through Paytm using " + method.getType());
method.collectDetails();
System.out.println("Payment of "+ amount + " successfull through Paytm!");
}
}
class AmazonPay implements PaymentProvider {
public void processPayment(double amount, PaymentMethod method) {
System.out.println("Intializing" + amount + " INR through Amazon Pay using " + method.getType());
method.collectDetails();
System.out.println("Payment of "+ amount + " successfull through Amazon Pay!");
}
}
class PhonePe implements PaymentProvider {
public void processPayment(double amount, PaymentMethod method) {
System.out.println("Intializing" + amount + " INR through PhonePe using " + method.getType());
method.collectDetails();
System.out.println("Payment of "+ amount + " successfull through PhonePe!");
}
}
interface PayDetail {
void collectDetails();
}
abstract class PaymentMethod {
private String type;
private PayDetail detailsCollector;
public PaymentMethod(String type, PayDetail detailsCollector) {
this.type = type;
this.detailsCollector = detailsCollector;
}
public String getType() {
return type;
}
public void collectDetails() {
detailsCollector.collectDetails();
}
}
class WalletDetails implements PayDetail {
private String phoneNumber;
public void collectDetails() {
Scanner scanner = new Scanner(System.in);
boolean check = true;
while(check)
{ int cnt = 0;
System.out.println("please enter a 10 digit phone number");
this.phoneNumber = scanner.nextLine();
if(phoneNumber.length()==10)
{
for(int i = 0 ; i< phoneNumber.length() ; i++){
if((phoneNumber.charAt(i)>47 && phoneNumber.charAt(i)<58))
{
cnt++;
}
else{
cnt--;
}
}
if(cnt==10){
check = false;
}
}
}
}
}
class Wallet extends PaymentMethod {
public Wallet() {
super("Wallet", new WalletDetails());
}
}
class UPIDetails implements PayDetail {
private String upiId;
public void collectDetails() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter UPI ID: ");
this.upiId = scanner.nextLine();
}
}
class UPI extends PaymentMethod {
public UPI() {
super("UPI", new UPIDetails());
}
}
class CardDetails implements PayDetail {
private String cardNumber;
private String expiry;
private String cvv;
public void collectDetails() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Card Number: ");
this.cardNumber = scanner.nextLine();
if(cardNumber.length()!=16){
System.out.println("Your Card number is invalid, Please restart the process");
System.exit(1);
}
System.out.print("Enter Expiry (MM/YY): ");
this.expiry = scanner.nextLine();
System.out.print("Enter CVV: ");
this.cvv = scanner.nextLine();
if(cvv.length()!=3){
System.out.println("Your Card cvv is invalid, Please restart the process");
System.exit(1);
}
}
}
class Card extends PaymentMethod {
public Card(String type) {
super(type, new CardDetails());
}
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double amount;
while (true) {
try {
System.out.print("Enter amount: ");
amount = scanner.nextDouble();
if (amount <= 0) {
throw new IllegalArgumentException("Amount must be positive.");
}
break; // Exit the loop if input is valid
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number.");
scanner.next(); // Clear the invalid input from the scanner
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
System.out.println("Choose payment provider:");
System.out.println("Press 1 for Paytm");
System.out.println("Press 2 for Amazon Pay");
System.out.println("Press 3 for PhonePe");
int providerChoice = scanner.nextInt();
PaymentProvider provider = null;
switch (providerChoice) {
case 1:
provider = new Paytm();
break;
case 2:
provider = new AmazonPay();
break;
case 3:
provider = new PhonePe();
break;
default:
System.out.println("Please choose from the above options");
return;
}
System.out.println("Choose payment method:");
System.out.println("Press 1 for Wallet");
System.out.println("Press 2 for UPI");
System.out.println("Press 3 for Credit Card");
System.out.println("Press 4 for Debit Card");
int methodChoice = scanner.nextInt();
PaymentMethod method = null;
switch (methodChoice) {
case 1:
method = new Wallet();
break;
case 2:
method = new UPI();
break;
case 3:
method = new Card("Credit Card");
break;
case 4:
method = new Card("Debit card");
break;
default:
System.out.println("Select the option from given methods");
return;
}
if (provider != null && method != null) {
provider.processPayment(amount, method);
}
}
}