Browse Source

committing Interfacecomparable

chitraB 8 years ago
parent
commit
2d8349e5df

+ 20
- 0
pom.xml View File

@@ -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>junit</groupId>
25
+            <artifactId>junit</artifactId>
26
+            <version>RELEASE</version>
27
+            <scope>test</scope>
28
+        </dependency>
29
+    </dependencies>
10 30
 
11 31
 
12 32
 </project>

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

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

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

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

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

@@ -0,0 +1,48 @@
1
+package com.zipcoder.payment;
2
+
3
+public class PayPal implements Payment {
4
+    private long id;
5
+    private String payerName;
6
+    private 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 long getId() {
15
+        return id;
16
+    }
17
+
18
+    public void setId(long id) {
19
+        this.id = id;
20
+    }
21
+
22
+    public String getPayerName() {
23
+        return payerName;
24
+    }
25
+
26
+    public String getShortDescription() {
27
+        return ("PayPal " + getPayerName() + " " + getEmail());
28
+    }
29
+
30
+    public void setPayerName(String payerName) {
31
+        this.payerName = payerName;
32
+    }
33
+
34
+    public String getEmail() {
35
+        return email;
36
+    }
37
+
38
+    public void setEmail(String email) {
39
+        this.email = email;
40
+    }
41
+
42
+    public int compareTo(Payment payPal) {
43
+        return (this.getShortDescription().compareTo(payPal.getShortDescription()));
44
+
45
+    }
46
+
47
+
48
+}

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

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

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

@@ -1,6 +1,40 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 public class PaymentPresenter {
4 6
 
5 7
 
8
+
9
+    public String toString(Payment[] payments) {
10
+        Arrays.sort(payments);
11
+        StringBuilder sb = new StringBuilder();
12
+        for(Payment obj: payments){
13
+            sb.append(obj.getShortDescription()+"\n");
14
+        }
15
+        return sb.toString();
16
+    }
17
+
18
+    public String toStringName(Payment[] payments)
19
+    {
20
+        Arrays.sort(payments, new PaymentSortByPayer());
21
+        StringBuilder br=new StringBuilder();
22
+        for(int i=0;i<payments.length;i++)
23
+        {
24
+
25
+            br.append(payments[i].getShortDescription()+"\n");
26
+        }
27
+        return br.toString();
28
+    }
29
+    public String toStringById(Payment[] payments)
30
+    {
31
+        Arrays.sort(payments, (Payment o1,Payment o2) -> Long.compare(o1.getId(),o2.getId()));
32
+        StringBuilder br;
33
+        br = new StringBuilder();
34
+        for(int i=0;i<payments.length;i++)
35
+        {
36
+            br.append(payments[i].getShortDescription()+"\n");
37
+        }
38
+        return br.toString();
39
+    }
6 40
 }

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

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

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

@@ -0,0 +1,43 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Test;
4
+
5
+import static junit.framework.TestCase.assertEquals;
6
+
7
+public class CheckTest {
8
+
9
+    @Test
10
+    public void testId(){
11
+        long expected = 0;
12
+        Check check = new Check(1, "Laurie", "123", "44");
13
+        check.setId(0);
14
+        long actual = check.getId();
15
+        assertEquals(expected, actual);
16
+    }
17
+
18
+    @Test
19
+    public void testpayerName(){
20
+        String expected = "Kevin";
21
+        Check check = new Check(2, "Oscar", "334", "6767");
22
+        check.setPayerName("Kevin");
23
+        String actual = check.getPayerName();
24
+        assertEquals(expected, actual);
25
+    }
26
+
27
+    @Test
28
+    public void testRoutingNum(){
29
+        String expected = "111";
30
+        Check check = new Check(3, "Maya", "676", "9988");
31
+        check.setRoutingNumber("111");
32
+        String actual = check.getRoutingNumber();
33
+        assertEquals(expected, actual);
34
+    }
35
+    @Test
36
+    public void testAccountNum(){
37
+        String expected = "222";
38
+        Check check = new Check(4, "Jay", "5656", "6734");
39
+        check.setAccountNumber("222");
40
+        String actual = check.getAccountNumber();
41
+        assertEquals(expected, actual);
42
+    }
43
+}

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

@@ -0,0 +1,56 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Test;
4
+
5
+import static junit.framework.TestCase.assertEquals;
6
+
7
+public class CreditCardTest {
8
+
9
+    @Test
10
+    public void testGetter(){
11
+        long expected = 3;
12
+        CreditCard card = new CreditCard(3, "Dave", "3424", 3, 2021);
13
+        card.setId();
14
+        long actual = card.getId();
15
+        assertEquals(expected, actual);
16
+
17
+    }
18
+
19
+
20
+    @Test
21
+    public void testPayerName(){
22
+        String expected = "John";
23
+        CreditCard card = new CreditCard(5, "Wendy", "3455", 7, 2020);
24
+        card.setPayerName("John");
25
+        String actual = card.getPayerName();
26
+        assertEquals(expected, actual);
27
+
28
+    }
29
+    @Test
30
+    public void testNumber(){
31
+        String expected = "123";
32
+        CreditCard card = new CreditCard(1, "Bob", "2323", 8, 2020);
33
+        card.setNumber("123");
34
+        String actual = card.getNumber();
35
+        assertEquals(expected, actual);
36
+
37
+    }
38
+
39
+    @Test
40
+    public void testExpiredMonth() {
41
+        int expected = 9;
42
+        CreditCard card = new CreditCard(2, "Freddy", "2343", 7, 2021);
43
+        card.setExpiredMonth(9);
44
+        int actual = card.getExpiredMonth();
45
+        assertEquals(expected, actual);
46
+
47
+    }
48
+    @Test
49
+    public void testExpiredYear(){
50
+        int expected = 2016;
51
+        CreditCard card = new CreditCard(3, "Leo", "2343", 6, 2019);
52
+        card.setExpiredYear(2016);
53
+        int actual = card.getExpiredYear();
54
+        assertEquals(expected, actual);
55
+    }
56
+}

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

@@ -0,0 +1,58 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Test;
4
+
5
+import static junit.framework.TestCase.assertEquals;
6
+import static junit.framework.TestCase.assertTrue;
7
+
8
+public class PayPalTest {
9
+
10
+    @Test
11
+    public void testGetter(){
12
+        long expected = 1;
13
+        PayPal payPal = new PayPal(3, "kevin", "K@yahoo.com");
14
+        payPal.setId(1);
15
+        long actual = payPal.getId();
16
+        assertEquals(expected, actual);
17
+    }
18
+    @Test
19
+    public void testPayerName(){
20
+        String expected = "Marie";
21
+        PayPal payPal = new PayPal(2, "Marie", "m@yahoo.com");
22
+        payPal.setPayerName("Marie");
23
+        String actual = payPal.getPayerName();
24
+        assertEquals(expected, actual);
25
+    }
26
+    @Test
27
+    public void testEmail(){
28
+        String expected = "abs@yahoo.com";
29
+        PayPal payPal = new PayPal(1, "john", "j@yahoo.com");
30
+        payPal.setEmail("abs@yahoo.com");
31
+        String actual = payPal.getEmail();
32
+        assertEquals(expected, actual);
33
+    }
34
+    @Test
35
+    public void testCompareToGreaterThan(){
36
+        int expected = 0;
37
+        Payment payPal = new PayPal(4, "Tia Mowry", "tia@mowry.com");
38
+        Payment check = new Check(6, "Tia Mowry", "2323", "4556");
39
+        int actual = payPal.compareTo(check);
40
+        assertTrue(actual > expected);
41
+    }
42
+    @Test
43
+    public void testCompareToEqual(){
44
+        int expected = 0;
45
+        Payment payPal = new PayPal(7, "Matt", "m@yahoo.com");
46
+        Payment payPal2 = new PayPal(7, "Matt", "m@yahoo.com");
47
+        int actual = payPal.compareTo(payPal2);
48
+        assertTrue(actual == expected);
49
+    }
50
+    @Test
51
+    public void testCompareToLessThan(){
52
+        int expected = 0;
53
+        Payment payPal = new PayPal(3, "Kay", "k@yahoo.com");
54
+        Payment check = new Check(2, "Leo", "55666", "5678");
55
+        int actual = payPal.compareTo(check);
56
+    }
57
+
58
+}

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

@@ -1,4 +1,67 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import org.junit.Test;
4
+
5
+import static junit.framework.TestCase.assertEquals;
6
+
3 7
 public class PaymentPresenterTest {
8
+    @Test
9
+    public void test1() {
10
+        Payment[] payments = new Payment[0];
11
+        PaymentPresenter presenter = new PaymentPresenter();
12
+        String expected = "";
13
+
14
+        String actual = presenter.toString(payments);
15
+
16
+        assertEquals(expected, actual);
17
+    }
18
+
19
+    @Test
20
+    public void test2() {
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
+        assertEquals(expected, actual);
33
+    }
34
+
35
+    @Test
36
+    public void test3() {
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.toStringName(payments);
48
+        assertEquals(expected, actual);
49
+    }
50
+
51
+    @Test
52
+    public void test4() {
53
+
54
+        Payment[] payments = new Payment[2];
55
+        Payment paypal = new PayPal(120L, "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 = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
63
+
64
+        String actual = presenter.toStringById(payments);
65
+        assertEquals(expected, actual);
66
+    }
4 67
 }

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

@@ -0,0 +1,36 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Test;
4
+
5
+import static junit.framework.TestCase.assertTrue;
6
+
7
+public class PaymentSortByPayerTest {
8
+
9
+    @Test
10
+    public void testCompareGreaterthan(){
11
+      int expected = 0;
12
+      PaymentSortByPayer comparator = new PaymentSortByPayer();
13
+      Payment payer1 = new PayPal(2, "Laurie", "laurie@mowry.com");
14
+      Payment payer2 = new Check(3, "Anna Mowry", "3344", "5544");
15
+      int actual = comparator.compare(payer1, payer2);
16
+      assertTrue( actual > expected);
17
+    }
18
+    @Test
19
+    public void testCompareEquals(){
20
+        int expected = 0;
21
+        PaymentSortByPayer comparator = new PaymentSortByPayer();
22
+        Payment payer1 = new PayPal(2, "Laurie", "L@yahoo.com");
23
+        Payment payer2 = new Check(2, "Laurie", "3345", "7456");
24
+        int actual = comparator.compare(payer1, payer2);
25
+        assertTrue(actual == expected);
26
+    }
27
+    @Test
28
+    public void testCompareLessThan(){
29
+        int expected = 0;
30
+        PaymentSortByPayer comparator = new PaymentSortByPayer();
31
+        Payment payer1 = new PayPal(8, "Neil", "n@yahoo.com");
32
+        Payment payer2 = new PayPal(2, "Zoey", "z@yahoo.com");
33
+        int actual = comparator.compare(payer1, payer2);
34
+
35
+    }
36
+}