Sfoglia il codice sorgente

did it, thanks Rachelle

Kris Blassingame 8 anni fa
parent
commit
3f4b40a07e

+ 66
- 0
src/main/java/com/zipcoder/payment/Check.java Vedi File

@@ -0,0 +1,66 @@
1
+package com.zipcoder.payment;
2
+
3
+public class Check implements Payment {
4
+
5
+    Long id;
6
+    String payerName;
7
+    String routingNumber;
8
+    String accountNumber;
9
+
10
+    public Check(String payerName, String routingNumber, String accountNumber) {
11
+        this.payerName = payerName;
12
+        this.routingNumber = routingNumber;
13
+        this.accountNumber = accountNumber;
14
+    }
15
+
16
+    public Check() {
17
+    }
18
+
19
+    @Override
20
+    public String getPayerName() {
21
+        return payerName;
22
+    }
23
+
24
+    public void setPayerName(String payerName) {
25
+        this.payerName = payerName;
26
+    }
27
+
28
+    @Override
29
+    public Long getId() {
30
+        return id;
31
+    }
32
+
33
+    public void setId(Long id) {
34
+        this.id = id;
35
+    }
36
+
37
+    public String getRoutingNumber() {
38
+        return routingNumber;
39
+    }
40
+
41
+    public void setRoutingNumber(String routingNumber) {
42
+        this.routingNumber = routingNumber;
43
+    }
44
+
45
+    @Override
46
+    public String getShortDescription() {
47
+        return "Check " + getPayerName() + " ***" + getLastFourDigits();
48
+    }
49
+
50
+    public String getLastFourDigits() {
51
+        return getAccountNumber().substring(accountNumber.length() - 4);
52
+    }
53
+
54
+    public String getAccountNumber() {
55
+        return accountNumber;
56
+    }
57
+
58
+    public void setAccountNumber(String accountNumber) {
59
+        this.accountNumber = accountNumber;
60
+    }
61
+
62
+    @Override
63
+    public int compareTo(Payment o) {
64
+      return this.getShortDescription().compareTo(o.getShortDescription());
65
+    }
66
+}

+ 9
- 3
src/main/java/com/zipcoder/payment/CreditCard.java Vedi File

@@ -3,7 +3,7 @@ package com.zipcoder.payment;
3 3
 public class CreditCard implements Payment {
4 4
 
5 5
 
6
-    long id;
6
+    Long id;
7 7
     String payerName;
8 8
     String number;
9 9
     int expiredMonth;
@@ -13,6 +13,8 @@ public class CreditCard implements Payment {
13 13
     }
14 14
 
15 15
 
16
+
17
+
16 18
     public CreditCard(String payerName, String number, int expiredMonth, int expiredYear) {
17 19
         this.payerName = payerName;
18 20
         this.number = number;
@@ -21,11 +23,11 @@ public class CreditCard implements Payment {
21 23
     }
22 24
 
23 25
     @Override
24
-    public long getId() {
26
+    public Long getId() {
25 27
         return id;
26 28
     }
27 29
 
28
-    public void setId(long id) {
30
+    public void setId(Long id) {
29 31
         this.id = id;
30 32
     }
31 33
 
@@ -73,4 +75,8 @@ public class CreditCard implements Payment {
73 75
     }
74 76
 
75 77
 
78
+    @Override
79
+    public int compareTo(Payment o) {
80
+        return this.getShortDescription().compareTo(o.getShortDescription());
81
+    }
76 82
 }

+ 53
- 0
src/main/java/com/zipcoder/payment/PayPal.java Vedi File

@@ -0,0 +1,53 @@
1
+package com.zipcoder.payment;
2
+
3
+public class PayPal implements Payment {
4
+
5
+    Long id;
6
+    String payerName;
7
+    String email;
8
+
9
+    public PayPal(String payerName, String email) {
10
+        this.payerName = payerName;
11
+        this.email = email;
12
+    }
13
+
14
+    public PayPal() {
15
+    }
16
+
17
+    public void setId(Long id) {
18
+        this.id = id;
19
+    }
20
+
21
+    @Override
22
+    public String getPayerName() {
23
+        return payerName;
24
+    }
25
+
26
+    public void setPayerName(String payerName) {
27
+        this.payerName = payerName;
28
+    }
29
+
30
+    public String getEmail() {
31
+        return email;
32
+    }
33
+
34
+    public void setEmail(String email) {
35
+        this.email = email;
36
+    }
37
+
38
+    @Override
39
+    public Long getId() {
40
+        return id;
41
+    }
42
+
43
+
44
+    @Override
45
+    public String getShortDescription() {
46
+        return "PayPal " + getPayerName() + " " + getEmail();
47
+    }
48
+
49
+    @Override
50
+    public int compareTo(Payment o) {
51
+        return this.getShortDescription().compareTo(o.getShortDescription());
52
+    }
53
+}

+ 2
- 2
src/main/java/com/zipcoder/payment/Payment.java Vedi File

@@ -1,8 +1,8 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
-public interface Payment {
3
+public interface Payment extends Comparable<Payment> {
4 4
 
5
-    long getId();
5
+    Long getId();
6 6
 
7 7
     String getPayerName();
8 8
 

+ 29
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Vedi File

@@ -1,6 +1,35 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.Arrays;
4
+import java.util.Comparator;
5
+
3 6
 public class PaymentPresenter {
4 7
 
5 8
 
9
+    public String toString(Payment[] payments) {
10
+        Arrays.sort(payments);
11
+        return formatMultipleDescriptions(payments);
12
+    }
13
+
14
+
15
+    public String formatMultipleDescriptions(Payment[] payments) {
16
+        StringBuilder sb = new StringBuilder();
17
+        for (Payment payment : payments) {
18
+            sb.append(payment.getShortDescription() + "\n");
19
+        }
20
+        return sb.toString();
21
+    }
22
+
23
+    public String toStringByPayerName(Payment[] payments) {
24
+        PaymentSortByPayer psbp = new PaymentSortByPayer();
25
+        Arrays.sort(payments, psbp);
26
+        return formatMultipleDescriptions(payments);
27
+
28
+    }
29
+
30
+    public String toStringBySetId(Payment[] payments) {
31
+        Comparator<Payment> compareById = ((o1, o2) -> o1.getId().compareTo(o2.getId()));
32
+        Arrays.sort(payments, compareById);
33
+        return formatMultipleDescriptions(payments);
34
+    }
6 35
 }

+ 11
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java Vedi File

@@ -0,0 +1,11 @@
1
+package com.zipcoder.payment;
2
+
3
+import java.util.Comparator;
4
+
5
+public class PaymentSortByPayer implements Comparator<Payment>{
6
+
7
+    @Override
8
+    public int compare(Payment o1, Payment o2) {
9
+        return o1.getPayerName().compareTo(o2.getPayerName());
10
+    }
11
+}

+ 74
- 0
src/test/java/com/zipcoder/payment/CheckTest.java Vedi File

@@ -0,0 +1,74 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.assertEquals;
6
+import static org.junit.Assert.assertTrue;
7
+
8
+public class CheckTest {
9
+    @Test
10
+    public void getIdTest() {
11
+        Check check = new Check();
12
+        check.setId(0123L);
13
+
14
+        long expected = 0123L;
15
+        long actual = check.getId();
16
+
17
+        assertEquals(expected, actual);
18
+    }
19
+
20
+    @Test
21
+    public void getPayerNameTest() {
22
+        Check check = new Check();
23
+        check.setPayerName("Kris Blassingame");
24
+
25
+        String expected = "Kris Blassingame";
26
+        String actual = check.getPayerName();
27
+
28
+        assertEquals(expected, actual);
29
+    }
30
+
31
+
32
+    @Test
33
+    public void getRoutingNumberTest() {
34
+        Check check = new Check();
35
+        check.setRoutingNumber("123456789");
36
+
37
+        String expected = "123456789";
38
+        String actual = check.getRoutingNumber();
39
+
40
+        assertEquals(expected, actual);
41
+
42
+    }
43
+
44
+    @Test
45
+    public void getAccountNumberTest() {
46
+        Check check = new Check();
47
+        check.setAccountNumber("0000123456789");
48
+
49
+        String expected = "0000123456789";
50
+        String actual = check.getAccountNumber();
51
+
52
+        assertEquals(expected, actual);
53
+    }
54
+
55
+    @Test
56
+    public void getShortDescriptionTest() {
57
+        Check check = new Check("Kris Blassingame", "123456789", "0000123456789");
58
+
59
+        String expected = "Check Kris Blassingame ***6789";
60
+        check.getShortDescription();
61
+
62
+        assertEquals(check.getShortDescription(), expected);
63
+
64
+    }
65
+
66
+    @Test
67
+    public void compareToTest() {
68
+        PayPal payPal = new PayPal("Tia Mowry", "tia@mowry.com");
69
+        Check check = new Check("Tia Mowry", "123456789", "0000123456789");
70
+
71
+        assertTrue(check.compareTo(payPal) < 0);
72
+
73
+    }
74
+}

+ 15
- 5
src/test/java/com/zipcoder/payment/CreditCardTest.java Vedi File

@@ -3,6 +3,7 @@ package com.zipcoder.payment;
3 3
 import org.junit.Test;
4 4
 
5 5
 import static org.junit.Assert.assertEquals;
6
+import static org.junit.Assert.assertFalse;
6 7
 
7 8
 public class CreditCardTest {
8 9
 
@@ -30,7 +31,7 @@ public class CreditCardTest {
30 31
     }
31 32
 
32 33
     @Test
33
-    public void getNumberTest(){
34
+    public void getNumberTest() {
34 35
         CreditCard creditCard = new CreditCard();
35 36
         creditCard.setNumber("1234567890123456");
36 37
 
@@ -41,7 +42,7 @@ public class CreditCardTest {
41 42
     }
42 43
 
43 44
     @Test
44
-    public void getExpiredMonthTest(){
45
+    public void getExpiredMonthTest() {
45 46
         CreditCard creditCard = new CreditCard();
46 47
         creditCard.setExpiredMonth(06);
47 48
 
@@ -52,7 +53,7 @@ public class CreditCardTest {
52 53
     }
53 54
 
54 55
     @Test
55
-    public void getExpiredYearTest(){
56
+    public void getExpiredYearTest() {
56 57
         CreditCard creditCard = new CreditCard();
57 58
         creditCard.setExpiredYear(2020);
58 59
 
@@ -63,7 +64,7 @@ public class CreditCardTest {
63 64
     }
64 65
 
65 66
     @Test
66
-    public void getShortDescriptionTest(){
67
+    public void getShortDescriptionTest() {
67 68
         CreditCard creditCard = new CreditCard("Kris Blassingame", "1234567890987654", 6, 2020);
68 69
 
69 70
         String expected = "CC Kris Blassingame 7654 6/2020";
@@ -73,7 +74,7 @@ public class CreditCardTest {
73 74
     }
74 75
 
75 76
     @Test
76
-    public void getLastFourDigitsTest(){
77
+    public void getLastFourDigitsTest() {
77 78
         CreditCard creditCard = new CreditCard();
78 79
 
79 80
         String expected = "7654";
@@ -81,4 +82,13 @@ public class CreditCardTest {
81 82
 
82 83
         assertEquals(expected, creditCard.getLastFourDigits());
83 84
     }
85
+
86
+    @Test
87
+    public void compareToTest() {
88
+        CreditCard creditCard = new CreditCard("Tia Mowry", "1111222233334444", 6, 2020);
89
+        CreditCard creditCard1 = new CreditCard("Tia Mowry", "2222333344445555", 6, 2020);
90
+        assertFalse(creditCard.compareTo(creditCard1) == 0);
91
+    }
92
+
93
+
84 94
 }

+ 60
- 0
src/test/java/com/zipcoder/payment/PayPalTest.java Vedi File

@@ -0,0 +1,60 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.assertEquals;
6
+import static org.junit.Assert.assertTrue;
7
+
8
+public class PayPalTest {
9
+
10
+    @Test
11
+    public void getIdTest() {
12
+        PayPal payPal = new PayPal();
13
+        payPal.setId(6666L);
14
+
15
+        long expected = 6666;
16
+        long actual = payPal.getId();
17
+
18
+        assertEquals(expected, actual);
19
+    }
20
+
21
+    @Test
22
+    public void getPayerNameTest() {
23
+        PayPal payPal = new PayPal();
24
+        payPal.setPayerName("Kris Blassingame");
25
+
26
+        String expected = "Kris Blassingame";
27
+        String actual = payPal.getPayerName();
28
+
29
+        assertEquals(expected, actual);
30
+    }
31
+
32
+    @Test
33
+    public void getEmailTest() {
34
+        PayPal payPal = new PayPal();
35
+        payPal.setEmail("krisblassingame@gmail.com");
36
+
37
+        String expected = "krisblassingame@gmail.com";
38
+        String actual = payPal.getEmail();
39
+    }
40
+
41
+    @Test
42
+    public void getShortDescriptionTest() {
43
+        PayPal payPal = new PayPal("Kris Blassingame", "krisblassingame@gmail.com");
44
+
45
+        payPal.getShortDescription();
46
+        String actual = "PayPal Kris Blassingame krisblassingame@gmail.com";
47
+
48
+        assertEquals(payPal.getShortDescription(), actual);
49
+
50
+
51
+    }
52
+    @Test
53
+    public void compareToTest() {
54
+        PayPal payPal = new PayPal("Tia Mowry", "tia@mowry.com");
55
+        Check check = new Check("Tia Mowry", "123456789", "0000123456789");
56
+
57
+        assertTrue(payPal.compareTo(check) > 0);
58
+
59
+    }
60
+}

+ 69
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Vedi File

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

+ 16
- 0
src/test/java/com/zipcoder/payment/PaymentSortByPayerTest.java Vedi File

@@ -0,0 +1,16 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.assertFalse;
6
+import static org.junit.Assert.assertTrue;
7
+
8
+public class PaymentSortByPayerTest {
9
+    @Test
10
+    public void compareToTest() {
11
+        PaymentSortByPayer psbp = new PaymentSortByPayer();
12
+        CreditCard creditCard = new CreditCard("Tia Mowry", "1111222233334444", 6, 2020);
13
+        CreditCard creditCard1 = new CreditCard("Tia Mowry", "2222333344445555", 6, 2020);
14
+        assertTrue(psbp.compare(creditCard, creditCard1) == 0);
15
+    }
16
+}