#21 JT_InterfaceComparableLab

Open
jtith wants to merge 1 commits from jtith/InterfaceComparableLab:master into master

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

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

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

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

@@ -0,0 +1,71 @@
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
+    @Override
20
+    public Long getId() {
21
+        return Id;
22
+    }
23
+
24
+    @Override
25
+    public String getPayerName() {
26
+        return payerName;
27
+    }
28
+
29
+    @Override
30
+    public String getShortDescription() {
31
+        return "CC " + getPayerName() + " " + getNumber().substring(getNumber().length() -4)
32
+                + " " + getExpiredMonth() + "/" + getExpiredYear();
33
+    }
34
+
35
+    public void setId(Long ID) {
36
+        this.Id = ID;
37
+    }
38
+
39
+    public void setPayerName(String payerName) {
40
+        this.payerName = payerName;
41
+    }
42
+
43
+    public String getNumber() {
44
+        return number;
45
+    }
46
+
47
+    public void setNumber(String number) {
48
+        this.number = number;
49
+    }
50
+
51
+    public int getExpiredMonth() {
52
+        return expiredMonth;
53
+    }
54
+
55
+    public void setExpiredMonth(int expiredMonth) {
56
+        this.expiredMonth = expiredMonth;
57
+    }
58
+
59
+    public int getExpiredYear() {
60
+        return expiredYear;
61
+    }
62
+
63
+    public void setExpiredYear(int expiredYear) {
64
+        this.expiredYear = expiredYear;
65
+    }
66
+
67
+    @Override
68
+    public int compareTo(Payment o) {
69
+       return 0;
70
+    }
71
+}

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

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

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

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

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

@@ -2,5 +2,48 @@ package com.zipcoder.payment;
2 2
 
3 3
 public class PaymentPresenter {
4 4
 
5
+    PaymentSortByPayer sorter = new PaymentSortByPayer();
6
+
7
+    public String toString(Payment[] payments) {
8
+
9
+        String output = "";
10
+
11
+        if(payments.length > 0) {
12
+            payments = sorter.compareArray(payments);
13
+            for(Payment p: payments) {
14
+                output += p.getShortDescription() + "\n";
15
+            }
16
+        }
17
+
18
+        return output.trim();
19
+    }
20
+
21
+    public String toStringByPayerName(Payment[] payments) {
22
+
23
+        String output = "";
24
+
25
+        if(payments.length > 0) {
26
+            payments = sorter.compareArrayByName(payments);
27
+            for(Payment p: payments) {
28
+                output += p.getShortDescription() + "\n";
29
+            }
30
+        }
31
+
32
+        return output.trim();
33
+    }
34
+
35
+    public String toStringById(Payment[] payments) {
36
+
37
+        String output = "";
38
+
39
+        if(payments.length > 0) {
40
+            payments = sorter.compareArrayById(payments);
41
+            for(Payment p: payments) {
42
+                output += p.getShortDescription() + "\n";
43
+            }
44
+        }
45
+
46
+        return output.trim();
47
+    }
5 48
 
6 49
 }

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

@@ -0,0 +1,58 @@
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.compareTo(o2);
10
+    }
11
+
12
+    public int compareByName(Payment o1, Payment o2) {
13
+        return o1.getPayerName().compareTo(o2.getPayerName());
14
+    }
15
+
16
+    public int compareById(Payment o1, Payment o2) {
17
+        return o1.getId().compareTo(o2.getId());
18
+    }
19
+
20
+    public Payment[] compareArray(Payment[] payments) {
21
+        for (int i = 0; i < payments.length - 1; i++) {
22
+            for (int j = 0; j < payments.length - 1; j++) {
23
+                if(compare(payments[j], payments[j+1]) > 0) {
24
+                    Payment temp = payments[j];
25
+                    payments[j] = payments[j+1];
26
+                    payments[j+1] = temp;
27
+                }
28
+            }
29
+        }
30
+        return payments;
31
+    }
32
+
33
+    public Payment[] compareArrayByName(Payment[] payments) {
34
+        for (int i = 0; i < payments.length - 1; i++) {
35
+            for (int j = 0; j < payments.length - 1; j++) {
36
+                if(compareByName(payments[j], payments[j+1]) > 0) {
37
+                    Payment temp = payments[j];
38
+                    payments[j] = payments[j+1];
39
+                    payments[j+1] = temp;
40
+                }
41
+            }
42
+        }
43
+        return payments;
44
+    }
45
+
46
+    public Payment[] compareArrayById(Payment[] payments) {
47
+        for (int i = 0; i < payments.length - 1; i++) {
48
+            for (int j = 0; j < payments.length - 1; j++) {
49
+                if(compareById(payments[j], payments[j+1]) > 0) {
50
+                    Payment temp = payments[j];
51
+                    payments[j] = payments[j+1];
52
+                    payments[j+1] = temp;
53
+                }
54
+            }
55
+        }
56
+        return payments;
57
+    }
58
+}

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

@@ -0,0 +1,45 @@
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
+
9
+    Check check1;
10
+    Check check2;
11
+
12
+    @Before
13
+    public void setup(){
14
+        check1 = new Check(1L,"Kris","12345","1111");
15
+        check2 = new Check(2L,"Kate","56789","2222");
16
+    }
17
+
18
+    @Test
19
+    public void testId(){
20
+        check1.setId(3L);
21
+
22
+        Long expected = 3L;
23
+        Long actual = check1.getId();
24
+        Assert.assertEquals(expected,actual);
25
+    }
26
+
27
+    @Test
28
+    public void testName(){
29
+        check1.setPayerName("Jevit");
30
+
31
+        String expected = "Jevit";
32
+        String actual = check1.getPayerName();
33
+        Assert.assertEquals(expected,actual);
34
+    }
35
+
36
+    @Test
37
+    public void testAccountNumber(){
38
+        check1.setAccountNumber("02468");
39
+
40
+        String expected = "02468";
41
+        String actual = check1.getRoutingNumber();
42
+        Assert.assertEquals(expected,actual);
43
+    }
44
+
45
+}

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

@@ -0,0 +1,29 @@
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 card1;
10
+    CreditCard card2;
11
+
12
+    @Before
13
+    public void setup(){
14
+        card1 = new CreditCard(1L,"Jevit Tith","0000-1111-2222-3333", 3, 2020);
15
+        card2 = new CreditCard(2L,"Billy Boy", "4444-5555-6666-7777", 5, 2025);
16
+    }
17
+
18
+    @Test
19
+    public void testId(){
20
+        card1.setId(3L);
21
+
22
+        Long expected = 3L;
23
+        Long actual = card1.getId();
24
+
25
+        Assert.assertEquals(expected,actual);
26
+    }
27
+
28
+
29
+}