Shivam Patel 8 gadus atpakaļ
vecāks
revīzija
1e4e09653c

+ 20
- 0
pom.xml Parādīt failu

@@ -7,6 +7,26 @@
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>
22
+    <dependencies>
23
+        <dependency>
24
+            <groupId>org.junit.jupiter</groupId>
25
+            <artifactId>junit-jupiter-api</artifactId>
26
+            <version>RELEASE</version>
27
+            <scope>test</scope>
28
+        </dependency>
29
+    </dependencies>
10 30
 
11 31
 
12 32
 </project>

+ 103
- 0
src/main/java/com/zipcoder/payment/Check.java Parādīt failu

@@ -0,0 +1,103 @@
1
+package com.zipcoder.payment;
2
+
3
+public class Check implements Payment {
4
+
5
+    private long id;
6
+    private String routingNum;
7
+    private String accNum;
8
+    private String name;
9
+    private String last4;
10
+
11
+    public Check(long id, String name, String routingNum, String accNum) {
12
+        this.id = id;
13
+        this.name = name;
14
+        this.routingNum = routingNum;
15
+        //this.accNum = accNum;
16
+        this.accNum = getlast4(accNum);
17
+    }
18
+
19
+    public Check() {
20
+    }
21
+
22
+    public Check(long id, String name){
23
+        this.id = id;
24
+        this.name = name;
25
+
26
+    }
27
+
28
+
29
+    public long getId() {
30
+        return id;
31
+    }
32
+
33
+    public void setId(long id) {
34
+        this.id = id;
35
+
36
+    }
37
+
38
+    public String getAccNum() {
39
+        return accNum;
40
+    }
41
+
42
+    public void setAccNum(String accNum) {
43
+        this.accNum = accNum;
44
+    }
45
+
46
+    public String getRoutingNum() {
47
+        return routingNum;
48
+    }
49
+
50
+    public void setRoutingNum(String routingNum) {
51
+        this.routingNum = routingNum;
52
+    }
53
+
54
+
55
+    public void setPayerName(String name) {
56
+        this.name = name;
57
+
58
+    }
59
+
60
+    public String getPayerName() {
61
+        return name;
62
+    }
63
+
64
+    /*public void setLastFour(String num4){
65
+        this.last4 = num4;
66
+    }
67
+
68
+    public String getlastFour(){
69
+
70
+         return last4.substring(accNum.length()-4);
71
+
72
+
73
+    }*/
74
+
75
+    public String getlast4(String accNum){
76
+
77
+        return accNum.replace(accNum.substring(0,accNum.length()-4),"");
78
+
79
+
80
+    }
81
+
82
+
83
+
84
+
85
+    public String getShortDescription() {
86
+        return "Check " + getPayerName() + " " + "***" + getAccNum();
87
+    }
88
+
89
+
90
+    public int compareTo(Payment o) {
91
+
92
+
93
+        if (this.getShortDescription().compareTo(o.getShortDescription()) == 0) {
94
+            return 0;
95
+        } else if (this.getShortDescription().compareTo(o.getShortDescription()) < 0) {
96
+            return -1;
97
+        } else if (this.getShortDescription().compareTo(o.getShortDescription()) > 0) {
98
+            return 1;
99
+        } else {
100
+            return 101010;
101
+        }
102
+    }
103
+}

+ 102
- 0
src/main/java/com/zipcoder/payment/CreditCard.java Parādīt failu

@@ -0,0 +1,102 @@
1
+package com.zipcoder.payment;
2
+
3
+public class CreditCard implements Payment {
4
+    private long id;
5
+    private int month;
6
+    private int year;
7
+    private String number;
8
+    private String name;
9
+
10
+    public CreditCard(long id, int month, int year, String number, String name){
11
+            this.id = id;
12
+            this.month = month;
13
+            this.year = year;
14
+            this.number = number;
15
+            this.name = name;
16
+    }
17
+    public CreditCard(long id){
18
+        this.id = id;
19
+    }
20
+    public CreditCard(long id,String name,String number){
21
+        this.id=id;
22
+        this.name = name;
23
+        this.number = number;
24
+
25
+    }
26
+    public CreditCard(String name){
27
+        this.name = name;
28
+    }
29
+    public CreditCard(){
30
+
31
+    }
32
+
33
+
34
+    public long getId() {
35
+        return id;
36
+    }
37
+    public void setId(long id){
38
+        this.id = id;
39
+
40
+    }
41
+    public void setPayerName(String name){
42
+        this.name = name;
43
+
44
+    }
45
+    public String getPayerName() {
46
+        return name;
47
+    }
48
+
49
+    public void setExpiredMonth(int month){
50
+        this.month = month;
51
+
52
+    }
53
+    public int getExpiredMonth(){
54
+
55
+        return month;
56
+
57
+    }
58
+    public void setExpiredYear(int year){
59
+        this.year = year;
60
+
61
+    }
62
+
63
+    public int getExpiredYear(){
64
+
65
+        return year;
66
+
67
+    }
68
+    public String getNumber(){
69
+
70
+        return number;
71
+
72
+    }
73
+    public void setNumber(String number){
74
+        this.number = number;
75
+
76
+    }
77
+
78
+
79
+    public String getShortDescription() {
80
+        return "CC " + getPayerName() +" "+ getNumber() +" "+ getExpiredMonth()+"/"+getExpiredYear();
81
+    }
82
+
83
+
84
+    public int compareTo(Payment o) {
85
+
86
+
87
+        if(this.getShortDescription().compareTo(o.getShortDescription())== 0){
88
+            return 0;
89
+        }
90
+        else if(this.getShortDescription().compareTo(o.getShortDescription()) < 0){
91
+            return -1;
92
+        }
93
+        else if(this.getShortDescription().compareTo(o.getShortDescription())>0){
94
+            return 1;
95
+        }
96
+        else{
97
+            return 101010;
98
+        }
99
+
100
+    }
101
+}
102
+

+ 13
- 0
src/main/java/com/zipcoder/payment/Payment.java Parādīt failu

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

+ 55
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Parādīt failu

@@ -1,6 +1,61 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 public class PaymentPresenter {
4 6
 
5 7
 
8
+    public String toString(Payment[] pay){
9
+           Arrays.sort(pay);
10
+        StringBuilder build = new StringBuilder();
11
+
12
+           for(Payment o : pay){
13
+               o.getId();
14
+               o.getPayerName();
15
+               o.getShortDescription();
16
+
17
+
18
+               build.append(o.getShortDescription() + "\n");
19
+
20
+           }
21
+        return build.toString();
22
+
23
+    }
24
+
25
+    public String toStringByPayerName(Payment[] pay){
26
+
27
+        Arrays.sort(pay, new PaymentSortByPayer());
28
+        StringBuilder build = new StringBuilder();
29
+
30
+        for(Payment o : pay){
31
+            o.getId();
32
+            o.getPayerName();
33
+            o.getShortDescription();
34
+
35
+
36
+            build.append(o.getShortDescription() + "\n");
37
+
38
+        }
39
+        return build.toString();
40
+
41
+
42
+    }
43
+    public String toStringById(Payment[] pay){
44
+
45
+        Arrays.sort(pay, (Payment o1,Payment o2) -> Long.compare(o1.getId(),o2.getId()));    //LAMBDA FUNCTION
46
+
47
+
48
+        StringBuilder build = new StringBuilder();
49
+
50
+        for(Payment o : pay){
51
+
52
+
53
+            build.append(o.getShortDescription() + "\n");
54
+
55
+        }
56
+        return build.toString();
57
+    }
58
+
59
+
60
+
6 61
 }

+ 14
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java Parādīt failu

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

+ 66
- 0
src/main/java/com/zipcoder/payment/Paypal.java Parādīt failu

@@ -0,0 +1,66 @@
1
+package com.zipcoder.payment;
2
+
3
+public class Paypal implements Payment{
4
+    private long id;
5
+    private String name;
6
+    private String email;
7
+
8
+public Paypal(long id, String name, String email){
9
+    this.id = id;
10
+    this.name = name;
11
+    this.email = email;
12
+
13
+}
14
+public Paypal(){
15
+
16
+
17
+}
18
+
19
+    public long getId() {
20
+        return id;
21
+    }
22
+    public void setId(long id){
23
+        this.id=id;
24
+
25
+    }
26
+
27
+    public String getPayerName() {
28
+        return name;
29
+    }
30
+
31
+    public void setPayerName(String name){
32
+        this.name = name;
33
+
34
+    }
35
+
36
+    public String getEmail() {
37
+        return email;
38
+    }
39
+
40
+    public void setEmail(String email){
41
+        this.email = email;
42
+
43
+    }
44
+
45
+    public String getShortDescription() {
46
+        return "Paypal " + getPayerName() + " " + getEmail();
47
+    }
48
+
49
+
50
+    public int compareTo(Payment o) {
51
+
52
+        if(this.getShortDescription().compareTo(o.getShortDescription())== 0){
53
+            return 0;
54
+        }
55
+        else if(this.getShortDescription().compareTo(o.getShortDescription()) < 0){
56
+            return -1;
57
+        }
58
+        else if(this.getShortDescription().compareTo(o.getShortDescription())>0){
59
+            return 1;
60
+        }
61
+        else{
62
+            return 101010;
63
+        }
64
+
65
+    }
66
+}

+ 113
- 0
src/test/java/com/zipcoder/payment/CheckTest.java Parādīt failu

@@ -0,0 +1,113 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.jupiter.api.Test;
4
+
5
+import static org.junit.jupiter.api.Assertions.*;
6
+
7
+class CheckTest {
8
+    Check check = new Check();
9
+    Paypal pay = new Paypal();
10
+    CreditCard credit = new CreditCard();
11
+
12
+    @Test
13
+    public void compareToCheck(){
14
+        int expected = 0;
15
+
16
+        int actual = check.compareTo(check);
17
+
18
+
19
+        assertEquals(expected,actual);
20
+
21
+    }
22
+    @Test
23
+    public void compareToPP(){
24
+        int expected = -1;
25
+
26
+        int actual = check.compareTo(pay);
27
+
28
+        assertEquals(expected,actual);
29
+
30
+    }
31
+
32
+    @Test
33
+    public void compareToCC(){
34
+        int expected = 1;
35
+
36
+        int actual = check.compareTo(credit);
37
+
38
+        assertEquals(expected,actual);
39
+
40
+    }
41
+
42
+
43
+    @Test
44
+    public void creditCardTestGetID(){
45
+
46
+        long expected = 12345678;
47
+        check.setId(expected);
48
+        long actual = check.getId();
49
+
50
+        assertEquals(expected,actual);
51
+
52
+    }
53
+
54
+    @Test
55
+    public void creditPayerNameTest(){
56
+
57
+        String expected = "shiva";
58
+        check.setPayerName(expected);
59
+        String actual = check.getPayerName();
60
+        assertEquals(expected,actual);
61
+
62
+    }
63
+
64
+
65
+
66
+    @Test
67
+    public void getRouting(){
68
+        String expected = "3111212";
69
+        check.setRoutingNum(expected);
70
+        String actual = check.getRoutingNum();
71
+        assertEquals(expected,actual);
72
+
73
+
74
+    }
75
+
76
+    @Test
77
+    void getLast4(){
78
+        String expected = "5959";
79
+        String accnum = "12345959";
80
+        check.setAccNum(accnum);
81
+
82
+        String actual = check.getlast4(accnum);
83
+
84
+        assertEquals(expected,actual);
85
+
86
+    }
87
+
88
+    @Test
89
+    public void getAcc(){
90
+
91
+        String expected = "32324";
92
+        check.setRoutingNum(expected);
93
+        String actual = check.getRoutingNum();
94
+        assertEquals(expected,actual);
95
+    }
96
+
97
+    @Test
98
+    void getShortDescriptionTest() {
99
+        String name = "Tia Mowry";
100
+        String rout = "4551";
101
+
102
+        check.setAccNum(rout);
103
+        check.setPayerName(name);
104
+        String expected = "Check Tia Mowry ***4551";
105
+        String actual = check.getShortDescription();
106
+        assertEquals(expected,actual);
107
+
108
+    }
109
+
110
+
111
+
112
+
113
+}

+ 113
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java Parādīt failu

@@ -0,0 +1,113 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.jupiter.api.Assertions;
4
+import org.junit.jupiter.api.Test;
5
+
6
+import static org.junit.jupiter.api.Assertions.*;
7
+
8
+class CreditCardTest {
9
+    CreditCard credit = new CreditCard();
10
+    Check check = new Check();
11
+    Paypal pay = new Paypal();
12
+
13
+
14
+    @Test
15
+    public void compareToCC(){
16
+        int expected = -1;
17
+        CreditCard card1 = new CreditCard();
18
+        card1.setPayerName("A");
19
+        card1.setNumber("1234");
20
+
21
+
22
+        int actual = card1.compareTo(credit);
23
+
24
+
25
+        assertEquals(expected,actual);
26
+
27
+    }
28
+    @Test
29
+    public void compareToCheck(){
30
+        int expected = -1;
31
+
32
+        int actual = credit.compareTo(check);
33
+
34
+        assertEquals(expected,actual);
35
+
36
+    }
37
+    @Test
38
+    public void compareToPP(){
39
+        int expected = -1;
40
+
41
+       int actual = credit.compareTo(pay);
42
+
43
+        assertEquals(expected,actual);
44
+    }
45
+
46
+    @Test
47
+    public void creditCardTestGetID(){
48
+
49
+        long expected = 12345678;
50
+        credit.setId(expected);
51
+        long actual = credit.getId();
52
+
53
+        Assertions.assertEquals(expected,actual);
54
+
55
+
56
+    }
57
+
58
+    @Test
59
+    public void creditPayerNameTest(){
60
+
61
+        String expected = "shiva";
62
+        credit.setPayerName(expected);
63
+        String actual = credit.getPayerName();
64
+        assertEquals(expected,actual);
65
+
66
+    }
67
+    @Test
68
+    public void creditPayerNumberTest(){
69
+
70
+        String expected = "8573";
71
+        credit.setNumber(expected);
72
+        String actual = credit.getNumber();
73
+        assertEquals(expected,actual);
74
+
75
+    }
76
+
77
+    @Test
78
+    public void creditExpireMonthTest(){
79
+
80
+        int expected = 11;
81
+        credit.setExpiredMonth(expected);
82
+        int actual = credit.getExpiredMonth();
83
+        assertEquals(expected,actual);
84
+
85
+    }
86
+
87
+    @Test
88
+    public void creditExpiredYearTest() {
89
+
90
+        int expected = 2022;
91
+        credit.setExpiredYear(expected);
92
+        int actual = credit.getExpiredYear();
93
+        assertEquals(expected, actual);
94
+
95
+    }
96
+    @Test
97
+    public void creditShortDescriptionTest(){
98
+
99
+        String name = "Tia Mowry";
100
+        String number = "4551";
101
+        int expiredMo = 10;
102
+        int expiredYe = 2019;
103
+        credit.setPayerName(name);
104
+        credit.setNumber(number);
105
+        credit.setExpiredMonth(expiredMo);
106
+        credit.setExpiredYear(expiredYe);
107
+        String expected = "CC " + name + " " + number + " " + expiredMo + "/" + expiredYe;
108
+        String actual = credit.getShortDescription();
109
+        assertEquals(expected,actual);
110
+
111
+    }
112
+
113
+}

+ 80
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Parādīt failu

@@ -1,4 +1,84 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import org.junit.jupiter.api.Test;
4
+
5
+import static org.junit.jupiter.api.Assertions.assertEquals;
6
+
3 7
 public class PaymentPresenterTest {
8
+
9
+    @Test
10
+    public void testToString() {
11
+        Payment[] payments = new Payment[0];
12
+        PaymentPresenter presenter = new PaymentPresenter();
13
+        String expected = "";
14
+
15
+        String actual = presenter.toString(payments);
16
+
17
+        assertEquals(expected, actual);
18
+
19
+    }
20
+
21
+    /*paypal.setId(4L); paypal.setPayerName("Tia Mowry"); paypal.setEmail("tia@mowry.com");
22
+            check.setId(81L); check.setPayerName("Tia Mowry"); check.setAccNum("11432543");
23
+            check.setRoutingNum("4551");*/
24
+    @Test
25
+    public void testMultiple() {
26
+
27
+
28
+        Payment[] payments = new Payment[2];
29
+        Payment paypal = new Paypal(4L, "Tia Mowry", "tia@mowry.com");
30
+        Payment check = new Check(81L,"Tia Mowry", "11432543", "134344551");
31
+
32
+
33
+
34
+        payments[0] = paypal;
35
+        payments[1] = check;
36
+
37
+
38
+
39
+        PaymentPresenter presenter = new PaymentPresenter();
40
+        String expected = "Check Tia Mowry ***4551\nPaypal Tia Mowry tia@mowry.com\n";
41
+
42
+        String actual = presenter.toString(payments);
43
+        assertEquals(expected, actual);
44
+
45
+
46
+    }
47
+
48
+    @Test
49
+
50
+    public void toStringByPayerName() {
51
+
52
+        Payment[] payments = new Payment[2];
53
+        Payment paypal = new Paypal(4L, "Tamara Mowry", "tamara@mowry.com");
54
+        Payment check = new Check(81L, "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.toStringByPayerName(payments);
63
+        assertEquals(expected, actual);
64
+
65
+
66
+    }
67
+    @Test
68
+    public void toStringbyId() {
69
+        Payment[] payment = new Payment[2];
70
+        Payment paypal = new Paypal(120L, "Tamara Mowry", "tamara@mowry.com");
71
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
72
+
73
+        payment[0] = paypal;
74
+        payment[1] = check;
75
+
76
+        PaymentPresenter presenter = new PaymentPresenter();
77
+        String expected = "Check Tia Mowry ***4551\nPaypal Tamara Mowry tamara@mowry.com\n";
78
+
79
+        String actual = presenter.toStringById(payment);
80
+        assertEquals(expected, actual);
81
+    }
82
+
4 83
 }
84
+

+ 90
- 0
src/test/java/com/zipcoder/payment/PaypalTest.java Parādīt failu

@@ -0,0 +1,90 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.jupiter.api.Assertions;
4
+import org.junit.jupiter.api.Test;
5
+
6
+import static org.junit.jupiter.api.Assertions.*;
7
+
8
+class PaypalTest {
9
+    Paypal pay = new Paypal();
10
+    Check check = new Check();
11
+    CreditCard credit = new CreditCard();
12
+
13
+    @Test
14
+    public void compareToCheck(){
15
+        int expected = 1;
16
+
17
+        int actual = pay.compareTo(check);
18
+
19
+        assertEquals(expected,actual);
20
+
21
+    }
22
+    @Test
23
+    public void compareToPP(){
24
+        int expected = 0;
25
+
26
+        int actual = 0;
27
+
28
+
29
+        assertEquals(expected,actual);
30
+
31
+    }
32
+
33
+    @Test
34
+    public void compareToCC(){
35
+        int expected = 1;
36
+
37
+        int actual = pay.compareTo(credit);
38
+
39
+        assertEquals(expected,actual);
40
+
41
+    }
42
+
43
+
44
+    @Test
45
+    void getId() {
46
+        long expected = 12345678;
47
+        pay.setId(expected);
48
+        long actual = pay.getId();
49
+
50
+        Assertions.assertEquals(expected,actual);
51
+
52
+    }
53
+
54
+
55
+    @Test
56
+    void getPayerName() {
57
+
58
+        String expected = "shiva";
59
+        pay.setPayerName(expected);
60
+        String actual = pay.getPayerName();
61
+        assertEquals(expected,actual);
62
+    }
63
+
64
+
65
+    @Test
66
+    void getEmail(){
67
+
68
+        String expected = "shiva@p.com";
69
+        pay.setEmail(expected);
70
+        String actual = pay.getEmail();
71
+        assertEquals(actual,expected);
72
+
73
+    }
74
+
75
+
76
+    @Test
77
+    void getShortDescription() {
78
+
79
+        String name = "Tia Mowry";
80
+        String email = "tia@mowry.com";
81
+        pay.setEmail(email);
82
+        pay.setPayerName(name);
83
+        String expected = "Paypal " + name + " " + email;
84
+        String actual = pay.getShortDescription();
85
+        assertEquals(expected,actual);
86
+
87
+
88
+
89
+    }
90
+}