瀏覽代碼

submission

Rachelle 8 年之前
父節點
當前提交
5777355228

+ 12
- 1
pom.xml 查看文件

3
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
     <modelVersion>4.0.0</modelVersion>
5
     <modelVersion>4.0.0</modelVersion>
6
-
6
+    <build>
7
+        <plugins>
8
+            <plugin>
9
+                <groupId>org.apache.maven.plugins</groupId>
10
+                <artifactId>maven-compiler-plugin</artifactId>
11
+                <configuration>
12
+                    <source>1.8</source>
13
+                    <target>1.8</target>
14
+                </configuration>
15
+            </plugin>
16
+        </plugins>
17
+    </build>
7
     <groupId>com.zipcoder</groupId>
18
     <groupId>com.zipcoder</groupId>
8
     <artifactId>payment</artifactId>
19
     <artifactId>payment</artifactId>
9
     <version>1.0-SNAPSHOT</version>
20
     <version>1.0-SNAPSHOT</version>

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

9
 
9
 
10
     public Check(){}
10
     public Check(){}
11
 
11
 
12
+    public Check(Long id, String payerName, String routingName, String accountNumber){
13
+        this.id = id;
14
+        this.payerName = payerName;
15
+        this.routingName = routingName;
16
+        this.accountNumber = accountNumber;
17
+    }
18
+
12
     public Check(String payerName, String accountNumber){
19
     public Check(String payerName, String accountNumber){
13
         this.payerName = payerName;
20
         this.payerName = payerName;
14
         this.accountNumber = accountNumber;
21
         this.accountNumber = accountNumber;
54
         String lastFour = accountNumber.substring(accountNumber.length()-4);
61
         String lastFour = accountNumber.substring(accountNumber.length()-4);
55
         return lastFour;
62
         return lastFour;
56
     }
63
     }
64
+
65
+    public int compareTo(Payment o) {
66
+        return this.getShortDescription().compareTo(o.getShortDescription());
67
+    }
57
 }
68
 }

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

71
         return lastFour;
71
         return lastFour;
72
     }
72
     }
73
 
73
 
74
+    public int compareTo(Payment o) {
75
+        return this.getShortDescription().compareTo(o.getShortDescription());
76
+    }
74
 }
77
 }

+ 10
- 0
src/main/java/com/zipcoder/payment/PayPal.java 查看文件

12
         this.email = email;
12
         this.email = email;
13
     }
13
     }
14
 
14
 
15
+    public PayPal(Long id, String payerName, String email){
16
+        this.id = id;
17
+        this.payerName = payerName;
18
+        this.email = email;
19
+    }
20
+
15
     public Long getID() {
21
     public Long getID() {
16
         return id;
22
         return id;
17
     }
23
     }
39
     public String getShortDescription() {
45
     public String getShortDescription() {
40
         return ("Paypal " + getPayerName() + " " + email);
46
         return ("Paypal " + getPayerName() + " " + email);
41
     }
47
     }
48
+
49
+    public int compareTo(Payment o) {
50
+        return this.getShortDescription().compareTo(o.getShortDescription());
51
+    }
42
 }
52
 }

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

1
 package com.zipcoder.payment;
1
 package com.zipcoder.payment;
2
 
2
 
3
-public interface Payment {
3
+public interface Payment extends Comparable<Payment> {
4
     Long getID();
4
     Long getID();
5
 
5
 
6
     String getPayerName();
6
     String getPayerName();

+ 41
- 1
src/main/java/com/zipcoder/payment/PaymentPresenter.java 查看文件

1
 package com.zipcoder.payment;
1
 package com.zipcoder.payment;
2
 
2
 
3
+import java.util.Arrays;
4
+import java.util.Comparator;
5
+
3
 public class PaymentPresenter {
6
 public class PaymentPresenter {
4
 
7
 
8
+    public String toString(Payment[] payments) {
9
+        String result = "";
10
+        if (payments.length > 0) {
11
+           String[] descriptionArray = getArrayOfDescriptions(payments);
12
+           Arrays.sort(descriptionArray);
13
+           result = returnDescriptionsAsString(descriptionArray);
14
+        }
15
+        return result;
16
+    }
17
+
18
+    public String[] getArrayOfDescriptions(Payment[] payments) {
19
+        String[] descriptionArray = new String[payments.length];
20
+        for (int i = 0; i < payments.length; i++) {
21
+            descriptionArray[i] = payments[i].getShortDescription();
22
+            }
23
+        return descriptionArray;
24
+    }
25
+
26
+    public String returnDescriptionsAsString(String[] descriptions){
27
+        StringBuilder stringBuilder = new StringBuilder();
28
+        for(String element: descriptions){
29
+            stringBuilder.append(element + "\n");
30
+        }
31
+        return stringBuilder.toString();
32
+    }
33
+
34
+    public String toStringByPayerName(Payment[] payments){
35
+        Arrays.sort(payments, new PaymentSortByPayer());
36
+        return returnDescriptionsAsString(getArrayOfDescriptions(payments));
37
+    }
38
+
39
+    public String toStringById(Payment[] payments){
40
+        Comparator<Payment> compareById = Comparator.comparing(Payment::getID);
41
+        //Comparator<Payment> compareById = (payment1,payment2) -> payment1.getID().compareTo(payment2.getID());
42
+        Arrays.sort(payments,compareById);
43
+        return returnDescriptionsAsString(getArrayOfDescriptions(payments));
44
+    }
5
 
45
 
6
-}
46
+}

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

1
+package com.zipcoder.payment;
2
+
3
+import java.util.Comparator;
4
+
5
+public class PaymentSortByPayer implements Comparator<Payment> {
6
+
7
+    public int compare(Payment o1, Payment o2) {
8
+        return o1.getPayerName().compareTo(o2.getPayerName());
9
+    }
10
+}

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

64
 
64
 
65
         Assert.assertEquals(expected,check.getLastFourDigits());
65
         Assert.assertEquals(expected,check.getLastFourDigits());
66
     }
66
     }
67
+
68
+    @Test
69
+    public void compareToDifferentCheckTest(){
70
+        Check check = new Check("John Jacob", "12345");
71
+        Check check1 = new Check("Jingleheimer Schmidt", "45678");
72
+
73
+        Assert.assertTrue(check.compareTo(check1) > 0);
74
+    }
75
+
76
+    @Test
77
+    public void compareToCheckPayPalTest(){
78
+        Check check = new Check("John Jacob", "12345");
79
+        PayPal payPal = new PayPal();
80
+
81
+        Assert.assertTrue(payPal.compareTo(check) > 0);
82
+    }
67
 }
83
 }

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

73
 
73
 
74
         Assert.assertEquals(expected,creditCard.getLastFourDigits());
74
         Assert.assertEquals(expected,creditCard.getLastFourDigits());
75
     }
75
     }
76
+
77
+    @Test
78
+    public void compareToDifferentCCTest(){
79
+        CreditCard creditCard = new CreditCard("John Jacob", "1234567890123456", 12, 2018);
80
+        CreditCard creditCard1 = new CreditCard("Jingleheimer Schmidt", "0987654321098765",04,2019);
81
+
82
+        Assert.assertTrue(creditCard.compareTo(creditCard1) > 0);
83
+    }
84
+
85
+    @Test
86
+    public void compareToCCCheckTest(){
87
+        CreditCard creditCard = new CreditCard("John Jacob", "1234567890123456", 12, 2018);
88
+        Check check = new Check("John Jacob", "12345");
89
+
90
+        Assert.assertTrue(creditCard.compareTo(check) < 0);
91
+    }
76
 }
92
 }

+ 16
- 0
src/test/java/com/zipcoder/payment/PayPalTest.java 查看文件

43
 
43
 
44
         Assert.assertEquals(expected, payPal.getShortDescription());
44
         Assert.assertEquals(expected, payPal.getShortDescription());
45
     }
45
     }
46
+
47
+    @Test
48
+    public void compareToDifferentPayPalTest(){
49
+        PayPal payPal = new PayPal();
50
+        PayPal payPal1 = new PayPal();
51
+
52
+        Assert.assertTrue(payPal.compareTo(payPal1) == 0);
53
+    }
54
+
55
+    @Test
56
+    public void compareToCCPayPalTest(){
57
+        CreditCard creditCard = new CreditCard("John Jacob", "1234567890123456", 12, 2018);
58
+        PayPal payPal = new PayPal();
59
+
60
+        Assert.assertTrue(payPal.compareTo(creditCard) > 0);
61
+    }
46
 }
62
 }

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

1
 package com.zipcoder.payment;
1
 package com.zipcoder.payment;
2
 
2
 
3
+import org.junit.Test;
4
+import org.junit.Assert;
5
+
3
 public class PaymentPresenterTest {
6
 public class PaymentPresenterTest {
7
+    @Test
8
+    public void noPaymentTest() {
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 multiplePaymentsTest() {
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 toStringByPayerTest() {
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 toStringByIdTest() {
52
+        Payment[] payments = new Payment[2];
53
+        Payment paypal = new PayPal(81L, "Tamara Mowry", "tamara@mowry.com");
54
+        Payment check = new Check(120L, "Tia Mowry", "11432543", "134344551");
55
+
56
+        payments[0] = paypal;
57
+        payments[1] = check;
58
+
59
+        PaymentPresenter presenter = new PaymentPresenter();
60
+        String expected = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
61
+
62
+        String actual = presenter.toStringById(payments);
63
+        Assert.assertEquals(expected, actual);
64
+    }
4
 }
65
 }

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

1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class PaymentSortByPayerTest {
7
+    @Test
8
+    public void compareDifferentCCTest() {
9
+        CreditCard creditCard = new CreditCard("John Jacob", "1234567890123456", 12, 2018);
10
+        CreditCard creditCard1 = new CreditCard("Jingleheimer Schmidt", "0987654321098765", 04, 2019);
11
+
12
+        Assert.assertTrue(creditCard.compareTo(creditCard1) > 0);
13
+    }
14
+
15
+    @Test
16
+    public void compareCCCheckTest() {
17
+        CreditCard creditCard = new CreditCard("John Jacob", "1234567890123456", 12, 2018);
18
+        Check check = new Check("John Jacob", "12345");
19
+
20
+        Assert.assertTrue(creditCard.compareTo(check) < 0);
21
+    }
22
+
23
+    @Test
24
+    public void compareToDifferentCheckTest(){
25
+        Check check = new Check("John Jacob", "12345");
26
+        Check check1 = new Check("Jingleheimer Schmidt", "45678");
27
+
28
+        Assert.assertTrue(check.compareTo(check1) > 0);
29
+    }
30
+
31
+    @Test
32
+    public void compareToCheckPayPalTest(){
33
+        Check check = new Check("John Jacob", "12345");
34
+        PayPal payPal = new PayPal();
35
+
36
+        Assert.assertTrue(payPal.compareTo(check) > 0);
37
+    }
38
+
39
+    @Test
40
+    public void compareToDifferentPayPalTest(){
41
+        PayPal payPal = new PayPal();
42
+        PayPal payPal1 = new PayPal();
43
+
44
+        Assert.assertTrue(payPal.compareTo(payPal1) == 0);
45
+    }
46
+
47
+    @Test
48
+    public void compareToCCPayPalTest(){
49
+        CreditCard creditCard = new CreditCard("John Jacob", "1234567890123456", 12, 2018);
50
+        PayPal payPal = new PayPal();
51
+
52
+        Assert.assertTrue(payPal.compareTo(creditCard) > 0);
53
+    }
54
+}
55
+