Repository navigation
Expand file tree
/
Copy pathPaymentService.java
More file actions
97 lines (79 loc) · 3.15 KB
/
Copy pathPaymentService.java
File metadata and controls
97 lines (79 loc) · 3.15 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
package co.hillmerch.payments;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PaymentService {
private static final Logger logger = LoggerFactory.getLogger( PaymentService.class );
/**
* A partir de un archivo plano con los pagos recibidos se registra el pago
* se cambia el estado al producto
*
* @param paymentsFile contiene un archivo plano, cada linea contiene un número de factura,
* el valor y la fecha (DD/MM/YYYY) del pago, los treas valores separados por espacios en blanco.
*/
public void processPayments(File paymentsFile){
try (Scanner sc = new Scanner( paymentsFile)){
while (sc.hasNextLine()) {
String[] paymentLine = sc.nextLine().split( " " );
Invoice invoice = findInvoice(paymentLine[0]);
Payment payment = savedPayment( invoice , paymentLine[1], paymentLine[2]);
logger.info( payment.toString() );
}
}catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
}
public void processPaymentsAsync(File paymentsFile){
try (Scanner sc = new Scanner( paymentsFile)){
ExecutorService executorService = Executors.newFixedThreadPool( 50);
while (sc.hasNextLine()) {
String[] paymentLine = sc.nextLine().split( " " );
CompletableFuture
.supplyAsync( ()-> findInvoice( paymentLine[0] ), executorService )
.thenApplyAsync( invoice -> savedPayment( invoice , paymentLine[1], paymentLine[2]) )
.thenAccept( payment -> logger.info( payment.toString() ));
}
}catch (IOException x) {
System.err.format("IOException: %s%n", x);
}
}
private Invoice findInvoice(String reference){
// Este método simula la consulta de la factura generalmente sobre una base de datos
// Se genera un valor aleatorio para simular el total de la factura
Invoice invoice = new Invoice();
invoice.setReference( reference );
Random random = new Random();
invoice.setTotal( (double) random.ints( 100, 5000).findAny().getAsInt() );
this.sleep( 1_000);//Una pausa de 1 segundo para simular el tiempo que
// podría durar el cargue de la factura desde la base de datos
return invoice;
}
private Payment savedPayment(Invoice invoice, String value, String dateOfPayment){
// Este método simula el registro del pago generalmente sobre una base de datos
Payment payment = new Payment();
payment.setInvoice( invoice );
payment.setValue( Double.parseDouble( value ));
String[] dateOfPaymentArray = dateOfPayment.split( "/" );
payment.setDateOfPayment( LocalDate.of( Integer.parseInt(dateOfPaymentArray[2]) ,
Integer.parseInt(dateOfPaymentArray[1]),
Integer.parseInt(dateOfPaymentArray[0])));
this.sleep( 2_000);//Una pausa de 2 segundos para simular el tiempo que
// podría durar el registro del pago en la base de datos
return payment;
}
private void sleep(long time) {
try {
Thread.sleep( time );
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}