1
1
package id .ac .ui .cs .advprog .tutorial7 .service ;
2
2
3
+ import java .util .List ;
3
4
import java .util .NoSuchElementException ;
5
+ import java .util .concurrent .CompletableFuture ;
6
+ import java .util .concurrent .ExecutionException ;
7
+ import java .util .concurrent .atomic .AtomicInteger ;
8
+ import java .util .concurrent .atomic .AtomicReference ;
9
+ import java .util .stream .Stream ;
4
10
5
11
import org .springframework .stereotype .Service ;
6
12
7
13
import id .ac .ui .cs .advprog .tutorial7 .core .bankapi .BankApi ;
8
14
import id .ac .ui .cs .advprog .tutorial7 .core .miscapi .HolidayApi ;
9
15
import id .ac .ui .cs .advprog .tutorial7 .core .vaapi .VAHelper ;
10
- import id .ac .ui .cs .advprog .tutorial7 .core .vaapi .VirtualAccount ;
11
16
import id .ac .ui .cs .advprog .tutorial7 .model .PaymentResponse ;
12
17
13
18
@ Service
@@ -31,30 +36,52 @@ public String createNewVA(int vaAmount, String bank) {
31
36
32
37
@ Override
33
38
public PaymentResponse pay (String va , int payAmount , String day , String time ) {
34
-
35
- if (holidayApi .isHoliday (day )) return new PaymentResponse (0 , "Cannot pay on holidays" );
39
+ CompletableFuture <Boolean > isHolidayFuture = CompletableFuture .supplyAsync (() -> holidayApi .isHoliday (day ));
40
+ CompletableFuture <Integer > vaAmountFuture = CompletableFuture .supplyAsync (() -> vaHelper .getVAAmount (va ));
41
+ CompletableFuture <BankApi > bankFuture = CompletableFuture .supplyAsync (() -> vaHelper .getBankByVA (va ));
42
+ CompletableFuture <String > paymentValidateFuture ;
43
+ CompletableFuture <Boolean > bankClosedFuture ;
36
44
37
45
int vaAmount ;
38
46
BankApi bankApi ;
47
+ boolean paymentSuccessful = false ;
48
+
39
49
try {
40
- vaAmount = vaHelper .getVAAmount (va );
41
- bankApi = vaHelper .getBankByVA (va );
42
- } catch (NoSuchElementException e ) {
43
- return new PaymentResponse (0 , "VA number not found" );
44
- }
50
+ bankApi = bankFuture .get ();
51
+ bankClosedFuture = CompletableFuture .supplyAsync (() -> bankApi .isBankClosed (time , payAmount ));
52
+ vaAmount = vaAmountFuture .get ();
53
+ paymentValidateFuture = CompletableFuture .supplyAsync (() -> vaHelper .validatePayment (va , vaAmount , payAmount ));
45
54
46
- if (bankApi .isBankClosed (time , vaAmount )) return new PaymentResponse (0 , "Bank already closed, please try again tomorrow" );
47
-
48
- String errorMsg = vaHelper .validatePayment (va , vaAmount , payAmount );
49
- if (!errorMsg .equals ("" )) return new PaymentResponse (0 , errorMsg );
55
+ if (isHolidayFuture .get ()) {
56
+ return new PaymentResponse (0 , "Cannot pay on holidays" );
57
+ }
50
58
51
- boolean paymentSuccessfull = bankApi .pay (payAmount );
52
- vaHelper .logVAPayment (va , paymentSuccessfull );
53
- if (!paymentSuccessfull ) return new PaymentResponse (0 , "Payment unsuccessfull, please try again" );
54
- else return new PaymentResponse (1 , "Payment successfull" );
59
+ if ((bankClosedFuture .get ())) {
60
+ return new PaymentResponse (0 , "Bank already closed, please try again tomorrow" );
61
+ }
55
62
56
-
63
+ String errorMsg = paymentValidateFuture .get ();
64
+
65
+ if (!errorMsg .equals ("" )) {
66
+ return new PaymentResponse (0 , errorMsg );
67
+ }
57
68
69
+ CompletableFuture <Boolean > bankPaymentFuture = CompletableFuture .supplyAsync (() -> bankApi .pay (payAmount ));
70
+ paymentSuccessful = bankPaymentFuture .get ();
71
+
72
+ } catch (NoSuchElementException e ) {
73
+ return new PaymentResponse (0 , "VA number not found" );
74
+ } catch (InterruptedException | ExecutionException ignored ){
75
+
76
+ }
77
+
78
+ boolean paymentSuccessfulFinal = paymentSuccessful ;
79
+ CompletableFuture .runAsync (() -> vaHelper .logVAPayment (va , paymentSuccessfulFinal ));
80
+
81
+ if (paymentSuccessful ) {
82
+ return new PaymentResponse (1 , "Payment successful" );
83
+ }
84
+ return new PaymentResponse (0 , "Payment unsuccessful, please try again" );
58
85
}
59
86
60
87
}
0 commit comments