nafis nibir 8 år sedan
förälder
incheckning
6e91e4bbce

+ 10
- 1
src/main/java/com/zipcoder/payment/Check.java Visa fil

@@ -16,6 +16,15 @@ public class Check implements Payment{
16 16
         this.routingNumber = routingNumber;
17 17
         this.accountNumber = accountNumber;
18 18
     }
19
+
20
+    public Check(Long id, String payerName, String routingNumber, String accountNumber){
21
+        this.id = id;
22
+        this.payerName = payerName;
23
+        this.routingNumber = routingNumber;
24
+        this.accountNumber = accountNumber;
25
+    }
26
+
27
+
19 28
     public String getRoutingNumber() {
20 29
         return routingNumber;
21 30
     }
@@ -50,7 +59,7 @@ public class Check implements Payment{
50 59
     }
51 60
 
52 61
     public String getShortDescription(){
53
-        String result = "Check " + getPayerName() + " ***" + lastFour(getAccountNumber());
62
+        String result = "Check " + getPayerName() + " ***" + lastFour(getAccountNumber()) + "\n";
54 63
         return result;
55 64
     }
56 65
 

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

@@ -59,7 +59,7 @@ public class CreditCard implements Payment {
59 59
     }
60 60
 
61 61
     public String getShortDescription(){
62
-        String result = "CC " + getPayerName() + " " + lastFour(getNumber()) + " " + getExpiredMonth() + "/" + getExpiredYear();
62
+        String result = "CC " + getPayerName() + " " + lastFour(getNumber()) + " " + getExpiredMonth() + "/" + getExpiredYear() + "\n";
63 63
         return result;
64 64
     }
65 65
 

+ 9
- 1
src/main/java/com/zipcoder/payment/PayPal.java Visa fil

@@ -15,6 +15,14 @@ public class PayPal implements Payment {
15 15
         this.email = email;
16 16
     }
17 17
 
18
+    public PayPal(Long id, String payerName, String email){
19
+        this.id = id;
20
+        this.payerName = payerName;
21
+        this.email = email;
22
+    }
23
+
24
+
25
+
18 26
     public String getEmail() {
19 27
         return email;
20 28
     }
@@ -41,7 +49,7 @@ public class PayPal implements Payment {
41 49
     }
42 50
 
43 51
     public String getShortDescription() {
44
-        String result = "Paypal " + getPayerName() + " " + getEmail();
52
+        String result = "Paypal " + getPayerName() + " " + getEmail() + "\n";
45 53
         return result;
46 54
     }
47 55
 

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

@@ -1,6 +1,31 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 public class PaymentPresenter {
4 6
 
7
+    public String toString(Payment[] payments){
8
+        Arrays.sort(payments, new PaymentSortDescription());
9
+        return createArrayString(payments);
10
+    }
11
+
12
+    public String toStringByPayerName(Payment[] payments){
13
+        Arrays.sort(payments, new PaymentSortPayer());
14
+        return createArrayString(payments);
15
+    }
16
+
17
+    public String toStringById(Payment[] payments){
18
+        Arrays.sort(payments, new PaymentSortID());
19
+        return createArrayString(payments);
20
+    }
21
+
22
+    private String createArrayString(Payment[] payments){
23
+        StringBuilder result = new StringBuilder();
24
+        for(Payment element: payments){
25
+            result.append(element.getShortDescription());
26
+        }
27
+        return result.toString();
28
+    }
29
+
5 30
 
6 31
 }

+ 8
- 0
src/main/java/com/zipcoder/payment/PaymentSortDescription.java Visa fil

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

+ 10
- 0
src/main/java/com/zipcoder/payment/PaymentSortID.java Visa fil

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

+ 9
- 0
src/main/java/com/zipcoder/payment/PaymentSortPayer.java Visa fil

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

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

@@ -63,7 +63,7 @@ public class CheckTest {
63 63
     public void getShortDescriptionTest(){
64 64
         Check test = new Check("Nafis", "123456789", "1234567890");
65 65
 
66
-        String expected = "Check Nafis ***7890";
66
+        String expected = "Check Nafis ***7890\n";
67 67
         String actual = test.getShortDescription();
68 68
         Assert.assertEquals(expected, actual);
69 69
     }

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

@@ -66,7 +66,7 @@ public class CreditCardTest {
66 66
     public void getShortDescriptionTest(){
67 67
         CreditCard test = new CreditCard("Nafis", "1111222233331234", 9, 2018);
68 68
 
69
-        String expected = "CC Nafis 1234 9/2018";
69
+        String expected = "CC Nafis 1234 9/2018\n";
70 70
         String actual = test.getShortDescription();
71 71
         Assert.assertEquals(expected, actual);
72 72
     }
@@ -85,7 +85,7 @@ public class CreditCardTest {
85 85
     public void constructorTest(){
86 86
         CreditCard test = new CreditCard("Nafis", "1111222233331234", 9, 2018);
87 87
 
88
-        String expected = "CC Nafis 1234 9/2018";
88
+        String expected = "CC Nafis 1234 9/2018\n";
89 89
         String actual = test.getShortDescription();
90 90
         Assert.assertEquals(expected, actual);
91 91
     }

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

@@ -42,10 +42,19 @@ public class PayPalTest {
42 42
     public void getShortDescriptionTest(){
43 43
         PayPal test = new PayPal("Nafis", "nafisnibir@gmail.com");
44 44
 
45
-        String expected = "Paypal Nafis nafisnibir@gmail.com";
45
+        String expected = "Paypal Nafis nafisnibir@gmail.com\n";
46 46
         String actual = test.getShortDescription();
47 47
         Assert.assertEquals(expected, actual);
48 48
     }
49 49
 
50
+    @Test
51
+    public void compareToTest(){
52
+        CreditCard test1 = new CreditCard("Nafis", "1111222233331234", 9, 2018);
53
+        PayPal test2 = new PayPal("Nafis", "nafisnibir@gmail.com");
54
+
55
+        Assert.assertTrue(test1.compareTo(test2) < 0);
56
+
57
+    }
58
+
50 59
 
51 60
 }

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

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

+ 42
- 0
src/test/java/com/zipcoder/payment/PaymentSortPayerTest.java Visa fil

@@ -0,0 +1,42 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+import static org.junit.Assert.*;
7
+
8
+public class PaymentSortPayerTest {
9
+
10
+    @Test
11
+    public void compareTest(){
12
+
13
+        PaymentSortPayer test = new PaymentSortPayer();
14
+
15
+        CreditCard test1 = new CreditCard("Nafis", "1111222233331234", 9, 2018);
16
+        PayPal test2 = new PayPal("Nafis", "nafisnibir@gmail.com");
17
+
18
+        Assert.assertTrue(test.compare(test1, test2) == 0);
19
+    }
20
+
21
+    @Test
22
+    public void compareTest1(){
23
+
24
+        PaymentSortPayer test = new PaymentSortPayer();
25
+
26
+        CreditCard test1 = new CreditCard("Nafis", "1111222233331234", 9, 2018);
27
+        PayPal test2 = new PayPal("Afis", "nafisnibir@gmail.com");
28
+
29
+        Assert.assertTrue(test.compare(test1, test2) > 0);
30
+    }
31
+
32
+    @Test
33
+    public void compareTest2(){
34
+
35
+        PaymentSortPayer test = new PaymentSortPayer();
36
+
37
+        CreditCard test1 = new CreditCard("Nafis", "1111222233331234", 9, 2018);
38
+        PayPal test2 = new PayPal("Zafis", "nafisnibir@gmail.com");
39
+
40
+        Assert.assertTrue(test.compare(test1, test2) < 0);
41
+    }
42
+}