Browse Source

almost done

thulasi 7 years ago
parent
commit
c33e5a19b9

+ 8
- 0
pom.xml View File

@@ -7,6 +7,14 @@
7 7
     <groupId>com.zipcoder</groupId>
8 8
     <artifactId>payment</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>RELEASE</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
     <properties>
11 19
         <maven.compiler.source>1.8</maven.compiler.source>
12 20
         <maven.compiler.target>1.8</maven.compiler.target>

+ 64
- 0
src/main/java/com/zipcoder/payment/Check.java View File

@@ -0,0 +1,64 @@
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() {
11
+    }
12
+
13
+    public Check(long id, String name, String routingNumber, String accountNumber) {
14
+        this.id = id;
15
+        this.payerName = name;
16
+        this.routingNumber = routingNumber;
17
+        this.accountNumber = accountNumber;
18
+    }
19
+
20
+    @Override
21
+    public long getId() {
22
+        return this.id;
23
+    }
24
+
25
+    @Override
26
+    public int compareTo(Payment payment) {
27
+        return this.getShortDescription().compareTo(payment.getShortDescription());
28
+    }
29
+
30
+    @Override
31
+    public String getPayerName() {
32
+        return this.payerName;
33
+    }
34
+
35
+    @Override
36
+    public String getShortDescription() {
37
+        return String.format("Check %s ***%s", payerName, accountNumber.substring(accountNumber.length()-4, accountNumber.length()));
38
+    }
39
+
40
+    public void setId(long id) {
41
+        this.id = id;
42
+    }
43
+
44
+    public void setPayerName(String name) {
45
+        this.payerName = name;
46
+    }
47
+
48
+    public void setRoutingNumber(String number) {
49
+        this.routingNumber = number;
50
+    }
51
+
52
+    public String getRoutingNumber() {
53
+        return this.routingNumber;
54
+    }
55
+
56
+    public void setAccountNumber(String number) {
57
+        this.accountNumber = number;
58
+    }
59
+
60
+    public String getAccountNumber() {
61
+        return this.accountNumber;
62
+    }
63
+
64
+}

+ 70
- 0
src/main/java/com/zipcoder/payment/CreditCard.java View File

@@ -0,0 +1,70 @@
1
+package com.zipcoder.payment;
2
+
3
+public class CreditCard implements Payment{
4
+    private long id;
5
+    private String payerName;
6
+    private String number;
7
+    private int expiredMonth;
8
+    private int expiredYear;
9
+
10
+    public CreditCard(){
11
+
12
+    }
13
+
14
+    public CreditCard(long id, String payerName) {
15
+        this.id = id;
16
+        this.payerName = payerName;
17
+    }
18
+
19
+    @Override
20
+    public String getPayerName() {
21
+        return this.payerName;
22
+    }
23
+
24
+    @Override
25
+    public String getShortDescription() {
26
+        return String.format("CC %s %s %d/%d", payerName, number.substring(number.length() - 4, number.length()), expiredMonth, expiredYear);
27
+    }
28
+
29
+    public void setId(long id) {
30
+        this.id = id;
31
+    }
32
+
33
+    @Override
34
+    public long getId() {
35
+        return this.id;
36
+    }
37
+
38
+    public void setPayerName(String name) {
39
+        this.payerName = name;
40
+    }
41
+
42
+    public void setNumber(String number) {
43
+        this.number = number;
44
+    }
45
+
46
+    public String getNumber() {
47
+        return this.number;
48
+    }
49
+
50
+    public void setExpiredMonth(int month) {
51
+        this.expiredMonth = month;
52
+    }
53
+
54
+    public int getExpiredMonth() {
55
+        return this.expiredMonth;
56
+    }
57
+
58
+    public void setExpiredYear(int year) {
59
+        this.expiredYear = year;
60
+    }
61
+
62
+    public int getExpiredYear() {
63
+        return this.expiredYear;
64
+    }
65
+
66
+    @Override
67
+    public int compareTo(Payment payment) {
68
+        return this.getShortDescription().compareTo(payment.getShortDescription());
69
+    }
70
+}

+ 55
- 0
src/main/java/com/zipcoder/payment/PayPal.java View File

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

+ 7
- 0
src/main/java/com/zipcoder/payment/Payment.java View File

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

+ 33
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java View File

@@ -1,6 +1,39 @@
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
+    Comparator<Payment> comparator = (Payment p1, Payment p2)  -> {
9
+        Long p1ID = p1.getId();
10
+        Long p2ID = p2.getId();
11
+        return p1ID.compareTo(p2ID);
12
+    };
13
+
14
+    public String stringGenerator(Payment[] payments){
15
+        StringBuilder paymentSB = new StringBuilder();
16
+        for (Payment payment : payments){
17
+            paymentSB.append(payment.getShortDescription() + "\n");
18
+        }
19
+        return paymentSB.toString();
20
+
21
+    }
22
+
23
+    public String toString(Payment[] payments) {
24
+       // Arrays.sort(payments);
25
+        return stringGenerator(payments);
26
+    }
27
+
28
+    public String toStringById(Payment[] payments) {
29
+        Arrays.sort(payments, comparator);
30
+        return stringGenerator(payments);
31
+    }
32
+
33
+    public String toStringByPayerName(Payment[] payments) {
34
+        PaymentSortByPayer parser = new PaymentSortByPayer();
35
+        Arrays.sort(payments, parser);
36
+        return stringGenerator(payments);
37
+    }
5 38
 
6 39
 }

+ 11
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java View File

@@ -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
+    @Override
8
+    public int compare(Payment payment1, Payment payment2) {
9
+        return payment1.getShortDescription().compareTo(payment2.getShortDescription());
10
+    }
11
+}

+ 34
- 0
src/test/java/com/zipcoder/payment/CheckTest.java View File

@@ -0,0 +1,34 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CheckTest  {
7
+    @Test
8
+    public void testSetGetId(){
9
+        Check check = new Check();
10
+        check.setId(2);
11
+        Assert.assertEquals(2, check.getId());
12
+    }
13
+
14
+    @Test
15
+    public void testSetGetName(){
16
+        Check check = new Check();
17
+        check.setPayerName("thulasi");
18
+        Assert.assertEquals("thulasi", check.getPayerName());
19
+    }
20
+
21
+    @Test
22
+    public void testSetGetRoutingNumber(){
23
+        Check check = new Check();
24
+        check.setRoutingNumber("3545");
25
+        Assert.assertEquals("3545", check.getRoutingNumber());
26
+    }
27
+
28
+    @Test
29
+    public void testSetGetAccountNumber(){
30
+        Check check = new Check();
31
+        check.setAccountNumber("4535");
32
+        Assert.assertEquals("4535", check.getAccountNumber());
33
+    }
34
+}

+ 43
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java View File

@@ -0,0 +1,43 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CreditCardTest {
7
+
8
+    @Test
9
+    public void testSetGetId(){
10
+
11
+        CreditCard card = new CreditCard();
12
+        card.setId(34);
13
+        Assert.assertEquals(34, card.getId());
14
+    }
15
+
16
+    @Test
17
+    public void testSetGetName(){
18
+        CreditCard card = new CreditCard();
19
+        card.setPayerName("thulasi");
20
+        Assert.assertEquals("thulasi", card.getPayerName());
21
+    }
22
+
23
+    @Test
24
+    public void testSetGetNumber(){
25
+        CreditCard card = new CreditCard();
26
+        card.setNumber("3555363");
27
+        Assert.assertEquals(555, card.getNumber());
28
+    }
29
+
30
+    @Test
31
+    public void testSetGetExpiredMonth(){
32
+        CreditCard card = new CreditCard();
33
+        card.setExpiredMonth(3);
34
+        Assert.assertEquals(3, card.getExpiredMonth());
35
+    }
36
+
37
+    @Test
38
+    public void testSetGetExpiredYear(){
39
+        CreditCard card = new CreditCard();
40
+        card.setExpiredYear(2003);
41
+        Assert.assertEquals(2003, card.getExpiredYear());
42
+    }
43
+}

+ 29
- 0
src/test/java/com/zipcoder/payment/PayPalTest.java View File

@@ -0,0 +1,29 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class PayPalTest {
7
+
8
+    @Test
9
+    public void testSetGetId(){
10
+        PayPal payPal = new PayPal();
11
+        payPal.setId(2);
12
+        Assert.assertEquals(2, payPal.getId());
13
+    }
14
+
15
+    @Test
16
+    public void testSetGetName(){
17
+        PayPal payPal = new PayPal();
18
+        payPal.setPayerName("thulasi");
19
+        Assert.assertEquals("thulasi", payPal.getPayerName());
20
+    }
21
+
22
+    @Test
23
+    public void testSetGetEmail(){
24
+        PayPal payPal = new PayPal();
25
+        payPal.setEmail("3545");
26
+        Assert.assertEquals("3545", payPal.getEmail());
27
+    }
28
+
29
+}

+ 64
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java View File

@@ -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 testNoPayment(){
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 testMultiplePayments(){
21
+
22
+        Payment[] payments = new Payment[2];
23
+        Payment paypal = new PayPal(4L, "Tia Mowry", "tia@mowry.com");
24
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
25
+
26
+        payments[0] = paypal;
27
+        payments[1] = check;
28
+
29
+        PaymentPresenter presenter = new PaymentPresenter();
30
+        String expected = "Check Tia Mowry ***4551\nPaypal Tia Mowry tia@mowry.com\n";
31
+
32
+        String actual = presenter.toString(payments);
33
+        Assert.assertEquals(expected, actual);
34
+    }
35
+
36
+    @Test
37
+    public void testToStringByPayerName(){
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 testToStringById(){
54
+        Payment[] payments = new Payment[2];
55
+        Payment paypal = new PayPal(4L, "Tamara Mowry", "tamara@mowry.com");
56
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
57
+
58
+        payments[0] = paypal;
59
+        payments[1] = check;
60
+
61
+        PaymentPresenter presenter = new PaymentPresenter();
62
+        String expected = "Check Tia Mowry ***4551\nPaypal Tamara Mowry tamara@mowry.com\n";
63
+
64
+        String actual = presenter.toStringById(payments);
65
+        Assert.assertEquals(expected, actual);
66
+    }
67
+
4 68
 }

+ 34
- 0
src/test/java/com/zipcoder/payment/PaymentSortByPayerTest.java View File

@@ -0,0 +1,34 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class PaymentSortByPayerTest {
7
+
8
+    @Test
9
+    public void testCompare1() {
10
+        PaymentSortByPayer sorter = new PaymentSortByPayer();
11
+        Payment p1 = new Check(165, "ajay", "3242", "5223");
12
+        Payment p2 = new PayPal(56, "reshma", "hjhjghj@fdf.com");
13
+        int expected = -1;
14
+        Assert.assertEquals(expected, sorter.compare(p1, p2));
15
+    }
16
+
17
+    @Test
18
+    public void testCompare2() {
19
+        PaymentSortByPayer sorter = new PaymentSortByPayer();
20
+        Payment p1 = new Check(165, "ramya", "3242", "5223");
21
+        Payment p2 = new PayPal(56, "ramya", "hjhjghj@fdf.com");
22
+        int expected = 0;
23
+        Assert.assertEquals(expected, sorter.compare(p1, p2));
24
+    }
25
+
26
+    @Test
27
+    public void testCompare3() {
28
+        PaymentSortByPayer sorter = new PaymentSortByPayer();
29
+        Payment p1 = new Check(165, "wright", "3242", "5223");
30
+        Payment p2 = new PayPal(56, "reshma", "hjhjghj@fdf.com");
31
+        int expected = 1;
32
+        Assert.assertEquals(expected, sorter.compare(p1, p2));
33
+    }
34
+}