Khalil Malik Saboor преди 8 години
родител
ревизия
00cc9a1a8d

+ 13
- 2
src/main/java/com/zipcoder/payment/PaymentPresenter.java Целия файл

17
 
17
 
18
 
18
 
19
     public String toStringByPayerName(Payment[] payments) {
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
     public String toStringById(Payment[] payments) {
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
 package com.zipcoder.payment;
1
 package com.zipcoder.payment;
2
 import java.util.Comparator;
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
     public void toStringByPayerName(){
34
     public void toStringByPayerName(){
35
         Payment[] payments = new Payment[2];
35
         Payment[] payments = new Payment[2];
36
         Payment paypal = new Paypal(4L, "Tamara Mowry", "tamara@mowry.com");
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
         payments[0] = paypal;
39
         payments[0] = paypal;
40
         payments[1] = check;
40
         payments[1] = check;
49
     public void toStringByID(){
49
     public void toStringByID(){
50
         Payment[] payments = new Payment[2];
50
         Payment[] payments = new Payment[2];
51
         Payment paypal = new Paypal(120L, "Tamara Mowry", "tamara@mowry.com");
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
         payments[0] = paypal;
54
         payments[0] = paypal;
55
         payments[1] = check;
55
         payments[1] = check;
56
 
56
 
57
         PaymentPresenter presenter = new PaymentPresenter();
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
         String actual = presenter.toStringById(payments);
60
         String actual = presenter.toStringById(payments);
61
         assertEquals(expected, actual);
61
         assertEquals(expected, actual);