Jennifer Chao 7 年之前
父節點
當前提交
c29cd0c699

+ 8
- 0
pom.xml 查看文件

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

+ 63
- 0
src/main/java/com/zipcoder/payment/Check.java 查看文件

@@ -0,0 +1,63 @@
1
+package com.zipcoder.payment;
2
+
3
+public class Check implements Payment {
4
+
5
+
6
+    long id;
7
+    String payerName;
8
+    String routingNumber;
9
+    String accountNumber;
10
+
11
+    public Check(long id, String payerName, String routingNumber, String accountNumber) {
12
+        this.id = id;
13
+        this.payerName = payerName;
14
+        this.routingNumber = routingNumber;
15
+        this.accountNumber = accountNumber;
16
+    }
17
+
18
+    @Override
19
+    public int compareTo(Payment o) {
20
+        return this.getShortDescription().compareTo(o.getShortDescription());
21
+    }
22
+
23
+    @Override
24
+    public long getID() {
25
+        return id;
26
+    }
27
+
28
+    @Override
29
+    public String getPayerName() {
30
+        return payerName;
31
+    }
32
+
33
+    @Override
34
+    public String getShortDescription() {
35
+        String lastFour = accountNumber.substring(accountNumber.length() - 4);
36
+        return "Check " + payerName + " ***" + lastFour;
37
+    }
38
+
39
+    public void setId(long id) {
40
+        this.id = id;
41
+    }
42
+
43
+    public void setPayerName(String payerName) {
44
+        this.payerName = payerName;
45
+    }
46
+
47
+    public String getRoutingNumber() {
48
+        return routingNumber;
49
+    }
50
+
51
+    public void setRoutingNumber(String routingNumber) {
52
+        this.routingNumber = routingNumber;
53
+    }
54
+
55
+    public String getAccountNumber() {
56
+        return accountNumber;
57
+    }
58
+
59
+    public void setAccountNumber(String accountNumber) {
60
+        this.accountNumber = accountNumber;
61
+    }
62
+
63
+}

+ 71
- 0
src/main/java/com/zipcoder/payment/CreditCard.java 查看文件

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

+ 11
- 0
src/main/java/com/zipcoder/payment/Payment.java 查看文件

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

+ 56
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java 查看文件

@@ -2,5 +2,61 @@ package com.zipcoder.payment;
2 2
 
3 3
 public class PaymentPresenter {
4 4
 
5
+    public String toString(Payment[] payments) {
6
+        Payment temp;
7
+        for (int i = 0; i < payments.length; i++) {
8
+            for (int j = i + 1; j < payments.length; j++) {
9
+                if (payments[i].compareTo(payments[j]) > 0) {
10
+                    temp = payments[j];
11
+                    payments[j] = payments[i];
12
+                    payments[i] = temp;
13
+                }
14
+            }
15
+        }
5 16
 
17
+        String paymentsList = "";
18
+        for (Payment payment : payments) {
19
+            paymentsList += payment.getShortDescription() + "\n";
20
+        }
21
+        return paymentsList;
22
+    }
23
+
24
+    public String toStringByPayerName(Payment[] payments) {
25
+        PaymentSortByPayer paymentSortByPayer = new PaymentSortByPayer();
26
+        Payment temp;
27
+        for (int i = 0; i < payments.length; i++) {
28
+            for (int j = i + 1; j < payments.length; j++) {
29
+                if (paymentSortByPayer.compare(payments[i], (payments[j])) > 0) {
30
+                    temp = payments[j];
31
+                    payments[j] = payments[i];
32
+                    payments[i] = temp;
33
+                }
34
+            }
35
+        }
36
+
37
+        String paymentsList = "";
38
+        for (Payment payment : payments) {
39
+            paymentsList += payment.getShortDescription() + "\n";
40
+        }
41
+        return paymentsList;
42
+    }
43
+
44
+    public String toStringById(Payment[] payments) {
45
+        Payment temp;
46
+        for (int i = 0; i < payments.length; i++) {
47
+            for (int j = i + 1; j < payments.length; j++) {
48
+                if (payments[i].getID() > payments[j].getID()) {
49
+                    temp = payments[j];
50
+                    payments[j] = payments[i];
51
+                    payments[i] = temp;
52
+                }
53
+            }
54
+        }
55
+
56
+        String paymentsList = "";
57
+        for (Payment payment : payments) {
58
+            paymentsList += payment.getShortDescription() + "\n";
59
+        }
60
+        return paymentsList;
61
+    }
6 62
 }

+ 12
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java 查看文件

@@ -0,0 +1,12 @@
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
+
12
+}

+ 50
- 0
src/main/java/com/zipcoder/payment/Paypal.java 查看文件

@@ -0,0 +1,50 @@
1
+package com.zipcoder.payment;
2
+
3
+public class Paypal implements Payment {
4
+
5
+    long id;
6
+    String payerName;
7
+    String email;
8
+
9
+    public Paypal(long id, String payerName, String email) {
10
+        this.id = id;
11
+        this.payerName = payerName;
12
+        this.email = email;
13
+    }
14
+
15
+    @Override
16
+    public int compareTo(Payment o) {
17
+        return this.getShortDescription().compareTo(o.getShortDescription());
18
+    }
19
+
20
+    @Override
21
+    public long getID() {
22
+        return id;
23
+    }
24
+
25
+    @Override
26
+    public String getPayerName() {
27
+        return payerName;
28
+    }
29
+
30
+    @Override
31
+    public String getShortDescription() {
32
+        return "Paypal " + payerName + " " + email;
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 getEmail() {
44
+        return email;
45
+    }
46
+
47
+    public void setEmail(String email) {
48
+        this.email = email;
49
+    }
50
+}

+ 30
- 0
src/test/java/com/zipcoder/payment/CheckTest.java 查看文件

@@ -0,0 +1,30 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CheckTest {
7
+
8
+    CreditCard creditCard = new CreditCard(001, "Jenn Chao", "123456789876", 10, 2020);
9
+    Check check = new Check(002, "Jenn Chao", "031100089", "123456789");
10
+    Paypal payPal = new Paypal(003, "Jenn Chao", "jennchao@zcw.com");
11
+
12
+    @Test
13
+    public void testGetShortDescription() {
14
+        String expected = "Check Jenn Chao ***6789";
15
+        String actual = check.getShortDescription();
16
+
17
+        Assert.assertEquals(expected, actual);
18
+    }
19
+
20
+    @Test
21
+    public void testCompareTo_CheckToCC() {
22
+        Assert.assertTrue(check.compareTo(creditCard) > 0);
23
+    }
24
+
25
+    @Test
26
+    public void testCompareTo_CheckToPaypal(){
27
+        Assert.assertTrue(check.compareTo(payPal) < 0);
28
+    }
29
+
30
+}

+ 30
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java 查看文件

@@ -0,0 +1,30 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class CreditCardTest {
7
+
8
+    CreditCard creditCard = new CreditCard(001, "Jenn Chao", "123456789876", 10, 2020);
9
+    Check check = new Check(002, "Jenn Chao", "031100089", "123456789");
10
+    Paypal payPal = new Paypal(003, "Jenn Chao", "jennchao@zcw.com");
11
+
12
+    @Test
13
+    public void testGetShortDescription() {
14
+        String expected = "CC Jenn Chao 9876 10/2020";
15
+        String actual = creditCard.getShortDescription();
16
+
17
+        Assert.assertEquals(expected, actual);
18
+    }
19
+
20
+    @Test
21
+    public void testCompareTo_CCToCheck() {
22
+        Assert.assertTrue(creditCard.compareTo(check) < 0);
23
+    }
24
+
25
+    @Test
26
+    public void testCompareTo_CCToPaypal() {
27
+        Assert.assertTrue(creditCard.compareTo(payPal) < 0);
28
+    }
29
+
30
+}

+ 63
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java 查看文件

@@ -1,4 +1,67 @@
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 testToString_NoPayments() {
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 toString_Payments() {
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
+    @Test
36
+    public void toStringByPayerName(){
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.toStringByPayerName(payments);
48
+        Assert.assertEquals(expected, actual);
49
+    }
50
+
51
+    @Test
52
+    public void toStringbyId(){
53
+        Payment[] payments = new Payment[2];
54
+        Payment paypal = new Paypal(120L, "Tamara Mowry", "tamara@mowry.com");
55
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
56
+
57
+        payments[0] = paypal;
58
+        payments[1] = check;
59
+
60
+        PaymentPresenter presenter = new PaymentPresenter();
61
+        String expected = "Check Tia Mowry ***4551\nPaypal Tamara Mowry tamara@mowry.com\n";
62
+
63
+        String actual = presenter.toStringById(payments);
64
+        Assert.assertEquals(expected, actual);
65
+    }
66
+
4 67
 }

+ 28
- 0
src/test/java/com/zipcoder/payment/PaymentSortByPayerTest.java 查看文件

@@ -0,0 +1,28 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class PaymentSortByPayerTest {
7
+
8
+    CreditCard creditCard = new CreditCard(001, "Jenn Chao", "123456789876", 10, 2020);
9
+    Check check = new Check(002, "Jenn Chao", "031100089", "123456789");
10
+    Paypal payPal = new Paypal(003, "Jennifer Chao", "jennchao@zcw.com");
11
+    PaymentSortByPayer paymentSortByPayer = new PaymentSortByPayer();
12
+
13
+    @Test
14
+    public void testCompare_SameName() {
15
+        Assert.assertTrue(paymentSortByPayer.compare(check, creditCard) == 0);
16
+    }
17
+
18
+    @Test
19
+    public void testCompare_SmallerLarger() {
20
+        Assert.assertTrue(paymentSortByPayer.compare(check, payPal) < 0);
21
+    }
22
+
23
+    @Test
24
+    public void testCompare_LargerSmaller(){
25
+        Assert.assertTrue(paymentSortByPayer.compare(payPal, creditCard) > 0);
26
+    }
27
+
28
+}

+ 29
- 0
src/test/java/com/zipcoder/payment/PaypalTest.java 查看文件

@@ -0,0 +1,29 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class PaypalTest {
7
+
8
+    CreditCard creditCard = new CreditCard(001, "Jenn Chao", "123456789876", 10, 2020);
9
+    Check check = new Check(002, "Jenn Chao", "031100089", "123456789");
10
+    Paypal payPal = new Paypal(003, "Jenn Chao", "jennchao@zcw.com");
11
+
12
+    @Test
13
+    public void testGetShortDescription() {
14
+        String expected = "Paypal Jenn Chao jennchao@zcw.com";
15
+        String actual = payPal.getShortDescription();
16
+
17
+        Assert.assertEquals(expected, actual);
18
+    }
19
+
20
+    @Test
21
+    public void testCompareTo_PaypalToCC() {
22
+        Assert.assertTrue(payPal.compareTo(creditCard) > 0);
23
+    }
24
+
25
+    @Test
26
+    public void testCompareTo_PaypalToCheck() {
27
+        Assert.assertTrue(payPal.compareTo(check) > 0);
28
+    }
29
+}