Преглед изворни кода

finished using only lambdas

Tommy Rogers пре 8 година
родитељ
комит
d4c70329de

+ 20
- 0
pom.xml Прегледај датотеку

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

+ 67
- 0
src/main/java/com/zipcoder/payment/Check.java Прегледај датотеку

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

+ 78
- 0
src/main/java/com/zipcoder/payment/CreditCard.java Прегледај датотеку

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

+ 56
- 0
src/main/java/com/zipcoder/payment/PayPal.java Прегледај датотеку

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

+ 10
- 0
src/main/java/com/zipcoder/payment/Payment.java Прегледај датотеку

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

+ 40
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Прегледај датотеку

@@ -1,6 +1,46 @@
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[] payment) {
10
+        return buildString(sortMultiplePayments(payment));
11
+    }
12
+
13
+    public Payment[] sortMultiplePayments(Payment[] payment) {
14
+        Arrays.sort(payment, Comparator.comparing(Payment::getShortDescription));
15
+        return payment;
16
+    }
17
+
18
+
19
+    public String toStringByPayerName(Payment[] payment) {
20
+        return buildString(sortByPayer(payment));
21
+    }
22
+
23
+    public Payment[] sortByPayer(Payment[] payment) {
24
+        Arrays.sort(payment, Comparator.comparing(Payment::getPayerName));
25
+        return payment;
26
+    }
27
+
28
+    public String toStringById(Payment[] payment) {
29
+        return buildString(sortByID(payment));
30
+    }
31
+
32
+    public Payment [] sortByID(Payment[] payment){
33
+        Arrays.sort(payment, Comparator.comparing(Payment::getId));
34
+        return payment;
35
+    }
36
+
37
+    public String buildString(Payment[] payment){
38
+        StringBuilder builder = new StringBuilder();
39
+        for(Payment p : payment)
40
+            builder.append(p.getShortDescription() + "\n");
41
+        return builder.toString();
42
+    }
43
+
44
+
45
+
6 46
 }

+ 70
- 0
src/test/java/com/zipcoder/payment/CheckTest.java Прегледај датотеку

@@ -0,0 +1,70 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.*;
6
+
7
+public class CheckTest {
8
+
9
+    Check check = new Check();
10
+
11
+    @Test
12
+    public void getRoutingNumber() {
13
+        check.setRoutingNumber("928350383");
14
+
15
+        String expected = "928350383";
16
+        String actual = check.getRoutingNumber();
17
+
18
+        assertEquals(actual, expected);
19
+    }
20
+
21
+    @Test
22
+    public void getAccountNumber() {
23
+        check.setAccountNumber("292472491014");
24
+
25
+        String expected = "292472491014";
26
+        String actual = check.getAccountNumber();
27
+
28
+        assertEquals(actual, expected);
29
+    }
30
+
31
+    @Test
32
+    public void getId() {
33
+        check.setId(19838193);
34
+
35
+        long expected =19838193;
36
+        long actual = check.getId();
37
+
38
+        assertEquals(actual, expected);
39
+    }
40
+
41
+    @Test
42
+    public void getPayerName() {
43
+        check.setPayerName("Xerces The Great");
44
+
45
+        String expected = "Xerces The Great";
46
+        String actual = check.getPayerName();
47
+
48
+        assertEquals(actual, expected);
49
+    }
50
+
51
+    @Test
52
+    public void getShortDescription() {
53
+        check.setAccountNumber("1234 1723 1826 9736");
54
+        check.setPayerName("Xerces The Great");
55
+
56
+
57
+        String expected = "Check Xerces The Great ***9736";
58
+        String actual = check.getShortDescription();
59
+
60
+        assertEquals(actual, expected);
61
+    }
62
+
63
+    @org.junit.Test
64
+    public void compareTo(){
65
+        Check check = new Check("Jimmy Dean", "297393022");
66
+        PayPal pp = new PayPal("Slim Shady", "TheRealSlimShady@pleasestandup.com");
67
+
68
+        assertTrue(check.compareTo(pp) < 0 );
69
+    }
70
+}

+ 83
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java Прегледај датотеку

@@ -0,0 +1,83 @@
1
+package com.zipcoder.payment;
2
+
3
+import static org.junit.Assert.*;
4
+
5
+public class CreditCardTest {
6
+
7
+    CreditCard card = new CreditCard();
8
+
9
+    @org.junit.Test
10
+    public void getId() {
11
+        card.setId(19838193);
12
+
13
+        long expected =19838193;
14
+        long actual = card.getId();
15
+
16
+        assertEquals(actual, expected);
17
+    }
18
+
19
+    @org.junit.Test
20
+    public void getPayerName() {
21
+        card.setPayerName("Xerces The Great");
22
+
23
+        String expected = "Xerces The Great";
24
+        String actual = card.getPayerName();
25
+
26
+        assertEquals(actual, expected);
27
+
28
+    }
29
+
30
+    @org.junit.Test
31
+    public void getNumber() {
32
+        card.setNumber("1234 1723 1826 9736");
33
+
34
+        String expected = "1234 1723 1826 9736";
35
+        String actual = card.getNumber();
36
+
37
+        assertEquals(actual, expected);
38
+    }
39
+
40
+    @org.junit.Test
41
+    public void getExpiredMonth() {
42
+        card.setExpiredMonth(11);
43
+
44
+        int expected = 11;
45
+        int actual = card.getExpiredMonth();
46
+
47
+        assertEquals(actual, expected);
48
+    }
49
+
50
+    @org.junit.Test
51
+    public void getExpiredYear() {
52
+        card.setExpiredYear(535);
53
+
54
+        int expected = 535;
55
+        int actual = card.getExpiredYear();
56
+
57
+        assertEquals(actual, expected);
58
+    }
59
+
60
+    @org.junit.Test
61
+    public void getShortDescription() {
62
+        card.setExpiredYear(535);
63
+        card.setExpiredMonth(11);
64
+        card.setNumber("1234 1723 1826 9736");
65
+        card.setPayerName("Xerces The Great");
66
+
67
+
68
+        String expected = "CC Xerces The Great 9736 11/535";
69
+        String actual = card.getShortDescription();
70
+
71
+        assertEquals(actual, expected);
72
+    }
73
+
74
+    @org.junit.Test
75
+    public void compareTo(){
76
+        CreditCard cc = new CreditCard("Jimmy Dean", "1023 2983 0839 2938", 9, 2040);
77
+        PayPal pp = new PayPal("Slim Shady", "TheRealSlimShady@pleasestandup.com");
78
+
79
+        assertTrue(cc.compareTo(pp) < 0 );
80
+    }
81
+
82
+
83
+}

+ 60
- 0
src/test/java/com/zipcoder/payment/PayPalTest.java Прегледај датотеку

@@ -0,0 +1,60 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.*;
6
+
7
+public class PayPalTest {
8
+
9
+    PayPal payPal = new PayPal();
10
+
11
+    @Test
12
+    public void getEmail() {
13
+        payPal.setEmail("IConquerBecauseIamLonely@ruleworld.com");
14
+
15
+        String expected = "IConquerBecauseIamLonely@ruleworld.com";
16
+        String actual = payPal.getEmail();
17
+
18
+        assertEquals(actual, expected);
19
+    }
20
+
21
+    @Test
22
+    public void getId() {
23
+        payPal.setId(19838193);
24
+
25
+        long expected =19838193;
26
+        long actual = payPal.getId();
27
+
28
+        assertEquals(actual, expected);
29
+    }
30
+
31
+    @Test
32
+    public void getPayerName() {
33
+        payPal.setPayerName("Xerces The Great");
34
+
35
+        String expected = "Xerces The Great";
36
+        String actual = payPal.getPayerName();
37
+
38
+        assertEquals(actual, expected);
39
+    }
40
+
41
+    @Test
42
+    public void getShortDescription() {
43
+        payPal.setEmail("IConquerBecauseIamLonely@ruleworld.com");
44
+        payPal.setPayerName("Xerces The Great");
45
+
46
+
47
+        String expected = "Paypal Xerces The Great IConquerBecauseIamLonely@ruleworld.com";
48
+        String actual = payPal.getShortDescription();
49
+
50
+        assertEquals(actual, expected);
51
+    }
52
+
53
+    @org.junit.Test
54
+    public void compareTo(){
55
+        CreditCard cc = new CreditCard("Jimmy Dean", "1023 2983 0839 2938", 9, 2040);
56
+        PayPal pp = new PayPal("Slim Shady", "TheRealSlimShady@pleasestandup.com");
57
+
58
+        assertTrue(cc.compareTo(pp) < 0 );
59
+    }
60
+}

+ 108
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Прегледај датотеку

@@ -1,4 +1,112 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.*;
6
+
3 7
 public class PaymentPresenterTest {
8
+
9
+    //When there is no payment
10
+    @Test
11
+    public void noPayment() {
12
+        Payment[] payment = new Payment[0];
13
+        PaymentPresenter presenter = new PaymentPresenter();
14
+        String expected = "";
15
+
16
+        String actual = presenter.toString(payment);
17
+
18
+        assertEquals(expected, actual);
19
+
20
+    }
21
+
22
+
23
+    //When there are multiple payments, you need to sort it first,
24
+    // then build the string using the short description
25
+
26
+    @Test
27
+    public void sortMultiplePayments() {
28
+        Payment[] payment = new Payment[2];
29
+        Payment paypal = new PayPal(4L, "Tia Mowry", "tia@mowry.com");
30
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
31
+
32
+        payment[0] = paypal;
33
+        payment[1] = check;
34
+
35
+        PaymentPresenter presenter = new PaymentPresenter();
36
+        String expected = "Check Tia Mowry ***2543\nPaypal Tia Mowry tia@mowry.com\n";
37
+
38
+        String actual = presenter.toString(payment);
39
+        assertEquals(expected, actual);
40
+    }
41
+
42
+
43
+    //Create a test case to test the toStringByPayerName method of the PaymentPresenter class
44
+    @Test
45
+    public void toStringByPayerName(){
46
+        Payment[] payment = new Payment[1];
47
+        Payment paypal = new PayPal(120L, "Tamara Mowry", "tamara@mowry.com");
48
+
49
+        payment[0] = paypal;
50
+        PaymentPresenter presenter = new PaymentPresenter();
51
+
52
+        String expected = "Paypal Tamara Mowry tamara@mowry.com";
53
+        String actual = presenter.toStringByPayerName(payment);
54
+        assertEquals(expected, actual);
55
+
56
+    }
57
+
58
+
59
+
60
+    //When there are multiple payments, you need to sort by calling the PaymentSortByPayer class to sort,
61
+    // then build the string using the short description
62
+
63
+    @Test
64
+    public void paymentSortByPayer() {
65
+        Payment[] payment = new Payment[2];
66
+        Payment paypal = new PayPal(4L, "Tamara Mowry", "tamara@mowry.com");
67
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
68
+
69
+        payment[0] = paypal;
70
+        payment[1] = check;
71
+
72
+        PaymentPresenter presenter = new PaymentPresenter();
73
+        String expected = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***2543\n";
74
+
75
+        String actual = presenter.toStringByPayerName(payment);
76
+        assertEquals(expected, actual);
77
+    }
78
+
79
+    //Create a test case to test the toStringById method of the PaymentPresenter class
80
+
81
+    @Test
82
+    public void toStringById(){
83
+        Payment[] payment = new Payment[1];
84
+        Payment paypal = new PayPal(120L, "Tamara Mowry", "tamara@mowry.com");
85
+
86
+        payment[0] = paypal;
87
+        PaymentPresenter presenter = new PaymentPresenter();
88
+
89
+        String expected = "Paypal Tamara Mowry tamara@mowry.com";
90
+        String actual = presenter.toStringById(payment);
91
+        assertEquals(expected, actual);
92
+
93
+    }
94
+
95
+    //When there are multiple payments, you need to sort by creating a lambda to sort the payment by id,
96
+    // then build the string using the short description
97
+    @Test
98
+    public void sortByID() {
99
+        Payment[] payment = new Payment[2];
100
+        Payment paypal = new PayPal(120L, "Tamara Mowry", "tamara@mowry.com");
101
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
102
+
103
+        payment[0] = paypal;
104
+        payment[1] = check;
105
+
106
+        PaymentPresenter presenter = new PaymentPresenter();
107
+        String expected = "Check Tia Mowry ***2543\nPaypal Tamara Mowry tamara@mowry.com\n";
108
+
109
+        String actual = presenter.toStringById(payment);
110
+        assertEquals(expected, actual);
111
+    }
4 112
 }