Mexi hace 8 años
padre
commit
3f084ac804

+ 35
- 2
src/main/java/com/zipcoder/payment/PaymentPresenter.java Ver fichero

@@ -1,11 +1,11 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.sql.SQLOutput;
3 4
 import java.util.Arrays;
4 5
 
5 6
 public class PaymentPresenter {
6 7
 
7
-    private  Payment payment;
8
-    private int n;
8
+
9 9
     public String toString(Payment[] payments){
10 10
         if(payments.length == 0) {
11 11
 
@@ -27,4 +27,37 @@ public class PaymentPresenter {
27 27
 
28 28
     }
29 29
 
30
+
31
+    public String toStringByPayerName(Payment[] payments){
32
+        if(payments.length == 0){
33
+            return "";
34
+        }
35
+        else{
36
+            Arrays.sort(payments,new PaymentSortByPayer());
37
+            StringBuilder sb = new StringBuilder();
38
+                for(Payment p : payments){
39
+                System.out.println(p.getShortDescription());
40
+                sb.append(p.getShortDescription()+ "\n");
41
+            }
42
+            return sb.toString();
43
+        }
44
+
45
+    }
46
+
47
+    public String toStringById(Payment[] payments){
48
+        if(payments.length == 0){
49
+            return "";
50
+        }
51
+        else{
52
+            Arrays.sort(payments,new PaymentSortById());
53
+            StringBuilder sb = new StringBuilder();
54
+            for(Payment p: payments){
55
+                System.out.println(p.getShortDescription());
56
+                sb.append(p.getShortDescription());
57
+            }
58
+            return sb.toString();
59
+        }
60
+
61
+    }
62
+
30 63
 }

+ 10
- 0
src/main/java/com/zipcoder/payment/PaymentSortById.java Ver fichero

@@ -0,0 +1,10 @@
1
+package com.zipcoder.payment;
2
+
3
+import java.util.Comparator;
4
+
5
+public class PaymentSortById implements Comparator<Payment> {
6
+    public int compare(Payment o1, Payment o2) {
7
+        int n = o1.getId().compareTo(o2.getId());
8
+        return n;
9
+    }
10
+}

+ 37
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Ver fichero

@@ -32,4 +32,41 @@ public class PaymentPresenterTest {
32 32
         Assert.assertEquals(expected, actual);
33 33
 
34 34
     }
35
+
36
+    @Test
37
+    public void toStringByPayerName(){
38
+
39
+        Payment[] payments = new Payment[2];
40
+        Payment paypal = new PayPal(4L, "Tamara Mowry", "tamara@mowry.com");
41
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
42
+
43
+        payments[0] = paypal;
44
+        payments[1] = check;
45
+
46
+        PaymentPresenter presenter = new PaymentPresenter();
47
+        String expected = "PayPal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
48
+
49
+        String actual = presenter.toStringByPayerName(payments);
50
+        Assert.assertEquals(expected, actual);
51
+
52
+    }
53
+
54
+    @Test
55
+    public void toStringById(){
56
+
57
+        Payment[] payments = new Payment[2];
58
+        Payment paypal = new PayPal(120L, "Tamara Mowry", "tamara@mowry.com");
59
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
60
+
61
+        payments[0] = paypal;
62
+        payments[1] = check;
63
+
64
+        PaymentPresenter presenter = new PaymentPresenter();
65
+        String expected = "Check Tia Mowry ***4551\nPayPal Tamara Mowry tamara@mowry.com\n";
66
+
67
+        String actual = presenter.toString(payments);
68
+        Assert.assertEquals(expected, actual);
69
+
70
+
71
+    }
35 72
 }