Khalil Malik Saboor 8 лет назад
Родитель
Сommit
00cc9a1a8d

+ 13
- 2
src/main/java/com/zipcoder/payment/PaymentPresenter.java Просмотреть файл

@@ -17,10 +17,21 @@ public class PaymentPresenter {
17 17
 
18 18
 
19 19
     public String toStringByPayerName(Payment[] payments) {
20
-        return null;
20
+        Arrays.sort(payments, new PaymentsSortByPayer());
21
+        StringBuilder sb = new StringBuilder();
22
+        for (Payment o : payments){
23
+            sb.append(o.getShortDescription() + "\n");
24
+        }
25
+
26
+        return sb.toString();
21 27
     }
22 28
 
23 29
     public String toStringById(Payment[] payments) {
24
-        return null;
30
+        Arrays.sort(payments, (Payment a, Payment b) -> Long.compare(a.getID(), b.getID()));
31
+        StringBuilder sb = new StringBuilder();
32
+        for (Payment o : payments){
33
+            sb.append(o.getShortDescription() + "\n");
34
+        }
35
+        return sb.toString();
25 36
     }
26 37
 }

+ 7
- 1
src/main/java/com/zipcoder/payment/PaymentsSortByPayer.java Просмотреть файл

@@ -1,6 +1,12 @@
1 1
 package com.zipcoder.payment;
2 2
 import java.util.Comparator;
3
+import java.util.function.Function;
3 4
 
4
-public class PaymentsSortByPayer {
5
+public class PaymentsSortByPayer implements Comparator<Payment> {
5 6
 
7
+
8
+    @Override
9
+    public int compare(Payment o1, Payment o2) {
10
+        return o1.getPayerName().compareTo(o2.getPayerName());
11
+    }
6 12
 }

+ 3
- 3
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Просмотреть файл

@@ -34,7 +34,7 @@ public class PaymentPresenterTest {
34 34
     public void toStringByPayerName(){
35 35
         Payment[] payments = new Payment[2];
36 36
         Payment paypal = new Paypal(4L, "Tamara Mowry", "tamara@mowry.com");
37
-        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551")
37
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
38 38
 
39 39
         payments[0] = paypal;
40 40
         payments[1] = check;
@@ -49,13 +49,13 @@ public class PaymentPresenterTest {
49 49
     public void toStringByID(){
50 50
         Payment[] payments = new Payment[2];
51 51
         Payment paypal = new Paypal(120L, "Tamara Mowry", "tamara@mowry.com");
52
-        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551")
52
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
53 53
 
54 54
         payments[0] = paypal;
55 55
         payments[1] = check;
56 56
 
57 57
         PaymentPresenter presenter = new PaymentPresenter();
58
-        String expected = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
58
+        String expected = "Check Tia Mowry ***4551\nPaypal Tamara Mowry tamara@mowry.com\n";
59 59
 
60 60
         String actual = presenter.toStringById(payments);
61 61
         assertEquals(expected, actual);