ThuyKhong před 7 roky
rodič
revize
4898115e48

+ 8
- 0
pom.xml Zobrazit soubor

@@ -11,5 +11,13 @@
11 11
         <maven.compiler.source>1.8</maven.compiler.source>
12 12
         <maven.compiler.target>1.8</maven.compiler.target>
13 13
     </properties>
14
+    <dependencies>
15
+        <dependency>
16
+            <groupId>junit</groupId>
17
+            <artifactId>junit</artifactId>
18
+            <version>4.12</version>
19
+            <scope>test</scope>
20
+        </dependency>
21
+    </dependencies>
14 22
 
15 23
 </project>

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

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

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

@@ -0,0 +1,58 @@
1
+package com.zipcoder.payment;
2
+
3
+public class CreditCard implements Payment{
4
+    private long id;
5
+    private String payerName;
6
+    private String routingNumber;
7
+    private String accountNumber;
8
+
9
+
10
+    public CreditCard() {
11
+    }
12
+
13
+    public  void setID(Long id){
14
+        this.id = id;
15
+
16
+    }
17
+    @Override
18
+    public Long getID() {
19
+        return id;
20
+    }
21
+
22
+    public void setPayerName(String payerName) {
23
+        this.payerName = payerName;
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
+    @Override
43
+    public String getPayerName() {
44
+        return payerName;
45
+    }
46
+
47
+    @Override
48
+    public String getShortDescription() {
49
+
50
+        return "CC " + this.payerName + " ***" + this.accountNumber.substring(this.accountNumber.length() -4);
51
+    }
52
+
53
+
54
+    @Override
55
+    public int compareTo(Payment o) {
56
+        return this.getShortDescription().compareTo(o.getShortDescription());
57
+    }
58
+}

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

@@ -0,0 +1,52 @@
1
+package com.zipcoder.payment;
2
+
3
+public class PayPal implements Payment{
4
+    long id;
5
+    String payerName;
6
+    String email;
7
+
8
+    public PayPal(long id, String payerName, String email) {
9
+        this.id = id;
10
+        this.payerName = payerName;
11
+        this.email = email;
12
+    }
13
+
14
+    public PayPal() {
15
+    }
16
+
17
+    public void setId(long id){
18
+        this.id = id;
19
+    }
20
+    @Override
21
+    public Long getID() {
22
+        return id;
23
+    }
24
+
25
+    public  void setPayerName(String payerName){
26
+        this.payerName = payerName;
27
+    }
28
+
29
+    @Override
30
+    public String getPayerName() {
31
+        return payerName;
32
+    }
33
+
34
+    public void setEmail(String email){
35
+        this.email = email;
36
+    }
37
+
38
+    public String getEmail(){
39
+        return email;
40
+    }
41
+
42
+    @Override
43
+    public String getShortDescription() {
44
+        return "Paypal " + this.getPayerName() + " " + this.getEmail();
45
+    }
46
+
47
+
48
+    @Override
49
+    public int compareTo(Payment o) {
50
+        return this.getShortDescription().compareTo(o.getShortDescription());
51
+    }
52
+}

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

@@ -0,0 +1,13 @@
1
+package com.zipcoder.payment;
2
+
3
+public interface Payment extends Comparable<Payment>{
4
+    long id = 0L;
5
+    String payerName = "";
6
+    String getShortDescription = "";
7
+
8
+    Long getID();
9
+
10
+    String getPayerName();
11
+
12
+    String getShortDescription();
13
+}

+ 37
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Zobrazit soubor

@@ -1,6 +1,43 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 public class PaymentPresenter {
6
+    PaymentSortByPayer sortPayment = new PaymentSortByPayer();
7
+
8
+    public PaymentPresenter() {
9
+    }
10
+
11
+    public String toString(Payment[] payments){
12
+        String result = "";
13
+        if(payments.length>0){
14
+            payments = sortPayment.compareArray(payments);
15
+            for(Payment p:payments){
16
+                result+= p.getShortDescription() + "\n";
17
+            }
18
+        }
19
+        return result;
20
+    }
4 21
 
22
+    public String toStringByPayerName(Payment[] payments){
23
+        String result = "";
24
+        if(payments.length>0){
25
+            payments = sortPayment.compareArrayByName(payments);
26
+            for(Payment p:payments){
27
+                result+= p.getShortDescription() + "\n";
28
+            }
29
+        }
30
+        return result;
31
+    }
5 32
 
33
+    public String toStringById(Payment[] payments) {
34
+        String result = "";
35
+        if (payments.length > 0) {
36
+            payments = sortPayment.compareArrayByID(payments);
37
+            for (Payment p : payments) {
38
+                result += p.getShortDescription() + "\n";
39
+            }
40
+        }
41
+        return result;
42
+    }
6 43
 }

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

@@ -0,0 +1,58 @@
1
+package com.zipcoder.payment;
2
+
3
+import java.util.Comparator;
4
+
5
+public class PaymentSortByPayer implements Comparator<Payment> {
6
+
7
+    @Override
8
+    public int compare(Payment o1, Payment o2) {
9
+        return o1.compareTo(o2);
10
+    }
11
+
12
+    public int compareByName(Payment o1,Payment o2){
13
+        return o1.getPayerName().compareTo(o2.getPayerName());
14
+    }
15
+
16
+    public int compareByID(Payment o1, Payment o2){
17
+        return o1.getID().compareTo(o2.getID());
18
+    }
19
+
20
+    public Payment[] compareArray(Payment[] payments){
21
+        for (int i = 0; i < payments.length-1; i++) {
22
+            for (int j = 0; j < payments.length-1; j++) {
23
+                if(compare(payments[j],payments[j+1])>0){
24
+                    Payment temp = payments[j];
25
+                    payments[j] = payments[j+1];
26
+                    payments[j+1] = temp;
27
+                }
28
+            }
29
+        }
30
+        return payments;
31
+    }
32
+
33
+    public Payment[] compareArrayByID(Payment[] payments){
34
+        for (int i = 0; i < payments.length-1; i++) {
35
+            for (int j = 0; j < payments.length-1; j++) {
36
+                if(compareByID(payments[j],payments[j+1])>0){
37
+                    Payment temp = payments[j];
38
+                    payments[j] = payments[j+1];
39
+                    payments[j+1] = temp;
40
+                }
41
+            }
42
+        }
43
+        return payments;
44
+    }
45
+
46
+    public Payment[] compareArrayByName(Payment[] payments){
47
+        for (int i = 0; i < payments.length-1; i++) {
48
+            for (int j = 0; j < payments.length-1; j++) {
49
+                if(compareByName(payments[j],payments[j+1])>0){
50
+                    Payment temp = payments[j];
51
+                    payments[j] = payments[j+1];
52
+                    payments[j+1] = temp;
53
+                }
54
+            }
55
+        }
56
+        return payments;
57
+    }
58
+}

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

@@ -0,0 +1,72 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CheckTest {
7
+
8
+    Check check = new Check();
9
+    PayPal payPal = new PayPal();
10
+
11
+    @Test
12
+    public void testGetSetID(){
13
+
14
+        check.setID(1000L);
15
+        Long expected = 1000L;
16
+        Long actual = check.getID();
17
+
18
+        Assert.assertEquals(expected, actual);
19
+    }
20
+
21
+    @Test
22
+    public void testGetSetPayerName(){
23
+
24
+        check.setPayerName("Stephanie Tanner");
25
+        String expected = "Stephanie Tanner";
26
+        String actual = check.getPayerName();
27
+
28
+        Assert.assertEquals(expected, actual);
29
+    }
30
+
31
+    @Test
32
+    public void testGetSetRoutingNumber(){
33
+        check.setRoutingNumber("12345");
34
+        String expected = "12345";
35
+        String actual = check.getRoutingNumber();
36
+
37
+        Assert.assertEquals(expected,actual);
38
+    }
39
+
40
+    @Test
41
+    public void testGetSetAccountNumber(){
42
+        check.setAccountNumber("54321");
43
+        String expected = "54321";
44
+        String actual = check.getAccountNumber();
45
+
46
+        Assert.assertEquals(expected,actual);
47
+    }
48
+
49
+    @Test
50
+    public void testGetDescription(){
51
+        check.setPayerName("PayerName");
52
+        check.setRoutingNumber("123Routing");
53
+        check.setAccountNumber("12345678");
54
+        String expected = "Check PayerName ***5678";
55
+        String actual =check.getShortDescription();
56
+
57
+        Assert.assertEquals(expected,actual);
58
+    }
59
+
60
+    @Test
61
+    public void testCompareTo(){
62
+        check.setPayerName("PayerName");
63
+        check.setRoutingNumber("123Routing");
64
+        check.setAccountNumber("12345678");
65
+
66
+        payPal.setPayerName("PayerName");
67
+        payPal.setEmail("payer@email.com");
68
+
69
+        Assert.assertTrue(check.compareTo(payPal)<0);
70
+    }
71
+
72
+}

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

@@ -0,0 +1,75 @@
1
+package com.zipcoder.payment;
2
+
3
+
4
+import org.junit.Assert;
5
+import org.junit.Test;
6
+
7
+public class CreditCardTest {
8
+    CreditCard creditCard = new CreditCard();
9
+    Check check = new Check();
10
+
11
+    @Test
12
+    public void testSetGetID(){
13
+        creditCard.setID(1000L);
14
+
15
+        long expected = 1000L;
16
+        long actual = creditCard.getID();
17
+
18
+        Assert.assertEquals(expected, actual);
19
+    }
20
+
21
+
22
+    @Test
23
+    public void testSetGetPayerName(){
24
+        creditCard.setPayerName("Payer");
25
+
26
+        String expected = "Payer";
27
+        String actual = creditCard.getPayerName();
28
+
29
+        Assert.assertEquals(expected, actual);
30
+    }
31
+
32
+    @Test
33
+    public void testSetGetRoutingNumber(){
34
+        creditCard.setRoutingNumber("1234");
35
+
36
+        String expected = "1234";
37
+        String actual = creditCard.getRoutingNumber();
38
+
39
+        Assert.assertEquals(expected, actual);
40
+    }
41
+
42
+    @Test
43
+    public void testSetGetAccountNumber(){
44
+        creditCard.setAccountNumber("12345");
45
+
46
+        String expected = "12345";
47
+        String actual = creditCard.getAccountNumber();
48
+
49
+        Assert.assertEquals(expected, actual);
50
+    }
51
+
52
+    @Test
53
+    public void testDescription(){
54
+        creditCard.setPayerName("PayerName");
55
+        creditCard.setRoutingNumber("123Routing");
56
+        creditCard.setAccountNumber("12345678");
57
+        String expected = "CC PayerName ***5678";
58
+        String actual =creditCard.getShortDescription();
59
+
60
+        Assert.assertEquals(expected,actual);
61
+    }
62
+
63
+    @Test
64
+    public void testCompareTo(){
65
+        check.setPayerName("PayerName");
66
+        check.setRoutingNumber("123Routing");
67
+        check.setAccountNumber("12345678");
68
+
69
+        creditCard.setPayerName("PayerName");
70
+        creditCard.setAccountNumber("123Account");
71
+        creditCard.setAccountNumber("321Routing");
72
+
73
+        Assert.assertTrue(check.compareTo(creditCard)>0);
74
+    }
75
+}

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

@@ -0,0 +1,58 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class PayPalTest {
7
+    PayPal payPal = new PayPal();
8
+    Check check = new Check();
9
+
10
+    @Test
11
+    public void testSetGetID(){
12
+        payPal.setId(101010);
13
+        long expected = 101010L;
14
+        long actual = payPal.getID();
15
+
16
+        Assert.assertEquals(expected, actual);
17
+    }
18
+
19
+    @Test
20
+    public void testSetGetPayerName(){
21
+        payPal.setPayerName("Payer");
22
+        String expected = "Payer";
23
+        String actual = payPal.getPayerName();
24
+
25
+        Assert.assertEquals(expected, actual);
26
+    }
27
+
28
+    @Test
29
+    public void testSetGetEmail(){
30
+        payPal.setEmail("Payer@aol.com");
31
+        String expected = "Payer@aol.com";
32
+        String actual = payPal.getEmail();
33
+
34
+        Assert.assertEquals(expected, actual);
35
+    }
36
+
37
+    @Test
38
+    public void testGetDescription(){
39
+        payPal.setPayerName("PayerName");
40
+        payPal.setEmail("payer@email.com");
41
+        String expected = "Paypal PayerName payer@email.com";
42
+        String actual = payPal.getShortDescription();
43
+
44
+        Assert.assertEquals(expected,actual);
45
+    }
46
+
47
+    @Test
48
+    public void testCompareTo(){
49
+        check.setPayerName("PayerName");
50
+        check.setRoutingNumber("123Routing");
51
+        check.setAccountNumber("12345678");
52
+
53
+        payPal.setPayerName("PayerName");
54
+        payPal.setEmail("payer@email.com");
55
+
56
+        Assert.assertTrue(check.compareTo(payPal)<0);
57
+    }
58
+}

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

@@ -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
+
9
+    @Test
10
+    public void testToString_noPayment(){
11
+
12
+        Payment[] payments = new Payment[0];
13
+        PaymentPresenter presenter = new PaymentPresenter();
14
+        String expected = "";
15
+
16
+        String actual = presenter.toString(payments);
17
+
18
+        Assert.assertEquals(expected, actual);
19
+    }
20
+
21
+    @Test
22
+    public void testToString_multiPayments(){
23
+        Payment[] payments = new Payment[2];
24
+        Payment paypal = new PayPal(4L, "Tia Mowry", "tia@mowry.com");
25
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
26
+
27
+        payments[0] = paypal;
28
+        payments[1] = check;
29
+
30
+        PaymentPresenter presenter = new PaymentPresenter();
31
+        String expected = "Check Tia Mowry ***4551\nPaypal Tia Mowry tia@mowry.com\n";
32
+
33
+        String actual = presenter.toString(payments);
34
+        Assert.assertEquals(expected, actual);
35
+    }
36
+
37
+    @Test
38
+    public void testToStringByPayerName(){
39
+        Payment[] payments = new Payment[2];
40
+        Payment paypal = new PayPal(4L, "Tamara Mowry", "tamara@mowry.com");
41
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
42
+
43
+        payments[0] = paypal;
44
+        payments[1] = check;
45
+
46
+        PaymentPresenter presenter = new PaymentPresenter();
47
+        String expected = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
48
+
49
+        String actual = presenter.toStringByPayerName(payments);
50
+        Assert.assertEquals(expected, actual);
51
+    }
52
+
53
+    @Test
54
+    public void testToStringById(){
55
+        Payment[] payments = new Payment[2];
56
+        Payment paypal = new PayPal(120L, "Tamara Mowry", "tamara@mowry.com");
57
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
58
+
59
+        payments[0] = paypal;
60
+        payments[1] = check;
61
+
62
+        PaymentPresenter presenter = new PaymentPresenter();
63
+        String expected = "Check Tia Mowry ***4551\nPaypal Tamara Mowry tamara@mowry.com\n";
64
+
65
+        String actual = presenter.toStringById(payments);
66
+        Assert.assertEquals(expected, actual);
67
+    }
4 68
 }

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

@@ -0,0 +1,35 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class PaymentSortByPayerTest {
7
+    PaymentSortByPayer paymentSortByPayer = new PaymentSortByPayer();
8
+    CreditCard creditCard = new CreditCard();
9
+    Check check = new Check();
10
+
11
+
12
+    @Test
13
+    public void testCompare_1After2(){
14
+        check.setPayerName("xyz");
15
+        creditCard.setPayerName("ABC");
16
+
17
+        Assert.assertTrue(paymentSortByPayer.compareByName(check,creditCard)>0);
18
+    }
19
+
20
+    @Test
21
+    public void testCompare_same(){
22
+        creditCard.setID(100L);
23
+        check.setID(100L);
24
+
25
+        Assert.assertTrue(paymentSortByPayer.compareByID(creditCard,check)==0);
26
+    }
27
+
28
+    @Test
29
+    public void testCompare_1Before2(){
30
+        check.setPayerName("xyz");
31
+        creditCard.setPayerName("ABC");
32
+
33
+        Assert.assertTrue(paymentSortByPayer.compareByName(creditCard,check)<0);
34
+    }
35
+}