#33 KrisBlassingame

Offen
KrisBlassingame möchte 2 Commits von KrisBlassingame/InterfaceComparableLab:master nach master zusammenführen

+ 8
- 0
pom.xml Datei anzeigen

@@ -7,6 +7,14 @@
7 7
     <groupId>com.zipcoder</groupId>
8 8
     <artifactId>payment</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>RELEASE</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
 
11 19
 
12 20
 </project>

BIN
src/main/java/com/zipcoder/.DS_Store Datei anzeigen


+ 66
- 0
src/main/java/com/zipcoder/payment/Check.java Datei anzeigen

@@ -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
+}

+ 82
- 0
src/main/java/com/zipcoder/payment/CreditCard.java Datei anzeigen

@@ -0,0 +1,82 @@
1
+package com.zipcoder.payment;
2
+
3
+public class CreditCard implements Payment {
4
+
5
+
6
+    Long id;
7
+    String payerName;
8
+    String number;
9
+    int expiredMonth;
10
+    int expiredYear;
11
+
12
+    public CreditCard() {
13
+    }
14
+
15
+
16
+
17
+
18
+    public CreditCard(String payerName, String number, int expiredMonth, int expiredYear) {
19
+        this.payerName = payerName;
20
+        this.number = number;
21
+        this.expiredMonth = expiredMonth;
22
+        this.expiredYear = expiredYear;
23
+    }
24
+
25
+    @Override
26
+    public Long getId() {
27
+        return id;
28
+    }
29
+
30
+    public void setId(Long id) {
31
+        this.id = id;
32
+    }
33
+
34
+    @Override
35
+    public String getPayerName() {
36
+        return payerName;
37
+    }
38
+
39
+    public void setPayerName(String payerName) {
40
+        this.payerName = payerName;
41
+    }
42
+
43
+
44
+    public String getNumber() {
45
+        return number;
46
+    }
47
+
48
+    public void setNumber(String number) {
49
+        this.number = number;
50
+    }
51
+
52
+    public int getExpiredMonth() {
53
+        return expiredMonth;
54
+    }
55
+
56
+    public void setExpiredMonth(int expiredMonth) {
57
+        this.expiredMonth = expiredMonth;
58
+    }
59
+
60
+    public int getExpiredYear() {
61
+        return expiredYear;
62
+    }
63
+
64
+    public void setExpiredYear(int expiredYear) {
65
+        this.expiredYear = expiredYear;
66
+    }
67
+
68
+    public String getShortDescription() {
69
+
70
+        return "CC " + getPayerName() + " " + getLastFourDigits() + " " + getExpiredMonth() + "/" + getExpiredYear();
71
+    }
72
+
73
+    public String getLastFourDigits(){
74
+        return getNumber().substring(number.length() - 4);
75
+    }
76
+
77
+
78
+    @Override
79
+    public int compareTo(Payment o) {
80
+        return this.getShortDescription().compareTo(o.getShortDescription());
81
+    }
82
+}

+ 53
- 0
src/main/java/com/zipcoder/payment/PayPal.java Datei anzeigen

@@ -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
+}

+ 12
- 0
src/main/java/com/zipcoder/payment/Payment.java Datei anzeigen

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

+ 29
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Datei anzeigen

@@ -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 Datei anzeigen

@@ -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 Datei anzeigen

@@ -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
+}

+ 94
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java Datei anzeigen

@@ -0,0 +1,94 @@
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.assertFalse;
7
+
8
+public class CreditCardTest {
9
+
10
+    @Test
11
+    public void getIdTest() {
12
+        CreditCard creditCard = new CreditCard();
13
+        creditCard.setId(1234L);
14
+
15
+        long expected = 1234L;
16
+        long actual = creditCard.getId();
17
+
18
+        assertEquals(expected, actual);
19
+    }
20
+
21
+
22
+    @Test
23
+    public void getPayerNameTest() {
24
+        CreditCard creditCard = new CreditCard();
25
+        creditCard.setPayerName("Kris Blassingame");
26
+
27
+        String expected = "Kris Blassingame";
28
+        String actual = creditCard.getPayerName();
29
+
30
+        assertEquals(expected, actual);
31
+    }
32
+
33
+    @Test
34
+    public void getNumberTest() {
35
+        CreditCard creditCard = new CreditCard();
36
+        creditCard.setNumber("1234567890123456");
37
+
38
+        String expected = "1234567890123456";
39
+        String actual = creditCard.getNumber();
40
+
41
+        assertEquals(expected, actual);
42
+    }
43
+
44
+    @Test
45
+    public void getExpiredMonthTest() {
46
+        CreditCard creditCard = new CreditCard();
47
+        creditCard.setExpiredMonth(06);
48
+
49
+        int expected = 06;
50
+        int actual = creditCard.getExpiredMonth();
51
+
52
+        assertEquals(expected, actual);
53
+    }
54
+
55
+    @Test
56
+    public void getExpiredYearTest() {
57
+        CreditCard creditCard = new CreditCard();
58
+        creditCard.setExpiredYear(2020);
59
+
60
+        int expected = 2020;
61
+        int actual = creditCard.getExpiredYear();
62
+
63
+        assertEquals(expected, actual);
64
+    }
65
+
66
+    @Test
67
+    public void getShortDescriptionTest() {
68
+        CreditCard creditCard = new CreditCard("Kris Blassingame", "1234567890987654", 6, 2020);
69
+
70
+        String expected = "CC Kris Blassingame 7654 6/2020";
71
+        String actual = creditCard.getShortDescription();
72
+
73
+        assertEquals(expected, actual);
74
+    }
75
+
76
+    @Test
77
+    public void getLastFourDigitsTest() {
78
+        CreditCard creditCard = new CreditCard();
79
+
80
+        String expected = "7654";
81
+        creditCard.setNumber("123456789098 7654");
82
+
83
+        assertEquals(expected, creditCard.getLastFourDigits());
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
+
94
+}

+ 60
- 0
src/test/java/com/zipcoder/payment/PayPalTest.java Datei anzeigen

@@ -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 Datei anzeigen

@@ -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 Datei anzeigen

@@ -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
+}