瀏覽代碼

Finished still needs some refactoring I think

Nicholas Maidanos 8 年之前
父節點
當前提交
902c310498

+ 72
- 8
src/main/java/com/zipcoder/payment/PaymentPresenter.java 查看文件

1
 package com.zipcoder.payment;
1
 package com.zipcoder.payment;
2
 
2
 
3
+import com.zipcoder.paymentSort.ById;
4
+import com.zipcoder.paymentSort.ByPayer;
5
+import com.zipcoder.paymentSort.PaymentOrder;
6
+import com.zipcoder.paymentSort.ByShortDescription;
7
+
8
+import java.util.Comparator;
9
+
3
 public class PaymentPresenter {
10
 public class PaymentPresenter {
4
 
11
 
5
-    PaymentSort ofSort;
12
+    PaymentOrder order = null;
6
 
13
 
7
-    public PaymentPresenter(PaymentSort of){
8
-        this.ofSort = of;
9
-    }
14
+    //public PaymentPresenter(){}
10
 
15
 
11
     public String toString(Payment[] payments){
16
     public String toString(Payment[] payments){
12
         StringBuilder sb = new StringBuilder();
17
         StringBuilder sb = new StringBuilder();
13
 
18
 
14
-        sortBy(payments);
19
+        if(this.order != null){
20
+            this.orderBy(payments);
21
+        }
15
 
22
 
16
         for(int i = 0; i < payments.length; i++){
23
         for(int i = 0; i < payments.length; i++){
17
             sb.append(payments[i].getShortDescription());
24
             sb.append(payments[i].getShortDescription());
21
         return sb.toString();
28
         return sb.toString();
22
     }
29
     }
23
 
30
 
24
-    public Payment[] byShortName(Payment[] payments){
31
+    public void setOrderBy(PaymentOrder order){
32
+        this.order = order;
33
+    }
34
+
35
+    public void orderBy(Payment[] payments){
36
+        switch(this.order){
37
+            case SHORTDESCRIPTION:
38
+                this.bubbleSort(payments, new ByShortDescription());
39
+                break;
40
+            case PAYERNAME:
41
+                this.bubbleSort(payments, new ByPayer());
42
+                break;
43
+            case ID:
44
+                this.bubbleSort(payments, new ById());
45
+                break;
46
+            default:
47
+                break;
48
+        }
49
+    }
50
+
51
+    public void bubbleSort(Payment[] payments, Comparator<Payment> comparator){
25
         boolean isNotSorted = true;
52
         boolean isNotSorted = true;
26
 
53
 
27
         while(isNotSorted){
54
         while(isNotSorted){
28
             isNotSorted = false;
55
             isNotSorted = false;
29
             for(int i = 0; i < payments.length - 1; i++){
56
             for(int i = 0; i < payments.length - 1; i++){
30
-                if(payments[i].getShortDescription().compareTo(payments[i + 1].getShortDescription()) > 0){
57
+                if(comparator.compare(payments[i], payments[i + 1]) > 0){
31
                     isNotSorted = true;
58
                     isNotSorted = true;
32
                     Payment temp = payments[i];
59
                     Payment temp = payments[i];
33
                     payments[i] = payments[i + 1];
60
                     payments[i] = payments[i + 1];
36
                 }
63
                 }
37
             }
64
             }
38
         }
65
         }
39
-        return payments;
66
+
40
     }
67
     }
41
 
68
 
69
+//
70
+//    public Payment[] orderByPayerName(Payment[] payments){
71
+//        boolean isNotSorted = true;
72
+//
73
+//        while(isNotSorted){
74
+//            isNotSorted = false;
75
+//            for(int i = 0; i < payments.length - 1; i++){
76
+//                if(payments[i].getPayerName().compareTo(payments[i + 1].getPayerName()) > 0){
77
+//                    isNotSorted = true;
78
+//                    Payment temp = payments[i];
79
+//                    payments[i] = payments[i + 1];
80
+//                    payments[i+1] = temp;
81
+//
82
+//                }
83
+//            }
84
+//        }
85
+//        return payments;
86
+//    }
87
+
88
+//    public Payment[] orderById(Payment[] payments){
89
+//        boolean isNotSorted = true;
90
+//
91
+//        while(isNotSorted){
92
+//            isNotSorted = false;
93
+//            for(int i = 0; i < payments.length - 1; i++){
94
+//                if(payments[i].getId() > payments[i + 1].getId()){
95
+//                    isNotSorted = true;
96
+//                    Payment temp = payments[i];
97
+//                    payments[i] = payments[i + 1];
98
+//                    payments[i+1] = temp;
99
+//
100
+//                }
101
+//            }
102
+//        }
103
+//        return payments;
104
+//    }
105
+
42
 
106
 
43
 
107
 
44
 
108
 

+ 0
- 5
src/main/java/com/zipcoder/payment/PaymentSort.java 查看文件

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

+ 22
- 0
src/main/java/com/zipcoder/paymentSort/ById.java 查看文件

1
+package com.zipcoder.paymentSort;
2
+
3
+import com.zipcoder.payment.Payment;
4
+
5
+import java.util.Comparator;
6
+
7
+public class ById implements Comparator<Payment> {
8
+
9
+    public int compare(Payment o1, Payment o2) {
10
+        String id1 = Long.toString(o1.getId());
11
+        String id2 = Long.toString(o2.getId());
12
+
13
+        if(id1.compareTo(id2) > 0){
14
+            return 1;
15
+        } else if(o1.getId() < 0){
16
+            return -1;
17
+        } else {
18
+            return 0;
19
+        }
20
+    }
21
+
22
+}

src/main/java/com/zipcoder/payment/PaymentSortByPayer.java → src/main/java/com/zipcoder/paymentSort/ByPayer.java 查看文件

1
-package com.zipcoder.payment;
1
+package com.zipcoder.paymentSort;
2
+
3
+import com.zipcoder.payment.Payment;
2
 
4
 
3
 import java.util.Comparator;
5
 import java.util.Comparator;
4
 
6
 
5
-public class PaymentSortByPayer implements Comparator<Payment> {
7
+public class ByPayer implements Comparator<Payment> {
6
 
8
 
7
     public int compare(Payment o1, Payment o2) {
9
     public int compare(Payment o1, Payment o2) {
8
         if(o1.getPayerName().compareTo(o2.getPayerName()) > 0){
10
         if(o1.getPayerName().compareTo(o2.getPayerName()) > 0){
13
             return 0;
15
             return 0;
14
         }
16
         }
15
     }
17
     }
18
+
16
 }
19
 }

+ 22
- 0
src/main/java/com/zipcoder/paymentSort/ByShortDescription.java 查看文件

1
+package com.zipcoder.paymentSort;
2
+
3
+import com.zipcoder.payment.Payment;
4
+
5
+import java.util.Comparator;
6
+
7
+public class ByShortDescription implements Comparator<Payment> {
8
+
9
+    public int compare(Payment o1, Payment o2) {
10
+        String str1 = o1.getShortDescription();
11
+        String str2 = o2.getShortDescription();
12
+
13
+        if(str1.compareTo(str2) > 0){
14
+            return 1;
15
+        } else if(str1.compareTo(str2) < 0){
16
+            return -1;
17
+        } else {
18
+            return 0;
19
+        }
20
+    }
21
+
22
+}

+ 5
- 0
src/main/java/com/zipcoder/paymentSort/PaymentOrder.java 查看文件

1
+package com.zipcoder.paymentSort;
2
+
3
+public enum PaymentOrder {
4
+    ID, PAYERNAME, SHORTDESCRIPTION
5
+}

src/test/java/com/zipcoder/payment/PaymentSortByPayerTest.java → src/test/java/com/zipcoder/payment/PaymentOrderByPayerTest.java 查看文件

1
 package com.zipcoder.payment;
1
 package com.zipcoder.payment;
2
 
2
 
3
+import com.zipcoder.paymentSort.ByPayer;
3
 import org.junit.jupiter.api.Test;
4
 import org.junit.jupiter.api.Test;
4
 
5
 
5
 import static org.junit.jupiter.api.Assertions.*;
6
 import static org.junit.jupiter.api.Assertions.*;
6
 
7
 
7
-class PaymentSortByPayerTest {
8
+class PaymentOrderByPayerTest {
8
 
9
 
9
     @Test
10
     @Test
10
     void compairTest1() {
11
     void compairTest1() {
11
         //When
12
         //When
12
-        PaymentSortByPayer ps = new PaymentSortByPayer();
13
+        ByPayer ps = new ByPayer();
13
         Payment cc = new CreditCard(12, 4111131,"Jimmy Maidanos", 4, 11);
14
         Payment cc = new CreditCard(12, 4111131,"Jimmy Maidanos", 4, 11);
14
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
15
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
15
 
16
 
25
     @Test
26
     @Test
26
     void compairTest2() {
27
     void compairTest2() {
27
         //When
28
         //When
28
-        PaymentSortByPayer ps = new PaymentSortByPayer();
29
+        ByPayer ps = new ByPayer();
29
         Payment cc = new CreditCard(12, 4111131,"Zimmy Maidanos", 4, 11);
30
         Payment cc = new CreditCard(12, 4111131,"Zimmy Maidanos", 4, 11);
30
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
31
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
31
 
32
 
41
     @Test
42
     @Test
42
     void compairTest3() {
43
     void compairTest3() {
43
         //When
44
         //When
44
-        PaymentSortByPayer ps = new PaymentSortByPayer();
45
+        ByPayer ps = new ByPayer();
45
         Payment cc = new CreditCard(12, 4111131,"Nicholas Maidanos", 4, 11);
46
         Payment cc = new CreditCard(12, 4111131,"Nicholas Maidanos", 4, 11);
46
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
47
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
47
 
48
 

+ 49
- 2
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java 查看文件

1
 package com.zipcoder.payment;
1
 package com.zipcoder.payment;
2
 
2
 
3
+import com.zipcoder.paymentSort.PaymentOrder;
3
 import org.junit.Test;
4
 import org.junit.Test;
4
 
5
 
5
 import static org.junit.Assert.*;
6
 import static org.junit.Assert.*;
29
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
30
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
30
         Payment ck = new Check(46,"Nicholas Maidanos", "8934", "3343234");
31
         Payment ck = new Check(46,"Nicholas Maidanos", "8934", "3343234");
31
 
32
 
32
-        PaymentPresenter paymentp = new PaymentPresenter(PaymentSort.SHORTDESCRIPTION);
33
+        PaymentPresenter paymentPresenter = new PaymentPresenter();
34
+        paymentPresenter.setOrderBy(PaymentOrder.SHORTDESCRIPTION);
33
 
35
 
34
         payments[0] = pp;
36
         payments[0] = pp;
35
         payments[1] = cc;
37
         payments[1] = cc;
38
         String expected = "CC Nicholas Maidanos 1131 4/11\nCheck Nicholas Maidanos ****3234\nPayPal Nicholas Maidanos nmaidanos@gmail.com\n";
40
         String expected = "CC Nicholas Maidanos 1131 4/11\nCheck Nicholas Maidanos ****3234\nPayPal Nicholas Maidanos nmaidanos@gmail.com\n";
39
 
41
 
40
         //Actual
42
         //Actual
41
-        String actual = paymentp.toString(payments);
43
+        String actual = paymentPresenter.toString(payments);
44
+
45
+        assertEquals(expected, actual);
46
+    }
47
+
48
+    @Test
49
+    public void SortByPayerId() {
50
+        //When
51
+        Payment[] payments = new Payment[3];
52
+        Payment cc = new CreditCard(3, 4111131,"Nicholas Maidanos", 4, 11);
53
+        Payment pp = new Paypal(2, "Nicholas Maidanos", "nmaidanos@gmail.com");
54
+        Payment ck = new Check(1,"Nicholas Maidanos", "8934", "3343234");
55
+
56
+        PaymentPresenter paymentPresenter = new PaymentPresenter();
57
+        paymentPresenter.setOrderBy(PaymentOrder.ID);
58
+
59
+        payments[0] = pp;
60
+        payments[1] = cc;
61
+        payments[2] = ck;
62
+        //Expect
63
+        String expected = "Check Nicholas Maidanos ****3234\nPayPal Nicholas Maidanos nmaidanos@gmail.com\nCC Nicholas Maidanos 1131 4/11\n";
64
+
65
+        //Actual
66
+        String actual = paymentPresenter.toString(payments);
67
+
68
+        assertEquals(expected, actual);
69
+    }
70
+    @Test
71
+    public void SortByPayerName() {
72
+        //When
73
+        Payment[] payments = new Payment[3];
74
+        Payment cc = new CreditCard(3, 4111131,"Jimmy Maidanos", 4, 11);
75
+        Payment pp = new Paypal(2, "Billy Maidanos", "nmaidanos@gmail.com");
76
+        Payment ck = new Check(1,"Nicholas Maidanos", "8934", "3343234");
77
+
78
+        PaymentPresenter paymentPresenter = new PaymentPresenter();
79
+        paymentPresenter.setOrderBy(PaymentOrder.PAYERNAME);
80
+
81
+        payments[0] = cc;
82
+        payments[1] = ck;
83
+        payments[2] = pp;
84
+        //Expect
85
+        String expected = "PayPal Billy Maidanos nmaidanos@gmail.com\nCC Jimmy Maidanos 1131 4/11\nCheck Nicholas Maidanos ****3234\n";
86
+
87
+        //Actual
88
+        String actual = paymentPresenter.toString(payments);
42
 
89
 
43
         assertEquals(expected, actual);
90
         assertEquals(expected, actual);
44
     }
91
     }