Rachelle před 8 roky
rodič
revize
5777355228

+ 12
- 1
pom.xml Zobrazit soubor

@@ -3,7 +3,18 @@
3 3
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 4
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 5
     <modelVersion>4.0.0</modelVersion>
6
-
6
+    <build>
7
+        <plugins>
8
+            <plugin>
9
+                <groupId>org.apache.maven.plugins</groupId>
10
+                <artifactId>maven-compiler-plugin</artifactId>
11
+                <configuration>
12
+                    <source>1.8</source>
13
+                    <target>1.8</target>
14
+                </configuration>
15
+            </plugin>
16
+        </plugins>
17
+    </build>
7 18
     <groupId>com.zipcoder</groupId>
8 19
     <artifactId>payment</artifactId>
9 20
     <version>1.0-SNAPSHOT</version>

+ 11
- 0
src/main/java/com/zipcoder/payment/Check.java Zobrazit soubor

@@ -9,6 +9,13 @@ public class Check implements Payment {
9 9
 
10 10
     public Check(){}
11 11
 
12
+    public Check(Long id, String payerName, String routingName, String accountNumber){
13
+        this.id = id;
14
+        this.payerName = payerName;
15
+        this.routingName = routingName;
16
+        this.accountNumber = accountNumber;
17
+    }
18
+
12 19
     public Check(String payerName, String accountNumber){
13 20
         this.payerName = payerName;
14 21
         this.accountNumber = accountNumber;
@@ -54,4 +61,8 @@ public class Check implements Payment {
54 61
         String lastFour = accountNumber.substring(accountNumber.length()-4);
55 62
         return lastFour;
56 63
     }
64
+
65
+    public int compareTo(Payment o) {
66
+        return this.getShortDescription().compareTo(o.getShortDescription());
67
+    }
57 68
 }

+ 3
- 0
src/main/java/com/zipcoder/payment/CreditCard.java Zobrazit soubor

@@ -71,4 +71,7 @@ public class CreditCard implements Payment {
71 71
         return lastFour;
72 72
     }
73 73
 
74
+    public int compareTo(Payment o) {
75
+        return this.getShortDescription().compareTo(o.getShortDescription());
76
+    }
74 77
 }

+ 10
- 0
src/main/java/com/zipcoder/payment/PayPal.java Zobrazit soubor

@@ -12,6 +12,12 @@ public class PayPal implements Payment {
12 12
         this.email = email;
13 13
     }
14 14
 
15
+    public PayPal(Long id, String payerName, String email){
16
+        this.id = id;
17
+        this.payerName = payerName;
18
+        this.email = email;
19
+    }
20
+
15 21
     public Long getID() {
16 22
         return id;
17 23
     }
@@ -39,4 +45,8 @@ public class PayPal implements Payment {
39 45
     public String getShortDescription() {
40 46
         return ("Paypal " + getPayerName() + " " + email);
41 47
     }
48
+
49
+    public int compareTo(Payment o) {
50
+        return this.getShortDescription().compareTo(o.getShortDescription());
51
+    }
42 52
 }

+ 1
- 1
src/main/java/com/zipcoder/payment/Payment.java Zobrazit soubor

@@ -1,6 +1,6 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
-public interface Payment {
3
+public interface Payment extends Comparable<Payment> {
4 4
     Long getID();
5 5
 
6 6
     String getPayerName();

+ 41
- 1
src/main/java/com/zipcoder/payment/PaymentPresenter.java Zobrazit soubor

@@ -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
 
8
+    public String toString(Payment[] payments) {
9
+        String result = "";
10
+        if (payments.length > 0) {
11
+           String[] descriptionArray = getArrayOfDescriptions(payments);
12
+           Arrays.sort(descriptionArray);
13
+           result = returnDescriptionsAsString(descriptionArray);
14
+        }
15
+        return result;
16
+    }
17
+
18
+    public String[] getArrayOfDescriptions(Payment[] payments) {
19
+        String[] descriptionArray = new String[payments.length];
20
+        for (int i = 0; i < payments.length; i++) {
21
+            descriptionArray[i] = payments[i].getShortDescription();
22
+            }
23
+        return descriptionArray;
24
+    }
25
+
26
+    public String returnDescriptionsAsString(String[] descriptions){
27
+        StringBuilder stringBuilder = new StringBuilder();
28
+        for(String element: descriptions){
29
+            stringBuilder.append(element + "\n");
30
+        }
31
+        return stringBuilder.toString();
32
+    }
33
+
34
+    public String toStringByPayerName(Payment[] payments){
35
+        Arrays.sort(payments, new PaymentSortByPayer());
36
+        return returnDescriptionsAsString(getArrayOfDescriptions(payments));
37
+    }
38
+
39
+    public String toStringById(Payment[] payments){
40
+        Comparator<Payment> compareById = Comparator.comparing(Payment::getID);
41
+        //Comparator<Payment> compareById = (payment1,payment2) -> payment1.getID().compareTo(payment2.getID());
42
+        Arrays.sort(payments,compareById);
43
+        return returnDescriptionsAsString(getArrayOfDescriptions(payments));
44
+    }
5 45
 
6
-}
46
+}

+ 10
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java Zobrazit soubor

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

+ 16
- 0
src/test/java/com/zipcoder/payment/CheckTest.java Zobrazit soubor

@@ -64,4 +64,20 @@ public class CheckTest {
64 64
 
65 65
         Assert.assertEquals(expected,check.getLastFourDigits());
66 66
     }
67
+
68
+    @Test
69
+    public void compareToDifferentCheckTest(){
70
+        Check check = new Check("John Jacob", "12345");
71
+        Check check1 = new Check("Jingleheimer Schmidt", "45678");
72
+
73
+        Assert.assertTrue(check.compareTo(check1) > 0);
74
+    }
75
+
76
+    @Test
77
+    public void compareToCheckPayPalTest(){
78
+        Check check = new Check("John Jacob", "12345");
79
+        PayPal payPal = new PayPal();
80
+
81
+        Assert.assertTrue(payPal.compareTo(check) > 0);
82
+    }
67 83
 }

+ 16
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java Zobrazit soubor

@@ -73,4 +73,20 @@ public class CreditCardTest {
73 73
 
74 74
         Assert.assertEquals(expected,creditCard.getLastFourDigits());
75 75
     }
76
+
77
+    @Test
78
+    public void compareToDifferentCCTest(){
79
+        CreditCard creditCard = new CreditCard("John Jacob", "1234567890123456", 12, 2018);
80
+        CreditCard creditCard1 = new CreditCard("Jingleheimer Schmidt", "0987654321098765",04,2019);
81
+
82
+        Assert.assertTrue(creditCard.compareTo(creditCard1) > 0);
83
+    }
84
+
85
+    @Test
86
+    public void compareToCCCheckTest(){
87
+        CreditCard creditCard = new CreditCard("John Jacob", "1234567890123456", 12, 2018);
88
+        Check check = new Check("John Jacob", "12345");
89
+
90
+        Assert.assertTrue(creditCard.compareTo(check) < 0);
91
+    }
76 92
 }

+ 16
- 0
src/test/java/com/zipcoder/payment/PayPalTest.java Zobrazit soubor

@@ -43,4 +43,20 @@ public class PayPalTest {
43 43
 
44 44
         Assert.assertEquals(expected, payPal.getShortDescription());
45 45
     }
46
+
47
+    @Test
48
+    public void compareToDifferentPayPalTest(){
49
+        PayPal payPal = new PayPal();
50
+        PayPal payPal1 = new PayPal();
51
+
52
+        Assert.assertTrue(payPal.compareTo(payPal1) == 0);
53
+    }
54
+
55
+    @Test
56
+    public void compareToCCPayPalTest(){
57
+        CreditCard creditCard = new CreditCard("John Jacob", "1234567890123456", 12, 2018);
58
+        PayPal payPal = new PayPal();
59
+
60
+        Assert.assertTrue(payPal.compareTo(creditCard) > 0);
61
+    }
46 62
 }

+ 61
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Zobrazit soubor

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

+ 55
- 0
src/test/java/com/zipcoder/payment/PaymentSortByPayerTest.java Zobrazit soubor

@@ -0,0 +1,55 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class PaymentSortByPayerTest {
7
+    @Test
8
+    public void compareDifferentCCTest() {
9
+        CreditCard creditCard = new CreditCard("John Jacob", "1234567890123456", 12, 2018);
10
+        CreditCard creditCard1 = new CreditCard("Jingleheimer Schmidt", "0987654321098765", 04, 2019);
11
+
12
+        Assert.assertTrue(creditCard.compareTo(creditCard1) > 0);
13
+    }
14
+
15
+    @Test
16
+    public void compareCCCheckTest() {
17
+        CreditCard creditCard = new CreditCard("John Jacob", "1234567890123456", 12, 2018);
18
+        Check check = new Check("John Jacob", "12345");
19
+
20
+        Assert.assertTrue(creditCard.compareTo(check) < 0);
21
+    }
22
+
23
+    @Test
24
+    public void compareToDifferentCheckTest(){
25
+        Check check = new Check("John Jacob", "12345");
26
+        Check check1 = new Check("Jingleheimer Schmidt", "45678");
27
+
28
+        Assert.assertTrue(check.compareTo(check1) > 0);
29
+    }
30
+
31
+    @Test
32
+    public void compareToCheckPayPalTest(){
33
+        Check check = new Check("John Jacob", "12345");
34
+        PayPal payPal = new PayPal();
35
+
36
+        Assert.assertTrue(payPal.compareTo(check) > 0);
37
+    }
38
+
39
+    @Test
40
+    public void compareToDifferentPayPalTest(){
41
+        PayPal payPal = new PayPal();
42
+        PayPal payPal1 = new PayPal();
43
+
44
+        Assert.assertTrue(payPal.compareTo(payPal1) == 0);
45
+    }
46
+
47
+    @Test
48
+    public void compareToCCPayPalTest(){
49
+        CreditCard creditCard = new CreditCard("John Jacob", "1234567890123456", 12, 2018);
50
+        PayPal payPal = new PayPal();
51
+
52
+        Assert.assertTrue(payPal.compareTo(creditCard) > 0);
53
+    }
54
+}
55
+