Connor Dunnigan il y a 7 ans
Parent
révision
45652650da

+ 9
- 1
pom.xml Voir le fichier

@@ -11,5 +11,13 @@
11 11
         <maven.compiler.source>1.8</maven.compiler.source>
12 12
         <maven.compiler.target>1.8</maven.compiler.target>
13 13
     </properties>
14
-
14
+    <dependencies>
15
+        <!-- https://mvnrepository.com/artifact/junit/junit -->
16
+        <dependency>
17
+            <groupId>junit</groupId>
18
+            <artifactId>junit</artifactId>
19
+            <version>4.12</version>
20
+            <scope>test</scope>
21
+        </dependency>
22
+    </dependencies>
15 23
 </project>

+ 41
- 0
src/main/java/com/zipcoder/payment/Check.java Voir le fichier

@@ -0,0 +1,41 @@
1
+package com.zipcoder.payment;
2
+
3
+public class Check implements Payment{
4
+    long id;
5
+    String payerName, routingNumber, accountNumber;
6
+
7
+    public Check(long id, String payerName, String routingNumber, String accountNumber) {
8
+        this.id = id;
9
+        this.payerName = payerName;
10
+        this.routingNumber = routingNumber;
11
+        this.accountNumber = accountNumber;
12
+    }
13
+
14
+    @Override
15
+    public long getId() { return id; }
16
+
17
+    public void setId(long id) { this.id = id; }
18
+
19
+    @Override
20
+    public String getPayerName() { return payerName; }
21
+
22
+    public void setPayerName(String payerName) { this.payerName = payerName; }
23
+
24
+    public String getRoutingNumber() { return routingNumber; }
25
+
26
+    public void setRoutingNumber(String routingNumber) { this.routingNumber = routingNumber; }
27
+
28
+    public String getAccountNumber() { return accountNumber; }
29
+
30
+    public void setAccountNumber(String accountNumber) { this.accountNumber = accountNumber; }
31
+
32
+    public String getShortDescription(){
33
+        return "Check" + " " + payerName +
34
+                " ***" + accountNumber.substring(accountNumber.length()-4);
35
+    }
36
+
37
+    @Override
38
+    public int compareTo(Payment other) {
39
+        return this.getShortDescription().compareTo(other.getShortDescription());
40
+    }
41
+}

+ 47
- 0
src/main/java/com/zipcoder/payment/CreditCard.java Voir le fichier

@@ -0,0 +1,47 @@
1
+package com.zipcoder.payment;
2
+
3
+public class CreditCard implements Payment {
4
+    long id;
5
+    String payerName, number;
6
+    int expiredMo, expiredYr;
7
+
8
+    public CreditCard(long id, String payerName, String number, int expiredMo, int expiredYr) {
9
+        this.id = id;
10
+        this.payerName = payerName;
11
+        this.number = number;
12
+        this.expiredMo = expiredMo;
13
+        this.expiredYr = expiredYr;
14
+    }
15
+
16
+    @Override
17
+    public long getId() { return id; }
18
+
19
+    public void setId(long id) { this.id = id; }
20
+
21
+    @Override
22
+    public String getPayerName() { return payerName; }
23
+
24
+    public void setPayerName(String payerName) { this.payerName = payerName; }
25
+
26
+    public String getNumber() { return number; }
27
+
28
+    public void setNumber(String number) { this.number = number; }
29
+
30
+    public int getExpiredMo() { return expiredMo; }
31
+
32
+    public void setExpiredMo(int expiredMo) { this.expiredMo = expiredMo; }
33
+
34
+    public int getExpiredYr() { return expiredYr; }
35
+
36
+    public void setExpiredYr(int expiredYr) { this.expiredYr = expiredYr; }
37
+
38
+    public String getShortDescription(){
39
+        return "CC" + " " + payerName + " " + number.substring(number.length()-4)
40
+                + " " + expiredMo + "/" + expiredYr;
41
+    }
42
+
43
+    @Override
44
+    public int compareTo(Payment other) {
45
+        return this.getShortDescription().compareTo(other.getShortDescription());
46
+    }
47
+}

+ 47
- 0
src/main/java/com/zipcoder/payment/PayPal.java Voir le fichier

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

+ 7
- 0
src/main/java/com/zipcoder/payment/Payment.java Voir le fichier

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

+ 24
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Voir le fichier

@@ -1,6 +1,30 @@
1 1
 package com.zipcoder.payment;
2 2
 
3 3
 public class PaymentPresenter {
4
+    PaymentSortByPayer sortPlayer = new PaymentSortByPayer();
5
+    String payPresenter = new String();
4 6
 
7
+    public String toString(Payment ...payments){
8
+        sortPlayer.compareArray(payments);
9
+        appendPresenter(payments);
10
+        return payPresenter;
11
+    }
5 12
 
13
+    public String toStringByPayerName(Payment ...payments) {
14
+        sortPlayer.compareByPayerName(payments);
15
+        appendPresenter(payments);
16
+        return payPresenter;
17
+    }
18
+
19
+    public String toStringById(Payment ...payments) {
20
+        sortPlayer.compareById(payments);
21
+        appendPresenter(payments);
22
+        return payPresenter;
23
+    }
24
+
25
+    private void appendPresenter(Payment[] payments) {
26
+        for(Payment payment : payments){
27
+            payPresenter += payment.getShortDescription() + "\n";
28
+        }
29
+    }
6 30
 }

+ 82
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java Voir le fichier

@@ -0,0 +1,82 @@
1
+package com.zipcoder.payment;
2
+
3
+public class PaymentSortByPayer implements Comparable<Payment> {
4
+    Payment pay1,pay2;
5
+    Payment[] payments;
6
+
7
+    public PaymentSortByPayer(){}
8
+
9
+    public PaymentSortByPayer(Payment pay1) { this.pay1 = pay1; }
10
+
11
+    public PaymentSortByPayer(Payment[] payments) { this.payments = payments; }
12
+
13
+    public PaymentSortByPayer(Payment pay1, Payment pay2) {
14
+        this.pay1 = pay1;
15
+        this.pay2 = pay2;
16
+    }
17
+
18
+    @Override
19
+    public int compareTo(Payment o){
20
+        return pay1.getShortDescription().compareTo(o.getShortDescription());
21
+    }
22
+
23
+    public int compare(){
24
+        return pay1.getShortDescription().compareTo(pay2.getShortDescription());
25
+    }
26
+
27
+    public int compare(Payment pay1, Payment pay2){
28
+        return pay1.getShortDescription().compareTo(pay2.getShortDescription());
29
+    }
30
+
31
+    public Payment[] compareArray(Payment ... payments) {
32
+        for (int i = 0; i < payments.length-1; i++) {
33
+            for (int j = 0; j < payments.length-1; j++)
34
+                sortByComparison(j, payments);
35
+        }
36
+        return payments;
37
+    }
38
+
39
+    private void sortByComparison(int j, Payment[] payments) {
40
+        if (compare(payments[j], payments[j+1]) > 0) {
41
+            Payment temp = payments[j];
42
+            payments[j] = payments[j+1];
43
+            payments[j+1] = temp;
44
+        }
45
+    }
46
+
47
+    public Payment[] compareById(Payment ... payments) {
48
+        for (int i = 0; i < payments.length-1; i++) {
49
+            for (int j = 0; j < payments.length-1; j++) {
50
+                sortById(j, payments);
51
+            }
52
+        }
53
+        return payments;
54
+    }
55
+
56
+    private void sortById(int j, Payment[] payments) {
57
+        if (payments[j].getId() > payments[j+1].getId()) {
58
+            Payment temp = payments[j];
59
+            payments[j] = payments[j+1];
60
+            payments[j+1] = temp;
61
+        }
62
+    }
63
+
64
+    public Payment[] compareByPayerName(Payment ... payments) {
65
+        for (int i = 0; i < payments.length-1; i++) {
66
+            for (int j = 0; j < payments.length-1; j++) {
67
+                sortByName(j, payments);
68
+            }
69
+        }
70
+        return payments;
71
+    }
72
+
73
+    private void sortByName(int j, Payment[] payments) {
74
+        if (payments[j].getPayerName().compareTo(payments[j+1].getPayerName()) > 0) {
75
+            Payment temp = payments[j];
76
+            payments[j] = payments[j+1];
77
+            payments[j+1] = temp;
78
+        }
79
+    }
80
+
81
+
82
+}

+ 36
- 0
src/test/java/com/zipcoder/payment/CheckTest.java Voir le fichier

@@ -0,0 +1,36 @@
1
+package com.zipcoder.payment;
2
+import org.junit.Before;
3
+import org.junit.Test;
4
+import org.junit.Assert;
5
+
6
+public class CheckTest {
7
+    Check check;
8
+
9
+    @Before
10
+    public void initialize(){
11
+        check = new Check(1L, "Bill", "1111111111", "2222222222");
12
+    }
13
+    @Test
14
+    public void testCheckName(){
15
+        Assert.assertEquals("Bill",check.getPayerName());
16
+    }
17
+    @Test
18
+    public void testCheckId(){
19
+        Assert.assertEquals(1L,check.getId());
20
+    }
21
+    @Test
22
+    public void testCheckRoutingNumber(){
23
+        Assert.assertEquals("1111111111",check.getRoutingNumber());
24
+    }
25
+    @Test
26
+    public void testCheckAccountNumber(){
27
+        Assert.assertEquals("2222222222",check.getAccountNumber());
28
+    }
29
+
30
+    @Test
31
+    public void testCheckDescription() {
32
+        String exp = "Check Bill ***2222";
33
+        // : Then
34
+        Assert.assertEquals(exp, check.getShortDescription());
35
+    }
36
+}

+ 18
- 0
src/test/java/com/zipcoder/payment/CompareTest.java Voir le fichier

@@ -0,0 +1,18 @@
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 CompareTest {
8
+
9
+    @Test
10
+    public void testCompareTo(){
11
+        // : Given
12
+        PayPal payPal = new PayPal(1L,"Joe","Joe@gmail.com");
13
+        Check check = new Check(2L,"Bob","5555555555", "6666666666");
14
+        // : Then
15
+        Assert.assertTrue(payPal.compareTo(check)>0);
16
+
17
+    }
18
+}

+ 40
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java Voir le fichier

@@ -0,0 +1,40 @@
1
+package com.zipcoder.payment;
2
+import org.junit.Before;
3
+import org.junit.Test;
4
+import org.junit.Assert;
5
+
6
+public class CreditCardTest {
7
+    CreditCard cc;
8
+
9
+    @Before
10
+    public void initialize(){
11
+        cc = new CreditCard(3L,"Pat","3333333333",10,2020);
12
+    }
13
+    @Test
14
+    public void testCCname(){
15
+        Assert.assertEquals("Pat",cc.getPayerName());
16
+    }
17
+    @Test
18
+    public void testCCid(){
19
+        Assert.assertEquals(3L,cc.getId());
20
+    }
21
+    @Test
22
+    public void testCCnumber(){
23
+        Assert.assertEquals("3333333333",cc.getNumber());
24
+    }
25
+    @Test
26
+    public void testCCexpMo(){
27
+        Assert.assertEquals(10,cc.getExpiredMo());
28
+    }
29
+    @Test
30
+    public void testCCexpYr(){
31
+        Assert.assertEquals(2020,cc.getExpiredYr());
32
+    }
33
+    @Test
34
+    public void testCCdescription(){
35
+        // : When
36
+        String exp = "CC Pat 3333 10/2020";
37
+        // : Then
38
+        Assert.assertEquals(exp,cc.getShortDescription());
39
+    }
40
+}

+ 36
- 0
src/test/java/com/zipcoder/payment/PayPalTest.java Voir le fichier

@@ -0,0 +1,36 @@
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 payPal;
9
+
10
+    @Before
11
+    public void initialize(){
12
+        payPal = new PayPal(1L,"Joe","joe@gmail.com");
13
+    }
14
+
15
+    @Test
16
+    public void testPayPalName(){
17
+        Assert.assertEquals("Joe",payPal.getPayerName());
18
+    }
19
+    @Test
20
+    public void testPayPalId(){
21
+        Assert.assertEquals(1L,payPal.getId());
22
+    }
23
+
24
+    @Test
25
+    public void testPayPalEmail(){
26
+        Assert.assertEquals("joe@gmail.com",payPal.getEmail());
27
+    }
28
+
29
+    @Test
30
+    public void testPayPalDescription() {
31
+        // : When
32
+        String exp = "Paypal Joe joe@gmail.com";
33
+        // : Then
34
+        Assert.assertEquals(exp, payPal.getShortDescription());
35
+    }
36
+}

+ 63
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Voir le fichier

@@ -1,4 +1,67 @@
1 1
 package com.zipcoder.payment;
2
+import org.junit.Test;
3
+import org.junit.Assert;
2 4
 
3 5
 public class PaymentPresenterTest {
6
+
7
+    @Test
8
+    public void testToStringNoPayments(){
9
+        Payment[] payments = new Payment[0];
10
+        PaymentPresenter presenter = new PaymentPresenter();
11
+        String expected = "";
12
+
13
+        String actual = presenter.toString(payments);
14
+
15
+        Assert.assertEquals(expected, actual);
16
+    }
17
+
18
+    @Test
19
+    public void testToStringMultiPayments(){
20
+        Payment[] payments = new Payment[2];
21
+        Payment paypal = new PayPal(4L, "Tia Mowry", "tia@mowry.com");
22
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
23
+
24
+        payments[0] = paypal;
25
+        payments[1] = check;
26
+
27
+        PaymentPresenter presenter = new PaymentPresenter();
28
+        String expected = "Check Tia Mowry ***4551\nPaypal Tia Mowry tia@mowry.com\n";
29
+
30
+        String actual = presenter.toString(payments);
31
+        Assert.assertEquals(expected, actual);
32
+    }
33
+
34
+    @Test
35
+    public void testToStringByName(){
36
+        Payment[] payments = new Payment[2];
37
+        Payment paypal = new PayPal(4L, "Tamara Mowry", "tamara@mowry.com");
38
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
39
+
40
+        payments[0] = paypal;
41
+        payments[1] = check;
42
+
43
+        PaymentPresenter presenter = new PaymentPresenter();
44
+        String expected = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
45
+
46
+        String actual = presenter.toStringByPayerName(payments);
47
+        Assert.assertEquals(expected, actual);
48
+    }
49
+
50
+    @Test
51
+    public void testToStringById(){
52
+        Payment[] payments = new Payment[2];
53
+        Payment paypal = new PayPal(120L, "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 = "Check Tia Mowry ***4551\nPaypal Tamara Mowry tamara@mowry.com\n";
61
+
62
+        String actual = presenter.toStringById(payments);
63
+        Assert.assertEquals(expected, actual);
64
+    }
65
+
66
+
4 67
 }