Przeglądaj źródła

finished commit

Joshua Chung 8 lat temu
rodzic
commit
473db7b53f

+ 8
- 0
pom.xml Wyświetl plik

@@ -8,5 +8,13 @@
8 8
     <artifactId>payment</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <dependencies>
12
+        <dependency>
13
+            <groupId>junit</groupId>
14
+            <artifactId>junit</artifactId>
15
+            <version>4.12</version>
16
+        </dependency>
17
+    </dependencies>
18
+
11 19
 
12 20
 </project>

+ 58
- 0
src/main/java/com/zipcoder/payment/Check.java Wyświetl plik

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

+ 77
- 0
src/main/java/com/zipcoder/payment/CreditCard.java Wyświetl plik

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

+ 9
- 0
src/main/java/com/zipcoder/payment/Payment.java Wyświetl plik

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

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

@@ -1,6 +1,34 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 public class PaymentPresenter {
4 6
 
5 7
 
8
+    public String toString(Payment[] payments) {
9
+
10
+         Arrays.sort(payments, new PaymentSortByShortDescription());
11
+
12
+        return createString(payments);
13
+    }
14
+
15
+    public String toStringByPayerName(Payment[] payments) {
16
+        Arrays.sort(payments, new PaymentSortByPayer());
17
+
18
+        return createString(payments);
19
+    }
20
+
21
+    public String toStringById(Payment[] payments) {
22
+        Arrays.sort(payments);
23
+
24
+        return createString(payments);
25
+    }
26
+
27
+    private String createString(Payment[] payments) {
28
+        String retVal = "";
29
+        for(Payment payment : payments) {
30
+            retVal+=payment.getShortDescription() + "\n";
31
+        }
32
+        return retVal;
33
+    }
6 34
 }

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

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

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

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

+ 35
- 0
src/main/java/com/zipcoder/payment/Paypal.java Wyświetl plik

@@ -0,0 +1,35 @@
1
+package com.zipcoder.payment;
2
+
3
+public class Paypal implements Payment{
4
+
5
+    private Long id;
6
+    private String payerName;
7
+    private String email;
8
+
9
+    public void setId(Long id) {
10
+        this.id = id;
11
+    }
12
+
13
+    public Paypal(Long id, String payerName, String email) {
14
+
15
+        this.id = id;
16
+        this.payerName = payerName;
17
+        this.email = email;
18
+    }
19
+
20
+    public Long getId() {
21
+        return id;
22
+    }
23
+
24
+    public String getPayerName() {
25
+        return payerName;
26
+    }
27
+
28
+    public String getShortDescription() {
29
+        return "Paypal " + payerName + " " + email;
30
+    }
31
+
32
+    public int compareTo(Payment o) {
33
+        return this.id.compareTo(o.getId());
34
+    }
35
+}

+ 20
- 0
src/test/java/com/zipcoder/payment/CheckTest.java Wyświetl plik

@@ -0,0 +1,20 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CheckTest {
7
+
8
+    @Test
9
+    public void  getShortDescriptionTest() {
10
+        // Arrange
11
+        Check check = new Check(81L, "Tia Mowry", "11432543", "134344551");
12
+        String expected = "Check Tia Mowry ***4551";
13
+
14
+        // Act
15
+        String actual = check.getShortDescription();
16
+
17
+        // Assert
18
+        Assert.assertEquals(expected, actual);
19
+    }
20
+}

+ 103
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java Wyświetl plik

@@ -0,0 +1,103 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CreditCardTest {
7
+
8
+    @Test
9
+    public void idTest() {
10
+        // Arrange
11
+        CreditCard creditCard = new CreditCard();
12
+        Long expectedId = 100L;
13
+        creditCard.setId(expectedId);
14
+
15
+        // Act
16
+        Long actualId = creditCard.getId();
17
+
18
+        // Assert
19
+        Assert.assertEquals(expectedId, actualId);
20
+    }
21
+
22
+    @Test
23
+    public void payerNameTest() {
24
+        // Arrange
25
+        CreditCard creditCard = new CreditCard();
26
+        String expectedPayerName = "Joe";
27
+        creditCard.setPayerName(expectedPayerName);
28
+
29
+        // Act
30
+        String actualPayerName= creditCard.getPayerName();
31
+
32
+        // Assert
33
+        Assert.assertEquals(expectedPayerName, actualPayerName);
34
+    }
35
+
36
+    @Test
37
+    public void numberTest() {
38
+        // Arrange
39
+        CreditCard creditCard = new CreditCard();
40
+        String expectedNumber = "12345";
41
+        creditCard.setNumber(expectedNumber);
42
+
43
+        // Act
44
+        String actualNumber = creditCard.getNumber();
45
+
46
+        // Assert
47
+        Assert.assertEquals(expectedNumber, actualNumber);
48
+    }
49
+
50
+    @Test
51
+    public void expiredMonthTest() {
52
+        // Arrange
53
+        CreditCard creditCard = new CreditCard();
54
+        int expectedExpiredMonth = 2;
55
+        creditCard.setExpiredMonth(expectedExpiredMonth);
56
+
57
+        // Act
58
+        int actualExpiredMonth = creditCard.getExpiredMonth();
59
+
60
+        // Assert
61
+        Assert.assertEquals(expectedExpiredMonth, actualExpiredMonth);
62
+    }
63
+
64
+    @Test
65
+    public void expiredYearTest() {
66
+        // Arrange
67
+        CreditCard creditCard = new CreditCard();
68
+        int expectedExpiredYear = 2019;
69
+        creditCard.setExpiredYear(expectedExpiredYear);
70
+
71
+        // Act
72
+        int actualExpiredYear = creditCard.getExpiredYear();
73
+
74
+        // Assert
75
+        Assert.assertEquals(expectedExpiredYear, actualExpiredYear);
76
+    }
77
+
78
+    @Test
79
+    public void getShortDescription_DoubleDigitMonth_ShouldReturnShortDescription() {
80
+        // Arrange
81
+        CreditCard creditCard = new CreditCard(284914091L, "John Doe", "1234567890123456", 11, 2001);
82
+        String expectedShortDescription = "CC John Doe 3456 11/2001";
83
+
84
+        // Act
85
+        String actualShortDescription = creditCard.getShortDescription();
86
+
87
+        // Assert
88
+        Assert.assertEquals(expectedShortDescription, actualShortDescription);
89
+    }
90
+
91
+    @Test
92
+    public void getShortDescription_SingleDigitMonth_ShouldReturnShortDescription() {
93
+        // Arrange
94
+        CreditCard creditCard = new CreditCard(284914091L, "John Doe", "1234567890123456", 1, 2001);
95
+        String expectedShortDescription = "CC John Doe 3456 01/2001";
96
+
97
+        // Act
98
+        String actualShortDescription = creditCard.getShortDescription();
99
+
100
+        // Assert
101
+        Assert.assertEquals(expectedShortDescription, actualShortDescription);
102
+    }
103
+}

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

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

+ 20
- 0
src/test/java/com/zipcoder/payment/PaypalTests.java Wyświetl plik

@@ -0,0 +1,20 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class PaypalTests {
7
+
8
+    @Test
9
+    public void  getShortDescriptionTest() {
10
+        // Arrange
11
+        Paypal paypal = new Paypal(100L, "Tia Mowry", "tia@mowry.com");
12
+        String expectedShortDescription = "Paypal Tia Mowry tia@mowry.com";
13
+
14
+        // Act
15
+        String actualShortDescription = paypal.getShortDescription();
16
+
17
+        // Assert
18
+        Assert.assertEquals(expectedShortDescription, actualShortDescription);
19
+    }
20
+}