Nicholas Maidanos 8 anni fa
parent
commit
69178a58e4

+ 39
- 1
src/main/java/com/zipcoder/payment/PaymentPresenter.java Vedi File

@@ -2,6 +2,44 @@ package com.zipcoder.payment;
2 2
 
3 3
 public class PaymentPresenter {
4 4
 
5
-    public
5
+    PaymentSort ofSort;
6
+
7
+    public PaymentPresenter(PaymentSort of){
8
+        this.ofSort = of;
9
+    }
10
+
11
+    public String toString(Payment[] payments){
12
+        StringBuilder sb = new StringBuilder();
13
+
14
+        sortBy(payments);
15
+
16
+        for(int i = 0; i < payments.length; i++){
17
+            sb.append(payments[i].getShortDescription());
18
+            sb.append("\n");
19
+        }
20
+
21
+        return sb.toString();
22
+    }
23
+
24
+    public Payment[] byShortName(Payment[] payments){
25
+        boolean isNotSorted = true;
26
+
27
+        while(isNotSorted){
28
+            isNotSorted = false;
29
+            for(int i = 0; i < payments.length - 1; i++){
30
+                if(payments[i].getShortDescription().compareTo(payments[i + 1].getShortDescription()) > 0){
31
+                    isNotSorted = true;
32
+                    Payment temp = payments[i];
33
+                    payments[i] = payments[i + 1];
34
+                    payments[i+1] = temp;
35
+
36
+                }
37
+            }
38
+        }
39
+        return payments;
40
+    }
41
+
42
+
43
+
6 44
 
7 45
 }

+ 5
- 0
src/main/java/com/zipcoder/payment/PaymentSort.java Vedi File

@@ -0,0 +1,5 @@
1
+package com.zipcoder.payment;
2
+
3
+public enum PaymentSort {
4
+    ID, PAYERNAME, SHORTDESCRIPTION
5
+}

+ 27
- 4
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Vedi File

@@ -1,12 +1,13 @@
1 1
 package com.zipcoder.payment;
2 2
 
3 3
 import org.junit.Test;
4
-import static org.junit.jupiter.api.Assertions.*;
5 4
 
6
-class PaymentPresenterTest {
5
+import static org.junit.Assert.*;
6
+
7
+public class PaymentPresenterTest {
7 8
 
8 9
     @Test
9
-    void emptyTestOne() {
10
+    public void emptyTestOne() {
10 11
         //When
11 12
         Payment[] payments = new Payment[0];
12 13
         PaymentPresenter presenter = new PaymentPresenter();
@@ -20,4 +21,26 @@ class PaymentPresenterTest {
20 21
         assertEquals(expected, actual);
21 22
     }
22 23
 
23
-}
24
+    @Test
25
+    public void SortByShortDescriptionTest() {
26
+        //When
27
+        Payment[] payments = new Payment[3];
28
+        Payment cc = new CreditCard(12, 4111131,"Nicholas Maidanos", 4, 11);
29
+        Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
30
+        Payment ck = new Check(46,"Nicholas Maidanos", "8934", "3343234");
31
+
32
+        PaymentPresenter paymentp = new PaymentPresenter(PaymentSort.SHORTDESCRIPTION);
33
+
34
+        payments[0] = pp;
35
+        payments[1] = cc;
36
+        payments[2] = ck;
37
+        //Expect
38
+        String expected = "CC Nicholas Maidanos 1131 4/11\nCheck Nicholas Maidanos ****3234\nPayPal Nicholas Maidanos nmaidanos@gmail.com\n";
39
+
40
+        //Actual
41
+        String actual = paymentp.toString(payments);
42
+
43
+        assertEquals(expected, actual);
44
+    }
45
+
46
+}