donnaj 6 år sedan
förälder
incheckning
e0c1dad756

+ 12
- 0
pom.xml Visa fil

@@ -7,6 +7,18 @@
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>
10 22
     <dependencies>
11 23
         <dependency>
12 24
             <groupId>junit</groupId>

+ 3
- 0
src/main/java/com/zipcoder/payment/Check.java Visa fil

@@ -50,5 +50,8 @@ public class Check implements Payment {
50 50
     }
51 51
 
52 52
 
53
+    public int compareTo(Payment o) {
54
+        return this.getShortDescription().compareTo(o.getShortDescription());
53 55
 
56
+    }
54 57
 }

+ 5
- 0
src/main/java/com/zipcoder/payment/CreditCard.java Visa fil

@@ -62,4 +62,9 @@ public class CreditCard implements Payment {
62 62
     public String getShortDescription() {
63 63
         return "CC" + " " + payerName + " " + number.substring(number.length()-4, number.length()) + " " + expiredMonth + "/" + expiredYear;
64 64
     }
65
+
66
+    public int compareTo(Payment o) {
67
+        return this.getShortDescription().compareTo(o.getShortDescription());
68
+
69
+    }
65 70
 }

+ 5
- 0
src/main/java/com/zipcoder/payment/PayPal.java Visa fil

@@ -38,4 +38,9 @@ public class PayPal implements Payment {
38 38
     public String getShortDescription() {
39 39
         return "PayPal" + " " + payerName + " " + email;
40 40
     }
41
+
42
+    public int compareTo(Payment o) {
43
+        return this.getShortDescription().compareTo(o.getShortDescription());
44
+
45
+    }
41 46
 }

+ 3
- 1
src/main/java/com/zipcoder/payment/Payment.java Visa fil

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

+ 28
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Visa fil

@@ -1,6 +1,34 @@
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[] payments) {
10
+        StringBuilder result = new StringBuilder();
11
+        Arrays.sort(payments);
12
+        for (int i = 0; i < payments.length; i++) {
13
+            result.append(payments[i].getShortDescription() + "\n");
14
+        }
15
+        return result.toString();
16
+    }
17
+
18
+    public String toStringByPayerName(Payment[] payments) {
19
+        StringBuilder result = new StringBuilder();
20
+        Arrays.sort(payments, new PaymentSortByPayer());
21
+        for (int i = 0; i < payments.length; i++) {
22
+            result.append(payments[i].getShortDescription() + "\n");
23
+        }
24
+        return result.toString();
25
+    }
26
+    public String toStringById(Payment[] payments) {
27
+        StringBuilder result = new StringBuilder();
28
+        Arrays.sort(payments, Comparator.comparing(Payment::getId));
29
+        for (int i = 0; i < payments.length; i++) {
30
+            result.append(payments[i].getShortDescription() + "\n");
31
+        }
32
+        return result.toString();
33
+    }
6 34
 }

+ 11
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java Visa fil

@@ -0,0 +1,11 @@
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
+}

+ 13
- 1
src/test/java/com/zipcoder/payment/CheckTest.java Visa fil

@@ -1,11 +1,18 @@
1 1
 package com.zipcoder.payment;
2 2
 
3 3
 import org.junit.Assert;
4
+import org.junit.Before;
4 5
 import org.junit.Test;
5 6
 
6 7
 public class CheckTest {
7
-    Check test = new Check(2L, "donna", "987654321", "1234567890");
8
+    Check test;
9
+    Check test1;
8 10
 
11
+    @Before
12
+    public void before() {
13
+        test = new Check(2L, "donna", "987654321", "1234567890");
14
+        test1 = new Check(7L, "marie", "987654301", "1234567800");
15
+    }
9 16
 
10 17
     @Test
11 18
     public void getIdTest() {
@@ -42,4 +49,9 @@ public class CheckTest {
42 49
         Assert.assertEquals(expected, actual);
43 50
 
44 51
     }
52
+    @Test
53
+    public void compareToTest() {
54
+        int actual = test.compareTo(test1);
55
+        Assert.assertTrue(actual < 0);
56
+    }
45 57
 }

+ 13
- 1
src/test/java/com/zipcoder/payment/CreditCardTest.java Visa fil

@@ -1,12 +1,19 @@
1 1
 package com.zipcoder.payment;
2 2
 
3 3
 import org.junit.Assert;
4
+import org.junit.Before;
4 5
 import org.junit.Test;
5 6
 
6 7
 public class CreditCardTest {
7
-    CreditCard test = new CreditCard(1L, "donna", "1234567890", 10, 2020);
8 8
 
9
+    CreditCard test;
10
+    CreditCard test1;
9 11
 
12
+    @Before
13
+    public void before() {
14
+    test = new CreditCard(1L, "donna", "1234567890", 10, 2020);
15
+    test1 = new CreditCard(6L, "marie", "1234507890", 1, 2022);
16
+}
10 17
     @Test
11 18
     public void getIdTest() {
12 19
         Long expected = 1L;
@@ -47,6 +54,11 @@ public class CreditCardTest {
47 54
         Assert.assertEquals(expected, actual);
48 55
 
49 56
     }
57
+    @Test
58
+    public void compareToTest() {
59
+        int actual = test.compareTo(test1);
60
+        Assert.assertTrue(actual < 0);
61
+    }
50 62
 
51 63
 }
52 64
 

+ 13
- 1
src/test/java/com/zipcoder/payment/PayPalTest.java Visa fil

@@ -1,11 +1,18 @@
1 1
 package com.zipcoder.payment;
2 2
 
3 3
 import org.junit.Assert;
4
+import org.junit.Before;
4 5
 import org.junit.Test;
5 6
 
6 7
 public class PayPalTest {
7
-    PayPal test = new PayPal(3L, "donna", "donna@jacobs.com");
8
+    PayPal test;
9
+    PayPal test1;
8 10
 
11
+    @Before
12
+    public void before() {
13
+        test = new PayPal(3L, "donna", "donna@jacobs.com");
14
+        test1 = new PayPal(8L, "marie", "marie@jacobs.com");
15
+    }
9 16
 
10 17
     @Test
11 18
     public void getIdTest() {
@@ -36,4 +43,9 @@ public class PayPalTest {
36 43
         Assert.assertEquals(expected, actual);
37 44
 
38 45
     }
46
+    @Test
47
+    public void compareToTest() {
48
+        int actual = test.compareTo(test1);
49
+        Assert.assertTrue(actual < 0);
50
+    }
39 51
 }

+ 64
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Visa fil

@@ -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 toStringPaymentTest(){
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 toStringMultiplePaymentTest() {
21
+        Payment[] payments = new Payment[2];
22
+        Payment paypal = new PayPal(4L, "Tia Mowry", "tia@mowry.com");
23
+        Payment check = new Check(81L, "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 ***4551\nPayPal Tia Mowry tia@mowry.com\n";
30
+
31
+        String actual = presenter.toString(payments);
32
+        Assert.assertEquals(expected, actual);
33
+
34
+    }
35
+
36
+    @Test
37
+    public void toStringByPayerName() {
38
+        Payment[] payments = new Payment[2];
39
+        Payment paypal = new PayPal(4L, "Tamara Mowry", "tamara@mowry.com");
40
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
41
+
42
+        payments[0] = paypal;
43
+        payments[1] = check;
44
+
45
+        PaymentPresenter presenter = new PaymentPresenter();
46
+        String expected = "PayPal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
47
+
48
+        String actual = presenter.toStringByPayerName(payments);
49
+        Assert.assertEquals(expected, actual);
50
+    }
51
+
52
+    @Test
53
+    public void toStringByIdTest() {
54
+        Payment[] payments = new Payment[2];
55
+        Payment paypal = new PayPal(81L, "Tamara Mowry", "tamara@mowry.com");
56
+        Payment check = new Check(120L, "Tia Mowry", "11432543", "134344551");
57
+
58
+        payments[0] = paypal;
59
+        payments[1] = check;
60
+
61
+        PaymentPresenter presenter = new PaymentPresenter();
62
+        String expected = "PayPal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
63
+
64
+        String actual = presenter.toStringById(payments);
65
+        Assert.assertEquals(expected, actual);
66
+    }
67
+
4 68
 }

+ 25
- 0
src/test/java/com/zipcoder/payment/PaymentSortByPayerTest.java Visa fil

@@ -0,0 +1,25 @@
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
+    CreditCard creditCard;
9
+    Check check;
10
+    PaymentSortByPayer test;
11
+
12
+    @Before
13
+    public void setUp() {
14
+        creditCard = new CreditCard(6L, "marie", "1234507890", 1, 2022);
15
+        check = new Check(2L, "donna", "987654321", "1234567890");
16
+        test = new PaymentSortByPayer();
17
+    }
18
+
19
+    @Test
20
+    public void compareTest() {
21
+        int actual = test.compare(creditCard, check);
22
+        Assert.assertTrue(actual > 0);
23
+
24
+    }
25
+}