alizalang 8 lat temu
rodzic
commit
841d3ab149

+ 20
- 0
pom.xml Wyświetl plik

@@ -7,6 +7,26 @@
7 7
     <groupId>com.zipcoder</groupId>
8 8
     <artifactId>payment</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
22
+    <dependencies>
23
+        <dependency>
24
+            <groupId>junit</groupId>
25
+            <artifactId>junit</artifactId>
26
+            <version>RELEASE</version>
27
+            <scope>test</scope>
28
+        </dependency>
29
+    </dependencies>
10 30
 
11 31
 
12 32
 </project>

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

@@ -0,0 +1,75 @@
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
+
11
+    public Check(Long id, String payerName, String routingNumber, String accountNumber) {
12
+        this.id = id;
13
+        this.payerName = payerName;
14
+        this.routingNumber = routingNumber;
15
+        this.accountNumber = accountNumber;
16
+    }
17
+
18
+    public Check() {
19
+        id = 0L;
20
+        payerName = "";
21
+        routingNumber = "";
22
+        accountNumber = "";
23
+
24
+    }
25
+
26
+
27
+    public void setId(Long id) {
28
+        this.id = id;
29
+    }
30
+
31
+    //on Payment interface
32
+    public Long getId() {
33
+        return id;
34
+    }
35
+
36
+
37
+    public void setPayerName(String payerName) {
38
+        this.payerName = payerName;
39
+    }
40
+
41
+    //on Payment interface
42
+    public String getPayerName() {
43
+        return payerName;
44
+    }
45
+
46
+    public void setRoutingNumber(String routingNumber) {
47
+        this.routingNumber = routingNumber;
48
+    }
49
+
50
+
51
+    public String getRoutingNumber() {
52
+        return routingNumber;
53
+    }
54
+
55
+    public void setAccountNumber(String accountNumber) {
56
+        this.accountNumber = accountNumber;
57
+    }
58
+
59
+
60
+    public String getAccountNumber() {
61
+        return accountNumber;
62
+    }
63
+
64
+
65
+    //on Payment interface
66
+    public String getShortDescription() {
67
+        return  "Check " + payerName +" ***"+accountNumber.substring(accountNumber.length()-4);
68
+    }
69
+
70
+
71
+    public int compareTo(Payment o) {
72
+        return this.getShortDescription().compareTo(o.getShortDescription());
73
+    }
74
+
75
+}

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

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

+ 60
- 0
src/main/java/com/zipcoder/payment/PayPal.java Wyświetl plik

@@ -0,0 +1,60 @@
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(Long id, String payerName, String email) {
10
+        this.id = id;
11
+        this.payerName = payerName;
12
+        this.email = email;
13
+    }
14
+
15
+    public PayPal() {
16
+        id = 0L;
17
+        payerName = "";
18
+        email = "";
19
+    }
20
+
21
+
22
+    public void setId(Long id) {
23
+        this.id = id;
24
+    }
25
+
26
+    //on Payment interface
27
+    public Long getId() {
28
+        return id;
29
+    }
30
+
31
+
32
+    public void setPayerName(String payerName) {
33
+        this.payerName = payerName;
34
+    }
35
+
36
+    //on Payment interface
37
+    public String getPayerName() {
38
+        return payerName;
39
+    }
40
+
41
+
42
+    public void setEmail(String email) {
43
+        this.email = email;
44
+    }
45
+
46
+
47
+    public String getEmail() {
48
+        return email;
49
+    }
50
+
51
+    //on Payment interface
52
+    public String getShortDescription() {
53
+        return  "Paypal " + payerName +" "+email;
54
+    }
55
+
56
+
57
+    public int compareTo(Payment o) {
58
+        return this.getShortDescription().compareTo(o.getShortDescription());
59
+    }
60
+}

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

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

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

@@ -1,6 +1,49 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.Arrays;
4
+import java.util.Comparator;
5
+
6
+
3 7
 public class PaymentPresenter {
4 8
 
5 9
 
6
-}
10
+    public String toString(Payment[] payments) {
11
+
12
+        Arrays.sort(payments);
13
+
14
+        StringBuilder sb = new StringBuilder();
15
+        for (int i = 0; i < payments.length; i++) {
16
+            sb.append(payments[i].getShortDescription() + "\n");
17
+        }
18
+        return sb.toString();
19
+    }
20
+
21
+    public String toStringByPayerName(Payment[] payments) {
22
+
23
+        Arrays.sort(payments, new PaymentSortByPayer());
24
+
25
+        StringBuilder sb = new StringBuilder();
26
+        for (int i = 0; i < payments.length; i++) {
27
+            sb.append(payments[i].getShortDescription() + "\n");
28
+        }
29
+        return sb.toString();
30
+    }
31
+
32
+    public String toStringById(Payment[] payments) {
33
+        // Arrays.sort(payments, new PaymentSortByPayer());
34
+
35
+        // We don't have a PaymentSortById class . (The PaymentSortByPayer class implements the Comparator interface(which has the "compare" method)!)
36
+        // MAKE A LAMBDA EXPRESSION!
37
+
38
+
39
+        Arrays.sort(payments, Comparator.comparing(Payment::getId));
40
+        StringBuilder sb = new StringBuilder();
41
+
42
+        for (int i = 0; i < payments.length; i++) {
43
+            sb.append(payments[i].getShortDescription() + "\n");
44
+        }
45
+
46
+        return sb.toString();
47
+
48
+    }
49
+}

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

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

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

@@ -0,0 +1,68 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class CheckTest {
8
+
9
+
10
+    Check check;
11
+
12
+    @Before
13
+    public void setUp(){
14
+        check = new Check();
15
+    }
16
+
17
+    @Test
18
+    public void getSetIdTest() {
19
+        check.setId(1L);
20
+        Long expected = 1L;
21
+        Long actual = check.getId();
22
+        Assert.assertEquals(expected, actual);
23
+    }
24
+
25
+
26
+    @Test
27
+    public void getSetPayerNameTest() {
28
+        check.setPayerName("Aliza");
29
+        String expected = "Aliza";
30
+        String actual = check.getPayerName();
31
+        Assert.assertEquals(expected, actual);
32
+    }
33
+
34
+
35
+    @Test
36
+    public void getSetRoutingNumberTest() {
37
+        check.setRoutingNumber("031201360");
38
+        String expected = "031201360";
39
+        String actual = check.getRoutingNumber();
40
+        Assert.assertEquals(expected, actual);
41
+    }
42
+
43
+    @Test
44
+    public void getSetAccountNumberTest() {
45
+        check.setAccountNumber("0123456789");
46
+        String expected = "0123456789";
47
+        String actual = check.getAccountNumber();
48
+        Assert.assertEquals(expected, actual);
49
+    }
50
+
51
+
52
+    @Test
53
+    public void getShortDescriptionTest() {
54
+        check= new Check(1L, "Aliza Lang", "031201360", "0123456789");
55
+        String expected = "Check Aliza Lang ***6789";
56
+        String actual = check.getShortDescription();
57
+        Assert.assertEquals(expected, actual);
58
+    }
59
+
60
+    @Test
61
+    public void compareToTest() {
62
+        check = new Check(1L, "Aliza Lang", "031201360", "0123456789");
63
+        Check check2 = new Check(2L, "Tom Smith", "031201360", "9876543210");
64
+
65
+        int actual = check.compareTo(check2);
66
+        Assert.assertTrue(actual < 0);
67
+    }
68
+}

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

@@ -0,0 +1,77 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class CreditCardTest {
8
+
9
+    CreditCard creditCard;
10
+    @Before
11
+    public void setUp() {
12
+        creditCard = new CreditCard();
13
+    }
14
+    @Test
15
+    public void getSetIdTest() {
16
+        creditCard.setId(1L);
17
+        Long expected = 1L;
18
+        Long actual = creditCard.getId();
19
+        Assert.assertEquals(expected, actual);
20
+    }
21
+
22
+
23
+
24
+    @Test
25
+    public void getSetPayerNameTest() {
26
+        creditCard.setPayerName("Aliza");
27
+        String expected = "Aliza";
28
+        String actual = creditCard.getPayerName();
29
+        Assert.assertEquals(expected, actual);
30
+    }
31
+
32
+
33
+
34
+    @Test
35
+    public void getSetNumberTest() {
36
+        creditCard.setNumber("4444555566667777");
37
+        String expected = "4444555566667777";
38
+        String actual = creditCard.getNumber();
39
+        Assert.assertEquals(expected, actual);
40
+    }
41
+
42
+
43
+    @Test
44
+    public void getSetExpiredMonthTest() {
45
+        creditCard.setExpiredMonth(8);
46
+        int expected = 8;
47
+        int actual = creditCard.getExpiredMonth();
48
+        Assert.assertEquals(expected, actual);
49
+    }
50
+
51
+
52
+
53
+    @Test
54
+    public void getSetExpiredYearTest() {
55
+        creditCard.setExpiredYear(2022);
56
+        int expected = 2022;
57
+        int actual = creditCard.getExpiredYear();
58
+        Assert.assertEquals(expected, actual);
59
+    }
60
+
61
+    @Test
62
+    public void getShortDescriptionTest() {
63
+        creditCard = new CreditCard(1L, "Aliza Lang", "4444555566667777", 8, 2022);
64
+        String expected = "CC Aliza Lang 7777 8/2022";
65
+        String actual = creditCard.getShortDescription();
66
+        Assert.assertEquals(expected, actual);
67
+    }
68
+
69
+    @Test
70
+    public void compareToTest() {
71
+        creditCard = new CreditCard(1L, "Aliza Lang", "4444555566667777", 8, 2022);
72
+        CreditCard creditCard1 = new CreditCard(2L, "Tom Smith", "4444555566669999", 9, 2032);
73
+        int actual = creditCard.compareTo(creditCard1);
74
+        Assert.assertTrue(actual < 0);
75
+    }
76
+
77
+}

+ 56
- 0
src/test/java/com/zipcoder/payment/PayPalTest.java Wyświetl plik

@@ -0,0 +1,56 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class PayPalTest {
8
+
9
+    PayPal payPal;
10
+
11
+    @Before
12
+    public void setUp() {
13
+        payPal = new PayPal();
14
+    }
15
+
16
+    @Test
17
+    public void getSetIdTest() {
18
+        payPal.setId(1L);
19
+        Long expected = 1L;
20
+        Long actual = payPal.getId();
21
+        Assert.assertEquals(expected, actual);
22
+    }
23
+
24
+
25
+    @Test
26
+    public void getSetPayerNameTest() {
27
+        payPal.setPayerName("Aliza Lang");
28
+        String expected = "Aliza Lang";
29
+        String actual = payPal.getPayerName();
30
+        Assert.assertEquals(expected, actual);
31
+    }
32
+
33
+    @Test
34
+    public void getSetEmailTest() {
35
+        payPal.setEmail("alizalang@gmail.com");
36
+        String expected = "alizalang@gmail.com";
37
+        String actual = payPal.getEmail();
38
+        Assert.assertEquals(expected, actual);
39
+    }
40
+
41
+    @Test
42
+    public void getShortDescriptionTest() {
43
+        payPal = new PayPal(1L, "Aliza Lang", "alizalang@gmail.com");
44
+        String expected = "Paypal Aliza Lang alizalang@gmail.com";
45
+        String actual = payPal.getShortDescription();
46
+        Assert.assertEquals(expected, actual);
47
+    }
48
+
49
+    @Test
50
+    public void compareToTest() {
51
+       PayPal payPal1 = new PayPal(1L, "Aliza Lang", "alizalang@gmail.com");
52
+       PayPal payPal2 = new PayPal(2L, "Tom Smith", "tomsmith@gmail.com");
53
+        int actual = payPal1.compareTo(payPal2);
54
+        Assert.assertTrue(actual < 0);
55
+    }
56
+}

+ 70
- 1
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Wyświetl plik

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

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

@@ -0,0 +1,50 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class PaymentSortByPayerTest {
8
+
9
+    PaymentSortByPayer paymentSortByPayer;
10
+    Check check;
11
+    PayPal payPal;
12
+    CreditCard creditCard;
13
+
14
+    @Before
15
+    public void setUp() {
16
+
17
+        paymentSortByPayer = new PaymentSortByPayer();
18
+        check = new Check(1L, "Aliza Lang", "0123456789", "031201360");
19
+        payPal = new PayPal(2L, "Tom Smith", "tomsmith@gmail.com");
20
+        creditCard = new CreditCard(1L, "Aliza Lang", "1111222233334444", 10, 2025);
21
+
22
+    }
23
+
24
+    @Test
25
+    public void ifPaymentOneComesAfterPaymentTwoCompareTest() {
26
+
27
+//  The compare method will return number larger than 0 if payment1 comes after payment2
28
+
29
+        int actual = paymentSortByPayer.compare(check, payPal);
30
+        Assert.assertTrue(actual < 0);
31
+    }
32
+
33
+    @Test
34
+    public void ifPaymentHasSameInformationCompareTest() {
35
+
36
+//    The compare method will return number 0 if the payment has the same information
37
+
38
+        int actual = paymentSortByPayer.compare(check, creditCard);
39
+        Assert.assertTrue(actual == 0);
40
+    }
41
+
42
+    @Test
43
+    public void ifPaymentOneComesBeforePaymentTwoCompareTest() {
44
+
45
+//    The compare method will return number less than 0 if payment1 comes before payment2
46
+
47
+        int actual = paymentSortByPayer.compare(payPal, check);
48
+        Assert.assertTrue(actual > 0);
49
+    }
50
+}