Demetrius Murray 7 anni fa
parent
commit
5654da1ef5

+ 8
- 0
pom.xml Vedi File

@@ -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
     <properties>
11 19
         <maven.compiler.source>1.8</maven.compiler.source>
12 20
         <maven.compiler.target>1.8</maven.compiler.target>

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

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

+ 64
- 0
src/main/java/com/zipcoder/payment/CreditCard.java Vedi File

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

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

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

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

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

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

@@ -1,6 +1,33 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.Arrays;
4
+import java.util.Collections;
5
+import java.util.Comparator;
6
+import java.util.List;
7
+
3 8
 public class PaymentPresenter {
9
+PaymentSortByPayer psp = new PaymentSortByPayer();
10
+    public String toString(Payment[] payments){
11
+        List<Payment> list = Arrays.asList(payments);
12
+        Collections.sort(list);
13
+        StringBuilder sb = new StringBuilder();
14
+        list.forEach(e -> sb.append(e.getShortDescription() + "\n"));
15
+        return sb.toString();
16
+    }
4 17
 
18
+    public String toStringByPayerName(Payment[] payments){
19
+        List<Payment> list = Arrays.asList(payments);
20
+        list.sort(psp);
21
+        StringBuilder sb = new StringBuilder();
22
+        list.forEach(e -> sb.append(e.getShortDescription() + "\n"));
23
+        return sb.toString();
24
+    }
5 25
 
26
+    public String toStringById(Payment[] payments){
27
+        List<Payment> list = Arrays.asList(payments);
28
+        list.sort(Comparator.comparingLong(e -> e.getId()));
29
+        StringBuilder sb = new StringBuilder();
30
+        list.forEach(e -> sb.append(e.getShortDescription() + "\n"));
31
+        return  sb.toString();
32
+    }
6 33
 }

+ 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
+//why instantiate this class in Payment Presenter when I can use lambda or a comparator in PaymentPresenter?
7
+    @Override
8
+    public int compare(Payment o1, Payment o2) {
9
+        return o1.getPayerName().compareTo(o2.getPayerName());
10
+    }
11
+}

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

@@ -0,0 +1,78 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CheckTest {
7
+    Check c = new Check("Moses","1111111111", "2222222222");
8
+
9
+    @Test
10
+    public void testGetId(){
11
+        Long actual = c.getId();
12
+        Long expected = 1L;
13
+        Assert.assertEquals(expected,actual);
14
+    }
15
+
16
+    @Test
17
+    public void testSetId(){
18
+        c.setId(2);
19
+        Long actual = c.getId();
20
+        Long expected = 2L;
21
+        Assert.assertEquals(expected,actual);
22
+    }
23
+
24
+    @Test
25
+    public void testGetPayerName(){
26
+        String actual = c.getPayerName();
27
+        String expected = "Moses";
28
+        Assert.assertEquals(expected,actual);
29
+    }
30
+    @Test
31
+    public void testSetPayerName(){
32
+        c.setPayerName("Abraham");
33
+        String actual = c.getPayerName();
34
+        String expected = "Abraham";
35
+        Assert.assertEquals(expected,actual);
36
+    }
37
+
38
+    @Test
39
+    public void testSetAccountNumber(){
40
+        c.setAccountNumber("3333333333");
41
+        String actual = c.getAccountNumber();
42
+        String expected = "3333333333";
43
+        Assert.assertEquals(expected,actual);
44
+    }
45
+    @Test
46
+    public void testGetAccountNumber(){
47
+        String actual = c.getAccountNumber();
48
+        String expected = "1111111111";
49
+        Assert.assertEquals(expected,actual);
50
+    }
51
+
52
+    @Test
53
+    public void testSetRoutingNumber(){
54
+        c.setRoutingNumber("4444444444");
55
+        String actual = c.getRoutingNumber();
56
+        String expected = "4444444444";
57
+        Assert.assertEquals(expected,actual);
58
+    }
59
+    @Test
60
+    public void testGetRoutingNumber(){
61
+        String actual = c.getRoutingNumber();
62
+        String expected = "2222222222";
63
+        Assert.assertEquals(expected,actual);
64
+    }
65
+
66
+    @Test
67
+    public void testGetShortDescription(){
68
+        String actual = c.getShortDescription();
69
+        String expected = "Check Moses ****1111";
70
+        Assert.assertEquals(expected,actual);
71
+    }
72
+
73
+    @Test
74
+    public void testCompareTo(){
75
+        Integer actual = c.compareTo(new CreditCard("Moses", 4, -2018, "302-xxx-xxxx"));
76
+        Assert.assertTrue(actual > 0);
77
+    }
78
+}

+ 95
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java Vedi File

@@ -0,0 +1,95 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CreditCardTest {
7
+    CreditCard cc = new CreditCard("Moses", 4, -2018, "302-xxx-xxxx");
8
+
9
+    @Test
10
+    public void testGetId(){
11
+        Long actual = cc.getId();
12
+        Long expected = 1L;
13
+        Assert.assertEquals(expected,actual);
14
+    }
15
+
16
+    @Test
17
+    public void testSetId(){
18
+        cc.setId(2);
19
+        Long actual = cc.getId();
20
+        Long expected = 2L;
21
+        Assert.assertEquals(expected,actual);
22
+    }
23
+
24
+    @Test
25
+    public void testGetPayerName(){
26
+        String actual = cc.getPayerName();
27
+        String expected = "Moses";
28
+        Assert.assertEquals(expected,actual);
29
+    }
30
+    @Test
31
+    public void testSetPayerName(){
32
+        cc.setPayerName("Abraham");
33
+        String actual = cc.getPayerName();
34
+        String expected = "Abraham";
35
+        Assert.assertEquals(expected,actual);
36
+    }
37
+
38
+    @Test
39
+    public void testSetNumber(){
40
+        cc.setNumber("302-xxx-xxxx");
41
+        String actual = cc.getNumber();
42
+        String expected = "302-xxx-xxxx";
43
+        Assert.assertEquals(expected,actual);
44
+    }
45
+    @Test
46
+    public void testGetNumber(){
47
+        cc.setNumber("302-xxx-xxxx");
48
+        String actual = cc.getNumber();
49
+        String expected = "302-xxx-xxxx";
50
+        Assert.assertEquals(expected,actual);
51
+    }
52
+
53
+    @Test
54
+    public void testGetExpiredMonth(){
55
+        Integer actual = cc.getExpiredMonth();
56
+        Integer expected = 4;
57
+        Assert.assertEquals(expected,actual);
58
+    }
59
+
60
+    @Test
61
+    public void testSetExpiredMonth(){
62
+        cc.setExpiredMonth(3);
63
+        Integer actual = cc.getExpiredMonth();
64
+        Integer expected = 3;
65
+        Assert.assertEquals(expected,actual);
66
+    }
67
+
68
+    @Test
69
+    public void testGetExpiredYear(){
70
+        Integer actual = cc.getExpiredYear();
71
+        Integer expected = -2018;
72
+        Assert.assertEquals(expected,actual);
73
+    }
74
+
75
+    @Test
76
+    public void testSetExpiredYear(){
77
+        cc.setExpiredYear(-3000);
78
+        Integer actual = cc.getExpiredYear();
79
+        Integer expected = -3000;
80
+        Assert.assertEquals(expected,actual);
81
+    }
82
+
83
+    @Test
84
+    public void testGetShortDescription(){
85
+        String actual = cc.getShortDescription();
86
+        String expected = "CC Moses xxxx 4/-2018";
87
+        Assert.assertEquals(expected,actual);
88
+    }
89
+
90
+    @Test
91
+    public void testCompareTo(){
92
+        Integer actual = cc.compareTo(new Check("Moses","1111111111", "2222222222"));
93
+        Assert.assertTrue(actual < 0);
94
+    }
95
+}

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

@@ -0,0 +1,66 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class PayPalTest {
7
+    PayPal p = new PayPal("Moses", "egyptianprince@gmail.com");
8
+
9
+    @Test
10
+    public void testGetId(){
11
+        Long actual = p.getId();
12
+        Long expected = 1L;
13
+        Assert.assertEquals(expected,actual);
14
+    }
15
+
16
+    @Test
17
+    public void testSetId(){
18
+        p.setId(2L);
19
+        Long actual = p.getId();
20
+        Long expected = 2L;
21
+        Assert.assertEquals(expected,actual);
22
+    }
23
+
24
+    @Test
25
+    public void testGetEmail(){
26
+        String actual = p.getEmail();
27
+        String expected = "egyptianprince@gmail.com";
28
+        Assert.assertEquals(expected,actual);
29
+    }
30
+
31
+    @Test
32
+    public void testSetEmail(){
33
+        p.setEmail("letmypeoplego@gmail.com");
34
+        String actual = p.getEmail();
35
+        String expected = "letmypeoplego@gmail.com";
36
+        Assert.assertEquals(expected,actual);
37
+    }
38
+
39
+    @Test
40
+    public void testGetPayerName(){
41
+        String actual = p.getPayerName();
42
+        String expected = "Moses";
43
+        Assert.assertEquals(expected,actual);
44
+    }
45
+
46
+    @Test
47
+    public void testSetPayerName(){
48
+        p.setPayerName("Pharoah");
49
+        String actual = p.getPayerName();
50
+        String expected = "Pharoah";
51
+        Assert.assertEquals(expected,actual);
52
+    }
53
+
54
+    @Test
55
+    public void testGetShortDescription(){
56
+            String actual = p.getShortDescription();
57
+            String expected = "Paypal Moses egyptianprince@gmail.com";
58
+            Assert.assertEquals(expected,actual);
59
+    }
60
+
61
+    @Test
62
+    public void testCompareTo(){
63
+        Integer actual = p.compareTo(new Check("Moses","1111111111", "2222222222"));
64
+        Assert.assertTrue(actual > 0);
65
+    }
66
+}

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

@@ -1,4 +1,68 @@
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
+    @Test
9
+    public void testToStringNoPayment(){
10
+        Payment[] payments = new Payment[0];
11
+        PaymentPresenter presenter = new PaymentPresenter();
12
+        String expected = "";
13
+
14
+        String actual = presenter.toString(payments);
15
+
16
+        Assert.assertEquals(expected,actual);
17
+    }
18
+
19
+    @Test
20
+    public void testToStringMultiplePayments(){
21
+        Payment[] payments = new Payment[2];
22
+        Payment paypal = new PayPal("Tia Mowry", "tia@mowry.com");
23
+        Payment check = new Check("Tia Mowry", "11432543","134344551");
24
+
25
+        payments[0] = paypal;
26
+        payments[1] = check;
27
+
28
+        PaymentPresenter presenter = new PaymentPresenter();
29
+        String expected = "Check Tia Mowry ****2543\nPaypal Tia Mowry tia@mowry.com\n";
30
+
31
+        String actual = presenter.toString(payments);
32
+        Assert.assertEquals(expected,actual);
33
+    }
34
+
35
+    @Test
36
+    public void testToStringByPayerName(){
37
+        Payment[] payments = new Payment[2];
38
+        Payment paypal = new PayPal("Tamara Mowry", "tamara@mowry.com");
39
+        Payment check = new Check("Tia Mowry", "11432543", "134344551");
40
+
41
+        payments[0] = paypal;
42
+        payments[1] = check;
43
+
44
+        PaymentPresenter presenter = new PaymentPresenter();
45
+        String expected = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ****2543\n";
46
+
47
+        String actual = presenter.toStringByPayerName(payments);
48
+        Assert.assertEquals(expected, actual);
49
+    }
50
+
51
+    @Test
52
+    public void testToStringById(){
53
+        Payment[] payments = new Payment[2];
54
+        Payment paypal = new PayPal("Tamara Mowry", "tamara@mowry.com");
55
+        Payment check = new Check("Tia Mowry", "11432543", "134344551");
56
+        ((PayPal) paypal).setId(3L);
57
+        ((Check) check).setId(1L);
58
+
59
+        payments[0] = paypal;
60
+        payments[1] = check;
61
+
62
+        PaymentPresenter presenter = new PaymentPresenter();
63
+        String expected = "Check Tia Mowry ****2543\nPaypal Tamara Mowry tamara@mowry.com\n";
64
+
65
+        String actual = presenter.toStringById(payments);
66
+        Assert.assertEquals(expected, actual);
67
+    }
4 68
 }