ソースを参照

Finished and refactored

Nicholas Maidanos 8 年 前
コミット
b268f516a3

src/main/java/com/zipcoder/payment/PaymentPresenter.java → src/main/java/com/zipcoder/PaymentPresenter.java ファイルの表示

1
-package com.zipcoder.payment;
1
+package com.zipcoder;
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;
3
+import com.zipcoder.payment.Payment;
4
+import com.zipcoder.payment.Comparators.Id;
5
+import com.zipcoder.payment.Comparators.Payer;
6
+import com.zipcoder.payment.Comparators.PaymentOrder;
7
+import com.zipcoder.payment.Comparators.ShortDescription;
7
 
8
 
8
 import java.util.Comparator;
9
 import java.util.Comparator;
9
 
10
 
22
 
23
 
23
         for(int i = 0; i < payments.length; i++){
24
         for(int i = 0; i < payments.length; i++){
24
             sb.append(payments[i].getShortDescription());
25
             sb.append(payments[i].getShortDescription());
25
-            sb.append("\n");
26
         }
26
         }
27
 
27
 
28
         return sb.toString();
28
         return sb.toString();
35
     public void orderBy(Payment[] payments){
35
     public void orderBy(Payment[] payments){
36
         switch(this.order){
36
         switch(this.order){
37
             case SHORTDESCRIPTION:
37
             case SHORTDESCRIPTION:
38
-                this.bubbleSort(payments, new ByShortDescription());
38
+                this.bubbleSort(payments, new ShortDescription());
39
                 break;
39
                 break;
40
             case PAYERNAME:
40
             case PAYERNAME:
41
-                this.bubbleSort(payments, new ByPayer());
41
+                this.bubbleSort(payments, new Payer());
42
                 break;
42
                 break;
43
             case ID:
43
             case ID:
44
-                this.bubbleSort(payments, new ById());
44
+                this.bubbleSort(payments, new Id());
45
                 break;
45
                 break;
46
             default:
46
             default:
47
                 break;
47
                 break;

+ 9
- 1
src/main/java/com/zipcoder/payment/Check.java ファイルの表示

63
     }
63
     }
64
 
64
 
65
     public int compareTo(Payment o) {
65
     public int compareTo(Payment o) {
66
-        return 0;
66
+        int result = this.getShortDescription().compareTo(o.getShortDescription());
67
+        if(result > 0){
68
+            return 1;
69
+        } else if(result < 0){
70
+            return -1;
71
+        } else {
72
+            return 0;
73
+        }
67
     }
74
     }
75
+
68
 }
76
 }

src/main/java/com/zipcoder/paymentSort/ById.java → src/main/java/com/zipcoder/payment/Comparators/Id.java ファイルの表示

1
-package com.zipcoder.paymentSort;
1
+package com.zipcoder.payment.Comparators;
2
 
2
 
3
 import com.zipcoder.payment.Payment;
3
 import com.zipcoder.payment.Payment;
4
 
4
 
5
 import java.util.Comparator;
5
 import java.util.Comparator;
6
 
6
 
7
-public class ById implements Comparator<Payment> {
7
+public class Id implements Comparator<Payment> {
8
 
8
 
9
     public int compare(Payment o1, Payment o2) {
9
     public int compare(Payment o1, Payment o2) {
10
         String id1 = Long.toString(o1.getId());
10
         String id1 = Long.toString(o1.getId());

src/main/java/com/zipcoder/paymentSort/ByPayer.java → src/main/java/com/zipcoder/payment/Comparators/Payer.java ファイルの表示

1
-package com.zipcoder.paymentSort;
1
+package com.zipcoder.payment.Comparators;
2
 
2
 
3
 import com.zipcoder.payment.Payment;
3
 import com.zipcoder.payment.Payment;
4
 
4
 
5
 import java.util.Comparator;
5
 import java.util.Comparator;
6
 
6
 
7
-public class ByPayer implements Comparator<Payment> {
7
+public class Payer implements Comparator<Payment> {
8
 
8
 
9
     public int compare(Payment o1, Payment o2) {
9
     public int compare(Payment o1, Payment o2) {
10
         if(o1.getPayerName().compareTo(o2.getPayerName()) > 0){
10
         if(o1.getPayerName().compareTo(o2.getPayerName()) > 0){

src/main/java/com/zipcoder/paymentSort/PaymentOrder.java → src/main/java/com/zipcoder/payment/Comparators/PaymentOrder.java ファイルの表示

1
-package com.zipcoder.paymentSort;
1
+package com.zipcoder.payment.Comparators;
2
 
2
 
3
 public enum PaymentOrder {
3
 public enum PaymentOrder {
4
     ID, PAYERNAME, SHORTDESCRIPTION
4
     ID, PAYERNAME, SHORTDESCRIPTION

src/main/java/com/zipcoder/paymentSort/ByShortDescription.java → src/main/java/com/zipcoder/payment/Comparators/ShortDescription.java ファイルの表示

1
-package com.zipcoder.paymentSort;
1
+package com.zipcoder.payment.Comparators;
2
 
2
 
3
 import com.zipcoder.payment.Payment;
3
 import com.zipcoder.payment.Payment;
4
 
4
 
5
 import java.util.Comparator;
5
 import java.util.Comparator;
6
 
6
 
7
-public class ByShortDescription implements Comparator<Payment> {
7
+public class ShortDescription implements Comparator<Payment> {
8
 
8
 
9
     public int compare(Payment o1, Payment o2) {
9
     public int compare(Payment o1, Payment o2) {
10
         String str1 = o1.getShortDescription();
10
         String str1 = o1.getShortDescription();

+ 11
- 1
src/main/java/com/zipcoder/payment/Paypal.java ファイルの表示

44
     }
44
     }
45
 
45
 
46
     public int compareTo(Payment o) {
46
     public int compareTo(Payment o) {
47
-        return 0;
47
+
48
+        int result = this.getShortDescription().compareTo(o.getShortDescription());
49
+
50
+        if(result > 0){
51
+            return 1;
52
+        } else if(result < 0){
53
+            return -1;
54
+        } else {
55
+            return 0;
56
+        }
57
+
48
     }
58
     }
49
 }
59
 }

+ 13
- 10
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;
4
-import org.junit.jupiter.api.Test;
3
+import com.zipcoder.payment.Comparators.Payer;
5
 
4
 
6
-import static org.junit.jupiter.api.Assertions.*;
5
+import static org.junit.Assert.*;
7
 
6
 
8
-class PaymentOrderByPayerTest {
7
+import org.junit.Test;
8
+
9
+import java.util.Comparator;
10
+
11
+public class PaymentOrderByPayerTest {
9
 
12
 
10
     @Test
13
     @Test
11
-    void compairTest1() {
14
+    public void compairTest1() {
12
         //When
15
         //When
13
-        ByPayer ps = new ByPayer();
16
+        Comparator ps = new Payer();
14
         Payment cc = new CreditCard(12, 4111131,"Jimmy Maidanos", 4, 11);
17
         Payment cc = new CreditCard(12, 4111131,"Jimmy Maidanos", 4, 11);
15
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
18
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
16
 
19
 
24
     }
27
     }
25
 
28
 
26
     @Test
29
     @Test
27
-    void compairTest2() {
30
+    public void compairTest2() {
28
         //When
31
         //When
29
-        ByPayer ps = new ByPayer();
32
+        Comparator ps = new Payer();
30
         Payment cc = new CreditCard(12, 4111131,"Zimmy Maidanos", 4, 11);
33
         Payment cc = new CreditCard(12, 4111131,"Zimmy Maidanos", 4, 11);
31
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
34
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
32
 
35
 
40
     }
43
     }
41
 
44
 
42
     @Test
45
     @Test
43
-    void compairTest3() {
46
+    public void compairTest3() {
44
         //When
47
         //When
45
-        ByPayer ps = new ByPayer();
48
+        Comparator ps = new Payer();
46
         Payment cc = new CreditCard(12, 4111131,"Nicholas Maidanos", 4, 11);
49
         Payment cc = new CreditCard(12, 4111131,"Nicholas Maidanos", 4, 11);
47
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
50
         Payment pp = new Paypal(23, "Nicholas Maidanos", "nmaidanos@gmail.com");
48
 
51
 

+ 6
- 6
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;
4
-import org.junit.Test;
5
-
3
+import com.zipcoder.PaymentPresenter;
4
+import com.zipcoder.payment.Comparators.PaymentOrder;
6
 import static org.junit.Assert.*;
5
 import static org.junit.Assert.*;
6
+import org.junit.Test;
7
 
7
 
8
 public class PaymentPresenterTest {
8
 public class PaymentPresenterTest {
9
 
9
 
37
         payments[1] = cc;
37
         payments[1] = cc;
38
         payments[2] = ck;
38
         payments[2] = ck;
39
         //Expect
39
         //Expect
40
-        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";
41
 
41
 
42
         //Actual
42
         //Actual
43
         String actual = paymentPresenter.toString(payments);
43
         String actual = paymentPresenter.toString(payments);
60
         payments[1] = cc;
60
         payments[1] = cc;
61
         payments[2] = ck;
61
         payments[2] = ck;
62
         //Expect
62
         //Expect
63
-        String expected = "Check Nicholas Maidanos ****3234\nPayPal Nicholas Maidanos nmaidanos@gmail.com\nCC Nicholas Maidanos 1131 4/11\n";
63
+        String expected = "Check Nicholas Maidanos ****3234\nPaypal Nicholas Maidanos nmaidanos@gmail.com\nCC Nicholas Maidanos 1131 4/11\n";
64
 
64
 
65
         //Actual
65
         //Actual
66
         String actual = paymentPresenter.toString(payments);
66
         String actual = paymentPresenter.toString(payments);
82
         payments[1] = ck;
82
         payments[1] = ck;
83
         payments[2] = pp;
83
         payments[2] = pp;
84
         //Expect
84
         //Expect
85
-        String expected = "PayPal Billy Maidanos nmaidanos@gmail.com\nCC Jimmy Maidanos 1131 4/11\nCheck Nicholas Maidanos ****3234\n";
85
+        String expected = "Paypal Billy Maidanos nmaidanos@gmail.com\nCC Jimmy Maidanos 1131 4/11\nCheck Nicholas Maidanos ****3234\n";
86
 
86
 
87
         //Actual
87
         //Actual
88
         String actual = paymentPresenter.toString(payments);
88
         String actual = paymentPresenter.toString(payments);