Trinh Tong 7 anos atrás
pai
commit
048d97d92e

+ 1
- 1
src/main/java/com/zipcoder/payment/Check.java Ver arquivo

@@ -60,7 +60,7 @@ public class Check implements Payment {
60 60
     @Override
61 61
     public String getShortDescription() {
62 62
 
63
-        return "Check " + this.getPayerName() + " ****" + this.getAccountNumber().substring(this.getAccountNumber().length() - 4);
63
+        return "Check " + this.getPayerName() + " ***" + this.getAccountNumber().substring(this.getAccountNumber().length() - 4);
64 64
     }
65 65
 
66 66
     public void setId(Long l) {

+ 75
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Ver arquivo

@@ -1,6 +1,81 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+import java.util.Collections;
6
+import java.util.Comparator;
7
+
3 8
 public class PaymentPresenter {
9
+    PaymentSortByPayer sorter;
10
+
11
+    public PaymentPresenter() {
12
+        sorter = new PaymentSortByPayer();
13
+    }
14
+
15
+    public String toString(Payment[] payments) {
16
+
17
+        String s = "";
18
+        StringBuilder sb = new StringBuilder();
19
+
20
+        if (payments.length > 0) {
21
+            Payment temp = payments[0];
22
+
23
+            for (int i = 0; i < payments.length; i++) {
24
+                for (int j = 0; j < payments.length - i - 1; j++) {
25
+                    if (sorter.compare(payments[j], payments[j + 1]) > 0) {
26
+                        temp = payments[j];
27
+                        payments[j] = payments[j + 1];
28
+                        payments[j + 1] = temp;
29
+                    }
30
+                }
31
+            }
32
+        } else {
33
+            return s;
34
+        }
35
+
36
+        for (int i = 0; i < payments.length; i++) {
37
+            sb.append(payments[i].getShortDescription() + "\n");
38
+        }
39
+
40
+        return sb.toString();
41
+    }
42
+
43
+    public String toStringByPayerName(Payment[] payments) {
44
+
45
+        String s = "";
46
+        StringBuilder sb = new StringBuilder();
4 47
 
48
+        ArrayList<Payment> sortedPayment;
49
+        if (payments.length > 0) {
5 50
 
51
+            sortedPayment = new ArrayList<>(Arrays.asList(payments));
52
+            Collections.sort(sortedPayment, Comparator.comparing(Payment::getPayerName));
53
+
54
+        } else {
55
+            return s;
56
+        }
57
+
58
+        return sortedString(sortedPayment);
59
+    }
60
+
61
+    public String toStringById(Payment[] payments) {
62
+
63
+        StringBuilder sb = new StringBuilder();
64
+        ArrayList<Payment> payments1 = new ArrayList<>(Arrays.asList(payments));
65
+        Collections.sort(payments1, Comparator.comparingLong(Payment::getId));
66
+
67
+        return sortedString(payments1);
68
+    }
69
+
70
+    public String sortedString (ArrayList<Payment> p) {
71
+        StringBuilder sb = new StringBuilder();
72
+
73
+        for (Payment pm : p) {
74
+            sb.append(pm.getShortDescription() + "\n");
75
+        }
76
+
77
+        return sb.toString();
78
+    }
6 79
 }
80
+
81
+

+ 4
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java Ver arquivo

@@ -9,4 +9,8 @@ public class PaymentSortByPayer implements Comparator<Payment> {
9 9
         return o1.compareTo(o2);
10 10
     }
11 11
 
12
+    public int compareByPayerName(String s1, String s2) {
13
+        return s1.compareTo(s2);
14
+    }
15
+
12 16
 }

+ 63
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Ver arquivo

@@ -1,4 +1,67 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.assertEquals;
6
+
3 7
 public class PaymentPresenterTest {
8
+
9
+    @Test
10
+    public void testNoPayments() {
11
+        Payment[] payments = new Payment[0];
12
+        PaymentPresenter presenter = new PaymentPresenter();
13
+        String expected = "";
14
+
15
+        String actual = presenter.toString(payments);
16
+
17
+        assertEquals(expected, actual);
18
+    }
19
+
20
+    @Test
21
+    public void testMultiplePaymentSort() {
22
+        Payment[] payments = new Payment[2];
23
+        Payment paypal = new PayPal(4L, "Tia Mowry", "tia@mowry.com");
24
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
25
+
26
+        payments[0] = paypal;
27
+        payments[1] = check;
28
+
29
+        PaymentPresenter presenter = new PaymentPresenter();
30
+        String expected = "Check Tia Mowry ***4551\nPayPal Tia Mowry tia@mowry.com\n";
31
+
32
+        String actual = presenter.toString(payments);
33
+        assertEquals(expected, actual);
34
+    }
35
+
36
+    @Test
37
+    public void testMultiPayersName() {
38
+        Payment[] payments = new Payment[2];
39
+        Payment paypal = new PayPal(4L, "Tamara Mowry", "tamara@mowry.com");
40
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
41
+
42
+        payments[0] = paypal;
43
+        payments[1] = check;
44
+
45
+        PaymentPresenter presenter = new PaymentPresenter();
46
+        String expected = "PayPal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
47
+
48
+        String actual = presenter.toStringByPayerName(payments);
49
+        assertEquals(expected, actual);
50
+    }
51
+
52
+    @Test
53
+    public void testLambaId() {
54
+        Payment[] payments = new Payment[2];
55
+        Payment paypal = new PayPal(120L, "Tamara Mowry", "tamara@mowry.com");
56
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
57
+
58
+        payments[0] = paypal;
59
+        payments[1] = check;
60
+
61
+        PaymentPresenter presenter = new PaymentPresenter();
62
+        String expected = "Check Tia Mowry ***4551\nPayPal Tamara Mowry tamara@mowry.com\n";
63
+
64
+        String actual = presenter.toStringById(payments);
65
+        assertEquals(expected, actual);
66
+    }
4 67
 }