Przeglądaj źródła

INTERFACECOMPARABLE

jpsp91 8 lat temu
rodzic
commit
393813c4d9

+ 18
- 4
src/main/java/com/zipcoder/payment/Check.java Wyświetl plik

@@ -7,7 +7,15 @@ public class Check implements Payment {
7 7
 
8 8
     String payerName;
9 9
 
10
-    String shortDescription;
10
+
11
+    public Check(Long id, String payerName, String accountNumber){
12
+        this.id = id;
13
+        this.payerName = payerName;
14
+        this.accountNumber = accountNumber;
15
+    }
16
+
17
+
18
+
11 19
 
12 20
     public String getRoutingNumber() {
13 21
         return routingNumber;
@@ -56,13 +64,19 @@ public class Check implements Payment {
56 64
     public String getShortDescription() {
57 65
 
58 66
         String accountNumb = getAccountNumber();
59
-        String lastFourAcct = accountNumb.substring(accountNumb.length()-4, accountNumb.length());
67
+        String lastFourAcct = accountNumb.substring(accountNumb.length()-4);
60 68
 
61
-        return "Check " + getPayerName() + " ***" + lastFourAcct;
69
+        return "Check " + this.payerName + " ***" + lastFourAcct + "\n";
62 70
     }
63 71
 
64 72
 
65 73
     public int compareTo(Payment o) {
66
-        return 0;
74
+        if(this.getShortDescription().compareTo(o.getShortDescription()) > 0){
75
+            return 1;
76
+        } else if(this.getShortDescription().compareTo(o.getShortDescription()) < 0) {
77
+            return -1;
78
+        } else {
79
+            return 0;
80
+        }
67 81
     }
68 82
 }

+ 1
- 5
src/main/java/com/zipcoder/payment/CreditCard.java Wyświetl plik

@@ -100,7 +100,7 @@ public class CreditCard implements Payment {
100 100
         String numb = getNumber();
101 101
         String lastFour = getNumber().substring(numb.length()-4, numb.length());
102 102
 
103
-        return "CC " + getPayerName() + " " + lastFour + " " + getExpiredMonth() + "/" + getExpiredYear();
103
+        return "CC " + this.payerName + " " + lastFour + " " + this.expiredMonth + "/" + this.expiredYear + "\n";
104 104
     }
105 105
 
106 106
 
@@ -115,8 +115,4 @@ public class CreditCard implements Payment {
115 115
             return 0;
116 116
         }
117 117
     }
118
-
119
-
120
-
121
-
122 118
 }

+ 22
- 3
src/main/java/com/zipcoder/payment/PayPal.java Wyświetl plik

@@ -8,7 +8,12 @@ public class PayPal implements Payment{
8 8
 
9 9
     String email;
10 10
 
11
-    String shortDescription;
11
+
12
+
13
+    public static void main(String[] args){
14
+        int var = "Paypal Tia Mowry tia@mowry.com".compareTo("Check Tia Mowry ***4551");
15
+        System.out.println(var);
16
+    }
12 17
 
13 18
     public Long getId() {
14 19
         return id;
@@ -35,13 +40,27 @@ public class PayPal implements Payment{
35 40
     }
36 41
 
37 42
 
43
+    public PayPal(Long id, String payerName, String email){
44
+        this.id = id;
45
+        this.payerName = payerName;
46
+        this.email = email;
47
+    }
48
+
49
+
50
+
38 51
 
39 52
     public String getShortDescription(){
40
-        return "PayPal " + getPayerName() + " " + getEmail();
53
+        return "PayPal " + this.payerName + " " + this.email + "\n";
41 54
     }
42 55
 
43 56
 
44 57
     public int compareTo(Payment o) {
45
-        return 0;
58
+        if(this.getShortDescription().compareTo(o.getShortDescription()) > 0){
59
+            return 1;
60
+        } else if(this.getShortDescription().compareTo(o.getShortDescription()) < 0) {
61
+            return -1;
62
+        } else {
63
+            return 0;
64
+        }
46 65
     }
47 66
 }

+ 44
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Wyświetl plik

@@ -1,6 +1,50 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 public class PaymentPresenter {
4 6
 
5 7
 
8
+
9
+    public String toStringPayer(Payment[] payments) {
10
+
11
+        Arrays.sort(payments, new PaymentSortByPayer());
12
+
13
+        StringBuilder sb = new StringBuilder();
14
+
15
+        for (Payment payment:payments) {
16
+            sb.append(payment.getShortDescription());
17
+        }
18
+
19
+        return sb.toString();
20
+    }
21
+
22
+
23
+    public String toString(Payment[] payments) {
24
+
25
+        Arrays.sort(payments, new PaymentSortByShortDescription());
26
+
27
+        StringBuilder sb = new StringBuilder();
28
+
29
+        for (Payment payment:payments) {
30
+            sb.append(payment.getShortDescription());
31
+        }
32
+
33
+        return sb.toString();
34
+    }
35
+
36
+
37
+    public String toStringID(Payment[] payments) {
38
+
39
+        Arrays.sort(payments, new PaymentSortByID());
40
+
41
+        StringBuilder sb = new StringBuilder();
42
+
43
+        for (Payment payment:payments) {
44
+            sb.append(payment.getShortDescription());
45
+        }
46
+
47
+        return sb.toString();
48
+    }
49
+
6 50
 }

+ 17
- 0
src/main/java/com/zipcoder/payment/PaymentSortByID.java Wyświetl plik

@@ -0,0 +1,17 @@
1
+package com.zipcoder.payment;
2
+
3
+import java.util.Comparator;
4
+
5
+public class PaymentSortByID implements Comparator<Payment> {
6
+
7
+
8
+    public int compare(Payment o1, Payment o2) {
9
+        if (o1.getId().compareTo(o2.getId()) > 0){
10
+            return 1;
11
+        } else if(o1.getId().compareTo(o2.getId()) < 0) {
12
+            return -1;
13
+        } else {
14
+            return 0;
15
+        }
16
+    }
17
+}

+ 39
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java Wyświetl plik

@@ -0,0 +1,39 @@
1
+package com.zipcoder.payment;
2
+
3
+import java.util.Comparator;
4
+
5
+public class PaymentSortByPayer implements Comparator<Payment> {
6
+
7
+
8
+
9
+    public int compare(Payment o1, Payment o2) {
10
+        if (o1.getPayerName().compareTo(o2.getPayerName()) > 0){
11
+            return 1;
12
+        } else if(o1.getPayerName().compareTo(o2.getPayerName()) < 0) {
13
+            return -1;
14
+        } else {
15
+            return 0;
16
+        }
17
+    }
18
+
19
+//    public int compareShortDescription(Payment o1, Payment o2) {
20
+//        if (o1.getShortDescription().compareTo(o2.getShortDescription()) > 0){
21
+//            return 1;
22
+//        } else if(o1.getShortDescription().compareTo(o2.getShortDescription()) < 0) {
23
+//            return -1;
24
+//        } else {
25
+//            return 0;
26
+//        }
27
+//    }
28
+//
29
+//    public int compareID(Payment o1, Payment o2) {
30
+//        if (o1.getId().compareTo(o2.getId()) > 0){
31
+//            return 1;
32
+//        } else if(o1.getId().compareTo(o2.getId()) < 0) {
33
+//            return -1;
34
+//        } else {
35
+//            return 0;
36
+//        }
37
+//    }
38
+
39
+}

+ 17
- 0
src/main/java/com/zipcoder/payment/PaymentSortByShortDescription.java Wyświetl plik

@@ -0,0 +1,17 @@
1
+package com.zipcoder.payment;
2
+
3
+import java.util.Comparator;
4
+
5
+public class PaymentSortByShortDescription implements Comparator<Payment> {
6
+
7
+
8
+    public int compare(Payment o1, Payment o2) {
9
+        if (o1.getShortDescription().compareTo(o2.getShortDescription()) > 0){
10
+            return 1;
11
+        } else if(o1.getShortDescription().compareTo(o2.getShortDescription()) < 0) {
12
+            return -1;
13
+        } else {
14
+            return 0;
15
+        }
16
+    }
17
+}

+ 23
- 27
src/test/java/com/zipcoder/payment/CheckTest.java Wyświetl plik

@@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test;
5 5
 
6 6
 public class CheckTest {
7 7
 
8
-    Check check = new Check();
8
+    Check check = new Check(456L, "no", "123123");
9 9
 
10 10
 
11 11
     @Test
@@ -74,46 +74,42 @@ public class CheckTest {
74 74
     @Test
75 75
     public void getShortDescriptionTest(){
76 76
         //when
77
-        check.setPayerName("Bob");
77
+        check.setPayerName("Abed");
78 78
         check.setAccountNumber("0001234");
79 79
 
80 80
         //expected
81
-        String expected = "Check Bob ***1234";
81
+        String expected = "Check Abed ***1234\n";
82 82
 
83 83
         //actual
84 84
         String actual = check.getShortDescription();
85 85
 
86 86
         Assert.assertEquals(expected, actual);
87
-    //*********************************************************
88
-    }
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111 87
 
88
+    }
112 89
 
90
+    //*********************************************************
113 91
 
114 92
 
93
+    @Test
94
+    public void compareTo() {
95
+        //when
96
+        check.setPayerName("Abed");
97
+        check.setAccountNumber("0001234");
98
+        check.getShortDescription();
115 99
 
100
+        CreditCard cc = new CreditCard();
101
+        cc.setPayerName("Troy");
102
+        cc.setNumber("1110000");
103
+        cc.setExpiredMonth(12);
104
+        cc.setExpiredYear(2020);
105
+        cc.getShortDescription();
116 106
 
107
+        //expected
108
+        int expected = -1;
117 109
 
110
+        //actual
111
+        int actual = cc.compareTo(check);
118 112
 
113
+        Assert.assertEquals(expected,actual);
114
+    }
119 115
 }

+ 18
- 2
src/test/java/com/zipcoder/payment/CreditCardTest.java Wyświetl plik

@@ -95,13 +95,13 @@ public class CreditCardTest {
95 95
     @Test
96 96
     public void getShortDescriptionTest(){
97 97
         //when
98
-        cc.setPayerName("Bobby");
98
+        cc.setPayerName("Troy");
99 99
         cc.setNumber("1110000");
100 100
         cc.setExpiredMonth(12);
101 101
         cc.setExpiredYear(2020);
102 102
 
103 103
         //expected
104
-        String expected = "CC Bobby 0000 12/2020";
104
+        String expected = "CC Troy 0000 12/2020\n";
105 105
 
106 106
         //actual
107 107
         String actual = cc.getShortDescription();
@@ -110,6 +110,22 @@ public class CreditCardTest {
110 110
     }
111 111
 
112 112
 
113
+    @Test
114
+    public void compareTo() {
115
+        //when
116
+        cc.setPayerName("Troy");
117
+        cc.setNumber("1110000");
118
+        cc.setExpiredMonth(12);
119
+        cc.setExpiredYear(2020);
120
+        cc.getShortDescription();
113 121
 
122
+        //expected
123
+        int expected = 0;
114 124
 
125
+        //actual
126
+        int actual = cc.compareTo(cc);
127
+
128
+        Assert.assertEquals(expected,actual);
129
+
130
+    }
115 131
 }

+ 17
- 11
src/test/java/com/zipcoder/payment/PayPalTest.java Wyświetl plik

@@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test;
5 5
 
6 6
 public class PayPalTest {
7 7
 
8
-    PayPal pp = new PayPal();
8
+    PayPal pp = new PayPal(123L, "yes", "yesyes");
9 9
 
10 10
     @Test
11 11
     public void getIdTest() {
@@ -68,18 +68,24 @@ public class PayPalTest {
68 68
     }
69 69
 
70 70
 
71
+    @Test
72
+    public void compareTo() {
73
+        //when
74
+        pp.setPayerName("Tia Mowry");
75
+        pp.setEmail("tia@mowry.com");
76
+        pp.getShortDescription();
71 77
 
78
+        Check check = new Check(123L, "Tia Mowry", "123123");
79
+        check.setPayerName("Tia Mowry");
80
+        check.setAccountNumber("0001234");
81
+        check.getShortDescription();
72 82
 
83
+        //expected
84
+        int expected = 1;
73 85
 
86
+        //actual
87
+        int actual = pp.compareTo(check);
74 88
 
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
89
+        Assert.assertEquals(expected,actual);
90
+    }
85 91
 }

+ 68
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Wyświetl plik

@@ -1,4 +1,72 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.jupiter.api.Test;
5
+
3 6
 public class PaymentPresenterTest {
7
+
8
+        @Test
9
+        public void noPaymentTest() {
10
+        Payment[] payments = new Payment[0];
11
+
12
+        PaymentPresenter presenter = new PaymentPresenter();
13
+        String expected = "";
14
+
15
+        String actual = presenter.toString(payments);
16
+
17
+        Assert.assertEquals(expected, actual);
18
+}
19
+
20
+    @Test
21
+    public void multiplePaymentTest(){
22
+        Payment[] payments = new Payment[2];
23
+        Payment paypal = new PayPal(4L, "Tia Mowry", "tia@mowry.com");
24
+        Payment check = new Check(81L, "Tia Mowry", "134344551");
25
+
26
+        payments[0] = paypal;
27
+        payments[1] = check;
28
+
29
+        PaymentPresenter presenter = new PaymentPresenter();
30
+        String expected = "Check Tia Mowry ***4551\nPayPal Tia Mowry tia@mowry.com\n";
31
+
32
+        String actual = presenter.toString(payments);
33
+        Assert.assertEquals(expected, actual);
34
+    }
35
+
36
+
37
+    @Test
38
+    public void multiplePaymentTestPayerName(){
39
+        Payment[] payments = new Payment[2];
40
+        Payment paypal = new PayPal(4L, "Tia Mowry", "tia@mowry.com");
41
+        Payment check = new Check(81L, "Alice Wonder", "134344551");
42
+
43
+        payments[0] = paypal;
44
+        payments[1] = check;
45
+
46
+        PaymentPresenter presenter = new PaymentPresenter();
47
+        String expected = "Check Alice Wonder ***4551\nPayPal Tia Mowry tia@mowry.com\n";
48
+
49
+        String actual = presenter.toStringPayer(payments);
50
+        Assert.assertEquals(expected, actual);
51
+    }
52
+
53
+    @Test
54
+    public void multiplePaymentTestID(){
55
+        Payment[] payments = new Payment[2];
56
+        Payment paypal = new PayPal(4L, "Tia Mowry", "tia@mowry.com");
57
+        Payment check = new Check(81L, "Tia Mowry", "134344551");
58
+
59
+        payments[0] = paypal;
60
+        payments[1] = check;
61
+
62
+        PaymentPresenter presenter = new PaymentPresenter();
63
+        String expected = "PayPal Tia Mowry tia@mowry.com\nCheck Tia Mowry ***4551\n";
64
+
65
+        String actual = presenter.toStringID(payments);
66
+        Assert.assertEquals(expected, actual);
67
+    }
68
+
69
+
70
+
71
+
4 72
 }

+ 96
- 0
src/test/java/com/zipcoder/payment/PaymentSortByPayerTest.java Wyświetl plik

@@ -0,0 +1,96 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.jupiter.api.Test;
6
+
7
+import java.util.Arrays;
8
+
9
+public class PaymentSortByPayerTest {
10
+
11
+    PaymentSortByPayer psbp;
12
+
13
+//    Payment payment1;
14
+//
15
+//    Payment payment2;
16
+
17
+
18
+//    @Before
19
+//    public void setup(){
20
+//        payment1 = new PayPal(123L, "Dexter", "dexter@gmail.com");
21
+//        payment2 = new Check(456L, "Mandark");
22
+//
23
+//    }
24
+
25
+//    @Test
26
+//    public void compareTest(){
27
+//        System.out.println(payment1.getShortDescription());
28
+//        System.out.println(payment2.getShortDescription());
29
+//        //expected
30
+//        int expected = -1;
31
+//
32
+//        //actual
33
+//        int actual = psbp.compare(payment1, payment2);
34
+//
35
+//        Assert.assertEquals(expected, actual);
36
+//
37
+//    }
38
+
39
+    @Test
40
+    public void compareTest2(){
41
+        //when
42
+        Check check = new Check(123L,"Dexter", "123123");
43
+        Check check2 = new Check(456L, "Mandark", "456456");
44
+
45
+        //expected
46
+        int expected = -1;
47
+
48
+        //actual
49
+        //int actual = check.getPayerName().compareTo(check2.getPayerName());
50
+
51
+        int actual = check.compareTo(check2);
52
+
53
+        Assert.assertEquals(expected, actual);
54
+
55
+    }
56
+
57
+    @Test
58
+    public void compareTest3(){
59
+        //when
60
+        Check check = new Check(123L,"Dexter", "123123");
61
+        Check check2 = new Check(456L, "Mandark", "456456");
62
+
63
+        //expected
64
+        int expected = 1;
65
+
66
+        //actual
67
+        int actual = check2.compareTo(check);
68
+
69
+        Assert.assertEquals(expected, actual);
70
+
71
+    }
72
+
73
+
74
+    @Test
75
+    public void compareTest4(){
76
+        //when
77
+        Check check = new Check(123L,"Dexter", "123123");
78
+        //Check check2 = new Check(456L, "Mandark", "456456");
79
+
80
+        //expected
81
+        int expected = 0;
82
+
83
+        //actual
84
+        int actual = check.compareTo(check);
85
+
86
+        Assert.assertEquals(expected, actual);
87
+
88
+
89
+    }
90
+
91
+
92
+
93
+
94
+
95
+
96
+}