Ver código fonte

finished comparator lab

Jonathan Hinds 7 anos atrás
pai
commit
1b5ceb6fb2

+ 8
- 0
pom.xml Ver arquivo

@@ -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>

+ 66
- 0
src/main/java/com/zipcoder/payment/Check.java Ver arquivo

@@ -0,0 +1,66 @@
1
+package com.zipcoder.payment;
2
+
3
+public class Check implements Payment {
4
+
5
+
6
+    private String account = "";
7
+    private String routing = "";
8
+    private String name = "";
9
+    private Long id = 0L;
10
+    private String description = "";
11
+
12
+    @Override
13
+    public Long getId() {
14
+        return this.id;
15
+    }
16
+
17
+    @Override
18
+    public String getPayerName() {
19
+        return this.name;
20
+    }
21
+
22
+    @Override
23
+    public String getShortDescription() {
24
+
25
+        String number = "";
26
+
27
+        for(int i = 0; i < account.length(); i ++){
28
+            if (i > account.length() - 5){
29
+                number += account.substring(i, i + 1);
30
+            } else {
31
+                number += "*";
32
+            }
33
+        }
34
+
35
+        return "Check [" + name + "][" + number + "]";
36
+    }
37
+
38
+    public void setId(Long id) {
39
+        this.id = id;
40
+    }
41
+
42
+    public void setName(String name) {
43
+        this.name = name;
44
+    }
45
+
46
+    public void setRouting(String number) {
47
+        this.routing = number;
48
+    }
49
+
50
+    public String getRouting() {
51
+        return this.routing;
52
+    }
53
+
54
+    public void setAccount(String account) {
55
+        this.account = account;
56
+    }
57
+
58
+    public String getAccount() {
59
+        return this.account;
60
+    }
61
+
62
+    @Override
63
+    public int compareTo(Payment o) {
64
+        return this.getShortDescription().compareTo(o.getShortDescription());
65
+    }
66
+}

+ 69
- 0
src/main/java/com/zipcoder/payment/CreditCard.java Ver arquivo

@@ -0,0 +1,69 @@
1
+package com.zipcoder.payment;
2
+
3
+public class CreditCard implements Payment {
4
+
5
+    private Long id = 0L;
6
+    private String name = "";
7
+    private String shortDesciption = "";
8
+    private String number = "";
9
+    private int expireMonth = 0;
10
+    private int exprieYear = 0;
11
+
12
+    public void setId(Long id) {
13
+        this.id = id;
14
+    }
15
+
16
+    @Override
17
+    public Long getId() {
18
+        return id;
19
+    }
20
+
21
+    @Override
22
+    public String getPayerName() {
23
+        return name;
24
+    }
25
+
26
+    @Override
27
+    public String getShortDescription() {
28
+        if(number.length() >= 16) {
29
+            return "CC [" + name + "][" + number.substring(number.length() - 4) + "][" + expireMonth + "]/[" + exprieYear + "]";
30
+        }
31
+        return null;
32
+    }
33
+
34
+    public void setName(String name) {
35
+        this.name = name;
36
+    }
37
+
38
+    public void setDesc(String description) {
39
+        shortDesciption = description;
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 setExpirationMonth(int month) {
51
+        this.expireMonth = month;
52
+    }
53
+
54
+    public int getExpirationMonth() {
55
+        return this.expireMonth;
56
+    }
57
+
58
+    public void setExpirationYear(int year) {
59
+        this.exprieYear = year;
60
+    }
61
+
62
+    public int getExpirationYear() {
63
+        return this.exprieYear;
64
+    }
65
+
66
+    public int compareTo(Payment other) {
67
+        return this.getShortDescription().compareTo(other.getShortDescription());
68
+    }
69
+}

+ 44
- 0
src/main/java/com/zipcoder/payment/PayPal.java Ver arquivo

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

+ 7
- 0
src/main/java/com/zipcoder/payment/Payment.java Ver arquivo

@@ -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
+}

+ 99
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Ver arquivo

@@ -1,6 +1,105 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.*;
4
+
3 5
 public class PaymentPresenter {
4 6
 
7
+    public String toString(Payment... payments){
8
+        Payment[] payments1 = new Payment[payments.length];
9
+
10
+        if(payments.length < 1){
11
+            return "";
12
+        } else if(payments.length == 1){
13
+            return payments[0].getShortDescription();
14
+        } else {
15
+            String result = "";
16
+
17
+            sort(payments);
18
+
19
+            for(Payment payment2 : payments){
20
+                result += payment2.getShortDescription() + "\n";
21
+            }
22
+
23
+            return result;
24
+        }
25
+    }
26
+
27
+    public void sort(Payment... payments){
28
+        for(int i = 0; i < payments.length - 1; i ++){
29
+
30
+            for(int m = 0; m < payments.length - 1; m ++){
31
+
32
+                Payment first = payments[m];
33
+                Payment next = payments[m + 1];
34
+                Payment temp = null;
35
+
36
+                if(first.compareTo(next) < 0){
37
+                    continue;
38
+                } else if (first.compareTo(next) > 0){
39
+                    temp = payments[m];
40
+                    payments[m] = payments[m + 1];
41
+                    payments[m + 1] = temp;
42
+                }
43
+            }
44
+        }
45
+    }
46
+
47
+    public void sortByName(Payment... payments){
48
+
49
+        PaymentSortByPayer sorter = new PaymentSortByPayer();
50
+
51
+        for(int i = 0; i < payments.length - 1; i ++){
52
+
53
+            for(int m = 0; m < payments.length - 1; m ++){
54
+
55
+                Payment first = payments[m];
56
+                Payment next = payments[m + 1];
57
+                Payment temp = null;
58
+
59
+                if(sorter.compare(first, next) < 0){
60
+                    continue;
61
+                } else if (sorter.compare(first, next) > 0){
62
+                    temp = payments[m];
63
+                    payments[m] = payments[m + 1];
64
+                    payments[m + 1] = temp;
65
+                }
66
+            }
67
+        }
68
+    }
69
+
70
+    public String toStringByPayerName(Payment[] payments) {
71
+
72
+        Payment[] payments1 = new Payment[payments.length];
73
+
74
+        if(payments.length < 1){
75
+            return "";
76
+        } else if(payments.length == 1){
77
+            return payments[0].getShortDescription();
78
+        } else {
79
+            String result = "";
80
+
81
+            sortByName(payments);
82
+
83
+            for(Payment payment2 : payments){
84
+                result += payment2.getShortDescription() + "\n";
85
+            }
86
+
87
+            return result;
88
+        }
89
+    }
90
+
91
+    public String toStringId(Payment... payments){
92
+
93
+        String result = "";
94
+
95
+        List<Payment> payments1 = new ArrayList<Payment>(Arrays.asList(payments));
96
+
97
+        Collections.sort(payments1, (Payment p1, Payment p2) -> p1.getId().compareTo(p2.getId()));
98
+
99
+        for(Payment payment : payments1){
100
+            result += payment.getShortDescription() + "\n";
101
+        }
5 102
 
103
+        return result;
104
+    }
6 105
 }

+ 11
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java Ver arquivo

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

+ 56
- 0
src/test/java/com/zipcoder/payment/CheckTest.java Ver arquivo

@@ -0,0 +1,56 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CheckTest {
7
+
8
+    @Test
9
+    public void idTest(){
10
+        Long id = 10L;
11
+
12
+        Check check = new Check();
13
+        check.setId(id);
14
+
15
+        Assert.assertEquals(id, check.getId());
16
+    }
17
+
18
+    @Test
19
+    public void nameTest(){
20
+        String name = "Jonathan";
21
+
22
+        Check check = new Check();
23
+        check.setName(name);
24
+
25
+        Assert.assertEquals(name, check.getPayerName());
26
+    }
27
+    @Test
28
+    public void routTest(){
29
+        String number = "1049591032949592349459";
30
+
31
+        Check check = new Check();
32
+        check.setRouting(number);
33
+
34
+        Assert.assertEquals(number, check.getRouting());
35
+    }
36
+    @Test
37
+    public void accountTest() {
38
+        String account = "2393093029309";
39
+
40
+        Check check = new Check();
41
+        check.setAccount(account);
42
+
43
+        Assert.assertEquals(account, check.getAccount());
44
+    }
45
+
46
+    @Test
47
+    public void descTest(){
48
+        String expect = "Check [Jonathan][***4444]";
49
+
50
+        Check check = new Check();
51
+        check.setAccount("4444444");
52
+        check.setName("Jonathan");
53
+
54
+        Assert.assertEquals(expect, check.getShortDescription());
55
+    }
56
+}

+ 46
- 0
src/test/java/com/zipcoder/payment/CompareToTest.java Ver arquivo

@@ -0,0 +1,46 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CompareToTest {
7
+
8
+    @Test
9
+    public void testSameDescCompare(){
10
+        Check check = new Check();
11
+        check.setName("Jonathan");
12
+        check.setAccount("1039340954092");
13
+        check.setRouting("304940392043");
14
+        check.setId(10L);
15
+
16
+        PayPal paypal = new PayPal();
17
+        paypal.setName("Jonathan");
18
+        paypal.setEmail("Jonathan@Jonathan.com");
19
+        paypal.setId(10L);
20
+
21
+        int acutal = paypal.compareTo(check);
22
+
23
+        Assert.assertTrue(acutal > 0);
24
+    }
25
+
26
+    @Test
27
+    public void testCompareName(){
28
+
29
+        PaymentSortByPayer sort = new PaymentSortByPayer();
30
+
31
+        Check check = new Check();
32
+        check.setName("Hinds");
33
+        check.setAccount("1039340954092");
34
+        check.setRouting("304940392043");
35
+        check.setId(10L);
36
+
37
+        PayPal paypal = new PayPal();
38
+        paypal.setName("Hinds");
39
+        paypal.setEmail("Jonathan@Jonathan.com");
40
+        paypal.setId(10L);
41
+
42
+        int acutal = sort.compare(paypal, check);
43
+
44
+        Assert.assertTrue(acutal == 0);
45
+    }
46
+}

+ 70
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java Ver arquivo

@@ -0,0 +1,70 @@
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 idGetterTest(){
10
+        Long id = 10L;
11
+
12
+        CreditCard creditCard = new CreditCard();
13
+        creditCard.setId(id);
14
+
15
+        Assert.assertEquals(id, creditCard.getId());
16
+    }
17
+
18
+    @Test
19
+    public void nameGetterTest(){
20
+        String name = "Jonathan";
21
+
22
+        CreditCard creditCard = new CreditCard();
23
+        creditCard.setName(name);
24
+
25
+        Assert.assertEquals(name, creditCard.getPayerName());
26
+    }
27
+
28
+    @Test
29
+    public void numGetterTest(){
30
+        String number = "4373886009987767";
31
+
32
+        CreditCard creditCard = new CreditCard();
33
+        creditCard.setNumber(number);
34
+
35
+        Assert.assertEquals(number, creditCard.getNumber());
36
+    }
37
+
38
+    @Test
39
+    public void expirationMonthGetterTest(){
40
+        int month = 12;
41
+
42
+        CreditCard creditCard = new CreditCard();
43
+        creditCard.setExpirationMonth(month);
44
+
45
+        Assert.assertEquals(month, creditCard.getExpirationMonth());
46
+    }
47
+
48
+    @Test
49
+    public void expirationYearGetterTest(){
50
+        int year = 2019;
51
+
52
+        CreditCard creditCard = new CreditCard();
53
+        creditCard.setExpirationYear(year);
54
+
55
+        Assert.assertEquals(year, creditCard.getExpirationYear());
56
+    }
57
+
58
+    @Test
59
+    public void testDescription(){
60
+        CreditCard creditCard = new CreditCard();
61
+        creditCard.setExpirationMonth(12);
62
+        creditCard.setExpirationYear(2018);
63
+        creditCard.setName("Jonathan");
64
+        creditCard.setNumber("43737294996928458683");
65
+
66
+        String expect = "CC [Jonathan][8683][12]/[2018]";
67
+
68
+        Assert.assertEquals(expect, creditCard.getShortDescription());
69
+    }
70
+}

+ 48
- 0
src/test/java/com/zipcoder/payment/PayPalTest.java Ver arquivo

@@ -0,0 +1,48 @@
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 idTest(){
10
+        Long id = 4L;
11
+
12
+        PayPal payPal = new PayPal();
13
+        payPal.setId(id);
14
+
15
+        Assert.assertEquals(id, payPal.getId());
16
+    }
17
+
18
+    @Test
19
+    public void nameTest(){
20
+        String name = "Jonathan";
21
+
22
+        PayPal payPal = new PayPal();
23
+        payPal.setName(name);
24
+
25
+        Assert.assertEquals(name, payPal.getPayerName());
26
+    }
27
+
28
+    @Test
29
+    public void emailTest(){
30
+        String email = "Jonathan@Jonathan.com";
31
+
32
+        PayPal payPal = new PayPal();
33
+        payPal.setEmail(email);
34
+
35
+        Assert.assertEquals(email, payPal.getEmail());
36
+    }
37
+
38
+    @Test
39
+    public void getDesciption(){
40
+        String expect = "Paypal [Jonathan][Jonathan@Jonathan.com]";
41
+
42
+        PayPal paypal = new PayPal();
43
+        paypal.setEmail("Jonathan@Jonathan.com");
44
+        paypal.setName("Jonathan");
45
+
46
+        Assert.assertEquals(expect, paypal.getShortDescription());
47
+    }
48
+}

+ 83
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Ver arquivo

@@ -1,4 +1,87 @@
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 toStringEmptyTest(){
10
+        Payment[] payments = new Payment[0];
11
+        PaymentPresenter presenter = new PaymentPresenter();
12
+        String expect = "";
13
+
14
+        String actual = presenter.toString(payments);
15
+
16
+        Assert.assertEquals(expect, actual);
17
+    }
18
+
19
+    @Test
20
+    public void toStringSortTest(){
21
+        Payment[] payments = new Payment[2];
22
+        Payment paypal = new PayPal();
23
+        ((PayPal) paypal).setEmail("Jonathan@Jonathan.com");
24
+        ((PayPal) paypal).setName("Jonathan");
25
+        payments[0] = paypal;
26
+
27
+        Payment check = new Check();
28
+        ((Check) check).setName("Jonathan");
29
+        ((Check) check).setAccount("1234567890123456");
30
+        ((Check) check).setRouting("23456756789056784");
31
+        payments[1] = check;
32
+
33
+
34
+        PaymentPresenter presenter = new PaymentPresenter();
35
+        String expect = "Check [Jonathan][************3456]\nPaypal [Jonathan][Jonathan@Jonathan.com]\n";
36
+
37
+        String actual = presenter.toString(payments);
38
+
39
+        Assert.assertEquals(expect, actual);
40
+    }
41
+
42
+    @Test
43
+    public void testPayerName(){
44
+        Payment[] payments = new Payment[2];
45
+        Payment paypal = new PayPal();
46
+        ((PayPal) paypal).setName("Jonathan");
47
+        ((PayPal) paypal).setEmail("Jonathan@Jonathan.com");
48
+
49
+        Payment check = new Check();
50
+        ((Check) check).setName("Jonathan");
51
+        ((Check) check).setAccount("44444551");
52
+
53
+        payments[0] = paypal;
54
+        payments[1] = check;
55
+
56
+        PaymentPresenter presenter = new PaymentPresenter();
57
+        String expected = "Paypal [Jonathan][Jonathan@Jonathan.com]\nCheck [Jonathan][****4551]\n";
58
+
59
+        String actual = presenter.toStringByPayerName(payments);
60
+        Assert.assertEquals(expected, actual);
61
+    }
62
+
63
+    @Test
64
+    public void toStringById() {
65
+        Payment[] payments = new Payment[2];
66
+        Payment paypal = new PayPal();
67
+        ((PayPal) paypal).setId(120L);
68
+        ((PayPal) paypal).setName("Jonathan");
69
+        ((PayPal) paypal).setEmail("Jonathan@Jonathan.com");
70
+
71
+        Payment check = new Check();
72
+        ((Check) check).setId(83L);
73
+        ((Check) check).setAccount("1234567890");
74
+        ((Check) check).setRouting("1234567890");
75
+        ((Check) check).setName("Jonathan");
76
+
77
+
78
+        payments[0] = paypal;
79
+        payments[1] = check;
80
+
81
+        PaymentPresenter presenter = new PaymentPresenter();
82
+        String expected = "Check [Jonathan][******7890]\nPaypal [Jonathan][Jonathan@Jonathan.com]\n";
83
+
84
+        String actual = presenter.toStringId(payments);
85
+        Assert.assertEquals(expected, actual);
86
+    }
4 87
 }