瀏覽代碼

Completed

Lauren Green 7 年之前
父節點
當前提交
1da8e142f1

+ 42
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java 查看文件

@@ -2,5 +2,47 @@ package com.zipcoder.payment;
2 2
 
3 3
 public class PaymentPresenter {
4 4
 
5
+    PaymentSortByPayer sorter = new PaymentSortByPayer();
5 6
 
7
+    public String toString(Payment[] payments) {
8
+
9
+        String output = "";
10
+
11
+        if(payments.length > 0) {
12
+            payments = sorter.compareArray(payments);
13
+            for(Payment p: payments) {
14
+                output += p.getShortDescription() + "\n";
15
+            }
16
+        }
17
+
18
+        return output.trim();
19
+    }
20
+
21
+    public String toStringByPayerName(Payment[] payments) {
22
+
23
+        String output = "";
24
+
25
+        if(payments.length > 0) {
26
+            payments = sorter.compareArrayByName(payments);
27
+            for(Payment p: payments) {
28
+                output += p.getShortDescription() + "\n";
29
+            }
30
+        }
31
+
32
+        return output.trim();
33
+    }
34
+
35
+    public String toStringById(Payment[] payments) {
36
+
37
+        String output = "";
38
+
39
+        if(payments.length > 0) {
40
+            payments = sorter.compareArrayById(payments);
41
+            for(Payment p: payments) {
42
+                output += p.getShortDescription() + "\n";
43
+            }
44
+        }
45
+
46
+        return output.trim();
47
+    }
6 48
 }

+ 57
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java 查看文件

@@ -0,0 +1,57 @@
1
+package com.zipcoder.payment;
2
+
3
+import java.util.Comparator;
4
+
5
+public class PaymentSortByPayer implements Comparator<Payment> {
6
+    @Override
7
+    public int compare(Payment o1, Payment o2) {
8
+        return o1.compareTo(o2);
9
+    }
10
+
11
+    public int compareByName(Payment o1, Payment o2) {
12
+        return o1.getPayerName().compareTo(o2.getPayerName());
13
+    }
14
+
15
+    public int compareById(Payment o1, Payment o2) {
16
+        return o1.getId().compareTo(o2.getId());
17
+    }
18
+
19
+    public Payment[] compareArray(Payment[] payments) {
20
+        for (int i = 0; i < payments.length - 1; i++) {
21
+            for (int j = 0; j < payments.length - 1; j++) {
22
+                if(compare(payments[j], payments[j+1]) > 0) {
23
+                    Payment temp = payments[j];
24
+                    payments[j] = payments[j+1];
25
+                    payments[j+1] = temp;
26
+                }
27
+            }
28
+        }
29
+        return payments;
30
+    }
31
+
32
+    public Payment[] compareArrayByName(Payment[] payments) {
33
+        for (int i = 0; i < payments.length - 1; i++) {
34
+            for (int j = 0; j < payments.length - 1; j++) {
35
+                if(compareByName(payments[j], payments[j+1]) > 0) {
36
+                    Payment temp = payments[j];
37
+                    payments[j] = payments[j+1];
38
+                    payments[j+1] = temp;
39
+                }
40
+            }
41
+        }
42
+        return payments;
43
+    }
44
+
45
+    public Payment[] compareArrayById(Payment[] payments) {
46
+        for (int i = 0; i < payments.length - 1; i++) {
47
+            for (int j = 0; j < payments.length - 1; j++) {
48
+                if(compareById(payments[j], payments[j+1]) > 0) {
49
+                    Payment temp = payments[j];
50
+                    payments[j] = payments[j+1];
51
+                    payments[j+1] = temp;
52
+                }
53
+            }
54
+        }
55
+        return payments;
56
+    }
57
+}

+ 41
- 0
src/test/java/com/zipcoder/payment/CheckTest.java 查看文件

@@ -68,4 +68,45 @@ public class CheckTest {
68 68
         Assert.assertEquals(expected, actual);
69 69
 
70 70
     }
71
+
72
+    @Test
73
+    public void testComparable() {
74
+        //When
75
+        int expected = 0;
76
+        int actual = check2.compareTo(check2);
77
+
78
+        //Then
79
+        Assert.assertEquals(expected, actual);
80
+
81
+    }
82
+
83
+    @Test
84
+    public void testComparable2() {
85
+        //When
86
+        boolean actual = false;
87
+        int compare = 0;
88
+        int result = check.compareTo(check2);
89
+        if(result > compare) {
90
+            actual = true;
91
+        }
92
+
93
+        //Then
94
+        Assert.assertTrue(actual);
95
+
96
+    }
97
+
98
+    @Test
99
+    public void testComparable3() {
100
+        //When
101
+        boolean actual = false;
102
+        int compare = 0;
103
+        int result = check2.compareTo(check);
104
+        if(result < compare) {
105
+            actual = true;
106
+        }
107
+
108
+        //Then
109
+        Assert.assertTrue(actual);
110
+
111
+    }
71 112
 }

+ 41
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java 查看文件

@@ -81,4 +81,45 @@ public class CreditCardTest {
81 81
 
82 82
     }
83 83
 
84
+    @Test
85
+    public void testComparable() {
86
+        //When
87
+        int expected = 0;
88
+        int actual = creditCard2.compareTo(creditCard2);
89
+
90
+        //Then
91
+        Assert.assertEquals(expected, actual);
92
+
93
+    }
94
+
95
+    @Test
96
+    public void testComparable2() {
97
+        //When
98
+        boolean actual = false;
99
+        int compare = 0;
100
+        int result = creditCard.compareTo(creditCard2);
101
+        if(result > compare) {
102
+            actual = true;
103
+        }
104
+
105
+        //Then
106
+        Assert.assertTrue(actual);
107
+
108
+    }
109
+
110
+    @Test
111
+    public void testComparable3() {
112
+        //When
113
+        boolean actual = false;
114
+        int compare = 0;
115
+        int result = creditCard2.compareTo(creditCard);
116
+        if(result < compare) {
117
+            actual = true;
118
+        }
119
+
120
+        //Then
121
+        Assert.assertTrue(actual);
122
+
123
+    }
124
+
84 125
 }

+ 41
- 0
src/test/java/com/zipcoder/payment/PayPalTest.java 查看文件

@@ -57,4 +57,45 @@ public class PayPalTest {
57 57
 
58 58
     }
59 59
 
60
+    @Test
61
+    public void testComparable() {
62
+        //When
63
+        int expected = 0;
64
+        int actual = customer2.compareTo(customer2);
65
+
66
+        //Then
67
+        Assert.assertEquals(expected, actual);
68
+
69
+    }
70
+
71
+    @Test
72
+    public void testComparable2() {
73
+        //When
74
+        boolean actual = false;
75
+        int compare = 0;
76
+        int result = customer.compareTo(customer2);
77
+        if(result > compare) {
78
+            actual = true;
79
+        }
80
+
81
+        //Then
82
+        Assert.assertTrue(actual);
83
+
84
+    }
85
+
86
+    @Test
87
+    public void testComparable3() {
88
+        //When
89
+        boolean actual = false;
90
+        int compare = 0;
91
+        int result = customer2.compareTo(customer);
92
+        if(result < compare) {
93
+            actual = true;
94
+        }
95
+
96
+        //Then
97
+        Assert.assertTrue(actual);
98
+
99
+    }
100
+
60 101
 }

+ 119
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java 查看文件

@@ -1,4 +1,123 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
3 6
 public class PaymentPresenterTest {
7
+
8
+    Payment paypal = new PayPal(4L, "Tia Mowry", "tia@mowry.com");
9
+    Payment check = new Check(81L, "Abigail Henry", "11432543", "134344551");
10
+    Payment creditCard = new CreditCard(15L, "Danny Tanner", "45454545", 12, 2099);
11
+
12
+    @Test
13
+    public void testNoPayment() {
14
+        Payment[] payments = new Payment[0];
15
+        PaymentPresenter presenter = new PaymentPresenter();
16
+        String expected = "";
17
+
18
+        String actual = presenter.toString(payments);
19
+
20
+        Assert.assertEquals(expected, actual);
21
+    }
22
+
23
+    @Test
24
+    public void testPaymentsNoSort() {
25
+        Payment[] payments = {paypal, check, creditCard};
26
+        PaymentPresenter presenter = new PaymentPresenter();
27
+        String expected = "CC Danny Tanner 4545 12/2099\n" +
28
+                "Check Abigail Henry ***4551\n" +
29
+                "PayPal Tia Mowry tia@mowry.com";
30
+
31
+        String actual = presenter.toString(payments);
32
+
33
+        Assert.assertEquals(expected, actual);
34
+    }
35
+
36
+    @Test
37
+    public void testPaymentsSort() {
38
+        Payment[] payments = {creditCard, check, paypal};
39
+        PaymentPresenter presenter = new PaymentPresenter();
40
+        String expected = "CC Danny Tanner 4545 12/2099\n" +
41
+                "Check Abigail Henry ***4551\n" +
42
+                "PayPal Tia Mowry tia@mowry.com";
43
+
44
+        String actual = presenter.toString(payments);
45
+
46
+        Assert.assertEquals(expected, actual);
47
+    }
48
+
49
+    @Test
50
+    public void testNoPaymentByName() {
51
+        Payment[] payments = new Payment[0];
52
+        PaymentPresenter presenter = new PaymentPresenter();
53
+        String expected = "";
54
+
55
+        String actual = presenter.toStringByPayerName(payments);
56
+
57
+        Assert.assertEquals(expected, actual);
58
+    }
59
+
60
+    @Test
61
+    public void testPaymentsByNameNoSort() {
62
+        Payment[] payments = {check, creditCard, paypal};
63
+        PaymentPresenter presenter = new PaymentPresenter();
64
+        String expected = "Check Abigail Henry ***4551\n" +
65
+                "CC Danny Tanner 4545 12/2099\n" +
66
+                "PayPal Tia Mowry tia@mowry.com";
67
+
68
+        String actual = presenter.toStringByPayerName(payments);
69
+
70
+        Assert.assertEquals(expected, actual);
71
+    }
72
+
73
+    @Test
74
+    public void testPaymentsByNameSort() {
75
+        Payment[] payments = {creditCard, check, paypal};
76
+        PaymentPresenter presenter = new PaymentPresenter();
77
+        String expected = "Check Abigail Henry ***4551\n" +
78
+                "CC Danny Tanner 4545 12/2099\n" +
79
+                "PayPal Tia Mowry tia@mowry.com";
80
+
81
+        String actual = presenter.toStringByPayerName(payments);
82
+
83
+        Assert.assertEquals(expected, actual);
84
+    }
85
+
86
+    @Test
87
+    public void testNoPaymentById() {
88
+        Payment[] payments = new Payment[0];
89
+        PaymentPresenter presenter = new PaymentPresenter();
90
+        String expected = "";
91
+
92
+        String actual = presenter.toStringById(payments);
93
+
94
+        Assert.assertEquals(expected, actual);
95
+    }
96
+
97
+    @Test
98
+    public void testPaymentsByIdNoSort() {
99
+        Payment[] payments = {paypal, creditCard, check};
100
+        PaymentPresenter presenter = new PaymentPresenter();
101
+        String expected = "PayPal Tia Mowry tia@mowry.com\n" +
102
+                "CC Danny Tanner 4545 12/2099\n" +
103
+                "Check Abigail Henry ***4551";
104
+
105
+        String actual = presenter.toStringById(payments);
106
+
107
+        Assert.assertEquals(expected, actual);
108
+    }
109
+
110
+    @Test
111
+    public void testPaymentsByIdSort() {
112
+        Payment[] payments = {creditCard, check, paypal};
113
+        PaymentPresenter presenter = new PaymentPresenter();
114
+        String expected = "PayPal Tia Mowry tia@mowry.com\n" +
115
+                "CC Danny Tanner 4545 12/2099\n" +
116
+                "Check Abigail Henry ***4551";
117
+
118
+        String actual = presenter.toStringById(payments);
119
+
120
+        Assert.assertEquals(expected, actual);
121
+    }
122
+
4 123
 }

+ 51
- 0
src/test/java/com/zipcoder/payment/PaymentSortByPayerTest.java 查看文件

@@ -0,0 +1,51 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class PaymentSortByPayerTest {
7
+
8
+    Payment payment1 = new Check(4L, "Bob Smith", "456789", "12345");
9
+    Payment payment2 = new PayPal(5L,"Kate Flynn", "kate@mail.com");
10
+
11
+    @Test
12
+    public void testComparable() {
13
+        //When
14
+        int expected = 0;
15
+        int actual = payment1.compareTo(payment1);
16
+
17
+        //Then
18
+        Assert.assertEquals(expected, actual);
19
+
20
+    }
21
+
22
+    @Test
23
+    public void testComparable2() {
24
+        //When
25
+        boolean actual = false;
26
+        int compare = 0;
27
+        int result = payment2.compareTo(payment1);
28
+        if(result > compare) {
29
+            actual = true;
30
+        }
31
+
32
+        //Then
33
+        Assert.assertTrue(actual);
34
+
35
+    }
36
+
37
+    @Test
38
+    public void testComparable3() {
39
+        //When
40
+        boolean actual = false;
41
+        int compare = 0;
42
+        int result = payment1.compareTo(payment2);
43
+        if(result < compare) {
44
+            actual = true;
45
+        }
46
+
47
+        //Then
48
+        Assert.assertTrue(actual);
49
+
50
+    }
51
+}