#38 dmjpub

Open
dmjpub wants to merge 2 commits from dmjpub/InterfaceComparableLab:master into master

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

+ 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
+    Long id;
5
+    String payerName;
6
+    String number;
7
+    int expiredMonth;
8
+    int expiredYear;
9
+
10
+    public CreditCard(Long id, String payerName, String number, int expiredMonth, int expiredYear) {
11
+        this.id = id;
12
+        this.payerName = payerName;
13
+        this.number = number;
14
+        this.expiredMonth = expiredMonth;
15
+        this.expiredYear = expiredYear;
16
+    }
17
+
18
+    public void setId(Long id) {
19
+        this.id = id;
20
+    }
21
+
22
+    public void setPayerName(String payerName) {
23
+        this.payerName = payerName;
24
+    }
25
+
26
+    public String getNumber() {
27
+        return number;
28
+    }
29
+
30
+    public void setNumber(String number) {
31
+        this.number = number;
32
+    }
33
+
34
+    public int getExpiredMonth() {
35
+        return expiredMonth;
36
+    }
37
+
38
+    public void setExpiredMonth(int expireMonth) {
39
+        this.expiredMonth = expireMonth;
40
+    }
41
+
42
+    public int getExpiredYear() {
43
+        return expiredYear;
44
+    }
45
+
46
+    public void setExpiredYear(int expireYear) {
47
+        this.expiredYear = expireYear;
48
+    }
49
+
50
+    public CreditCard(Long id) {
51
+        this.id = id;
52
+    }
53
+
54
+    public Long getId() {
55
+        return id;
56
+    }
57
+
58
+    public String getPayerName() {
59
+        return payerName;
60
+    }
61
+
62
+    public String getShortDescription() {
63
+        return "CC" + " " + payerName + " " + number.substring(number.length()-4, number.length()) + " " + expiredMonth + "/" + expiredYear;
64
+    }
65
+
66
+    public int compareTo(Payment o) {
67
+        return this.getShortDescription().compareTo(o.getShortDescription());
68
+
69
+    }
70
+}

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

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

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

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

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

@@ -1,6 +1,34 @@
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
 
5 8
 
9
+    public String toString(Payment[] payments) {
10
+        StringBuilder result = new StringBuilder();
11
+        Arrays.sort(payments);
12
+        for (int i = 0; i < payments.length; i++) {
13
+            result.append(payments[i].getShortDescription() + "\n");
14
+        }
15
+        return result.toString();
16
+    }
17
+
18
+    public String toStringByPayerName(Payment[] payments) {
19
+        StringBuilder result = new StringBuilder();
20
+        Arrays.sort(payments, new PaymentSortByPayer());
21
+        for (int i = 0; i < payments.length; i++) {
22
+            result.append(payments[i].getShortDescription() + "\n");
23
+        }
24
+        return result.toString();
25
+    }
26
+    public String toStringById(Payment[] payments) {
27
+        StringBuilder result = new StringBuilder();
28
+        Arrays.sort(payments, Comparator.comparing(Payment::getId));
29
+        for (int i = 0; i < payments.length; i++) {
30
+            result.append(payments[i].getShortDescription() + "\n");
31
+        }
32
+        return result.toString();
33
+    }
6 34
 }

+ 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
+
8
+    public int compare(Payment o1, Payment o2) {
9
+        return o1.getPayerName().compareTo(o2.getPayerName());
10
+    }
11
+}

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

@@ -0,0 +1,57 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class CheckTest {
8
+    Check test;
9
+    Check test1;
10
+
11
+    @Before
12
+    public void before() {
13
+        test = new Check(2L, "donna", "987654321", "1234567890");
14
+        test1 = new Check(7L, "marie", "987654301", "1234567800");
15
+    }
16
+
17
+    @Test
18
+    public void getIdTest() {
19
+        Long expected = 2L;
20
+        Long actual = test.getId();
21
+        Assert.assertEquals(expected, actual);
22
+
23
+    }
24
+    @Test
25
+    public void getPayerNameTest() {
26
+        String expected = "donna";
27
+        String actual = test.getPayerName();
28
+        Assert.assertEquals(expected, actual);
29
+    }
30
+    @Test
31
+    public void getRoutingNumberTest() {
32
+        String expected = "987654321";
33
+        String actual = test.getRoutingNumber();
34
+        Assert.assertEquals(expected, actual);
35
+
36
+    }
37
+    @Test
38
+    public void getNumberTest() {
39
+        String expected = "1234567890";
40
+        String actual = test.getNumber();
41
+        Assert.assertEquals(expected, actual);
42
+
43
+    }
44
+
45
+    @Test
46
+    public void getShortDescriptionTest() {
47
+        String expected = "Check donna ***7890";
48
+        String actual = test.getShortDescription();
49
+        Assert.assertEquals(expected, actual);
50
+
51
+    }
52
+    @Test
53
+    public void compareToTest() {
54
+        int actual = test.compareTo(test1);
55
+        Assert.assertTrue(actual < 0);
56
+    }
57
+}

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

@@ -0,0 +1,65 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class CreditCardTest {
8
+
9
+    CreditCard test;
10
+    CreditCard test1;
11
+
12
+    @Before
13
+    public void before() {
14
+    test = new CreditCard(1L, "donna", "1234567890", 10, 2020);
15
+    test1 = new CreditCard(6L, "marie", "1234507890", 1, 2022);
16
+}
17
+    @Test
18
+    public void getIdTest() {
19
+        Long expected = 1L;
20
+        Long actual = test.getId();
21
+        Assert.assertEquals(expected, actual);
22
+
23
+    }
24
+    @Test
25
+    public void getPayerNameTest() {
26
+        String expected = "donna";
27
+        String actual = test.getPayerName();
28
+        Assert.assertEquals(expected, actual);
29
+    }
30
+    @Test
31
+    public void getNumberTest() {
32
+        String expected = "1234567890";
33
+        String actual = test.getNumber();
34
+        Assert.assertEquals(expected, actual);
35
+
36
+    }
37
+    @Test
38
+    public void getExpiredMonthTest() {
39
+        int expected = 10;
40
+        int actual = test.getExpiredMonth();
41
+        Assert.assertEquals(expected, actual);
42
+    }
43
+    @Test
44
+    public void getExpiredYearTest() {
45
+        int expected = 2020;
46
+        int actual = test.getExpiredYear();
47
+        Assert.assertEquals(expected, actual);
48
+    }
49
+
50
+    @Test
51
+    public void getShortDescriptionTest() {
52
+        String expected = "CC donna 7890 10/2020";
53
+        String actual = test.getShortDescription();
54
+        Assert.assertEquals(expected, actual);
55
+
56
+    }
57
+    @Test
58
+    public void compareToTest() {
59
+        int actual = test.compareTo(test1);
60
+        Assert.assertTrue(actual < 0);
61
+    }
62
+
63
+}
64
+
65
+

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

@@ -0,0 +1,51 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class PayPalTest {
8
+    PayPal test;
9
+    PayPal test1;
10
+
11
+    @Before
12
+    public void before() {
13
+        test = new PayPal(3L, "donna", "donna@jacobs.com");
14
+        test1 = new PayPal(8L, "marie", "marie@jacobs.com");
15
+    }
16
+
17
+    @Test
18
+    public void getIdTest() {
19
+        Long expected = 3L;
20
+        Long actual = test.getId();
21
+        Assert.assertEquals(expected, actual);
22
+
23
+    }
24
+    @Test
25
+    public void getPayerNameTest() {
26
+        String expected = "donna";
27
+        String actual = test.getPayerName();
28
+        Assert.assertEquals(expected, actual);
29
+    }
30
+
31
+    @Test
32
+    public void getEmailTest() {
33
+        String expected = "donna@jacobs.com";
34
+        String actual = test.getEmail();
35
+        Assert.assertEquals(expected, actual);
36
+
37
+    }
38
+
39
+    @Test
40
+    public void getShortDescriptionTest() {
41
+        String expected = "PayPal donna donna@jacobs.com";
42
+        String actual = test.getShortDescription();
43
+        Assert.assertEquals(expected, actual);
44
+
45
+    }
46
+    @Test
47
+    public void compareToTest() {
48
+        int actual = test.compareTo(test1);
49
+        Assert.assertTrue(actual < 0);
50
+    }
51
+}

+ 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 toStringPaymentTest(){
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 toStringMultiplePaymentTest() {
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
+
36
+    @Test
37
+    public void toStringByPayerName() {
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 toStringByIdTest() {
54
+        Payment[] payments = new Payment[2];
55
+        Payment paypal = new PayPal(81L, "Tamara Mowry", "tamara@mowry.com");
56
+        Payment check = new Check(120L, "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
+        Assert.assertEquals(expected, actual);
66
+    }
67
+
4 68
 }

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

@@ -0,0 +1,25 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class PaymentSortByPayerTest {
8
+    CreditCard creditCard;
9
+    Check check;
10
+    PaymentSortByPayer test;
11
+
12
+    @Before
13
+    public void setUp() {
14
+        creditCard = new CreditCard(6L, "marie", "1234507890", 1, 2022);
15
+        check = new Check(2L, "donna", "987654321", "1234567890");
16
+        test = new PaymentSortByPayer();
17
+    }
18
+
19
+    @Test
20
+    public void compareTest() {
21
+        int actual = test.compare(creditCard, check);
22
+        Assert.assertTrue(actual > 0);
23
+
24
+    }
25
+}