Roy 8 лет назад
Родитель
Сommit
748dc2e97b

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

+ 59
- 0
src/main/java/com/zipcoder/payment/Check.java Просмотреть файл

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

+ 67
- 0
src/main/java/com/zipcoder/payment/CreditCard.java Просмотреть файл

@@ -0,0 +1,67 @@
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 expireYear;
10
+
11
+    public CreditCard(long id, String payerName, int expiredMonth, int expireYear) {
12
+        this.id = id;
13
+        this.payerName = payerName;
14
+        this.expiredMonth = expiredMonth;
15
+        this.expireYear = expireYear;
16
+    }
17
+
18
+    public void setId(long id) {
19
+        this.id = id;
20
+    }
21
+
22
+    public long getId(){
23
+       return id ;
24
+    }
25
+    public String getPayerName(){
26
+        return payerName;
27
+    }
28
+
29
+    public void setPayerName(String name) {
30
+        this.payerName = name;
31
+    }
32
+
33
+    public String getNumber() {
34
+        return number;
35
+    }
36
+
37
+    public void setNumber(String number) {
38
+        this.number = number;
39
+    }
40
+
41
+    public int getExpiredMonth() {
42
+        return expiredMonth;
43
+    }
44
+
45
+    public void setExpiredMonth(int expiredMonth) {
46
+        this.expiredMonth = expiredMonth;
47
+    }
48
+
49
+    public int getExpireYear() {
50
+        return expireYear;
51
+    }
52
+
53
+    public void setExpireYear(int expireYear) {
54
+        this.expireYear = expireYear;
55
+    }
56
+
57
+    public String getShortDescrption(){
58
+        String num = number.substring(number.length()-4);
59
+        return "CC " + payerName + " "+ num+" "+expiredMonth+"/"+expireYear;
60
+    }
61
+
62
+
63
+    @Override
64
+    public int compareTo(Payment o) {
65
+        return this.getShortDescrption().compareTo(o.getShortDescrption());
66
+    }
67
+}

+ 52
- 0
src/main/java/com/zipcoder/payment/PayPal.java Просмотреть файл

@@ -0,0 +1,52 @@
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 long getId() {
17
+        return id;
18
+    }
19
+
20
+    public void setId(long id) {
21
+        this.id = id;
22
+    }
23
+
24
+    @Override
25
+    public String getPayerName() {
26
+        return payerName;
27
+    }
28
+
29
+    public void setPayerName(String payerName) {
30
+        this.payerName = payerName;
31
+    }
32
+
33
+    public String getEmail() {
34
+        return email;
35
+    }
36
+
37
+    public void setEmail(String email) {
38
+        this.email = email;
39
+    }
40
+
41
+    @Override
42
+    public String getShortDescrption(){
43
+        return "PayPal "+ payerName + " "+ email;
44
+    }
45
+
46
+    @Override
47
+    public int compareTo(Payment o) {
48
+        return this.getShortDescrption().compareTo(o.getShortDescrption());
49
+    }
50
+
51
+
52
+}

+ 8
- 0
src/main/java/com/zipcoder/payment/Payment.java Просмотреть файл

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

+ 33
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Просмотреть файл

@@ -1,6 +1,39 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.Arrays;
4
+import java.util.Comparator;
5
+
3 6
 public class PaymentPresenter {
4 7
 
8
+    PaymentSortByPayer payementSort = new PaymentSortByPayer();
9
+
10
+    public String toString(Payment[] payments){
11
+        StringBuilder desc = new StringBuilder();
12
+        Arrays.sort(payments, payementSort);
13
+        for(Payment pay: payments){
14
+            desc.append(pay.getShortDescrption()+"\n");
15
+        }
16
+        return desc.toString();
17
+    }
18
+
19
+    public String toStringByPayerName(Payment[] payments){
20
+        StringBuilder desc = new StringBuilder();
21
+        Arrays.sort(payments, Comparator.comparing(Payment::getPayerName));
22
+        for(Payment pay: payments){
23
+            desc.append(pay.getShortDescrption()+"\n");
24
+        }
25
+        return desc.toString();
26
+    }
27
+
28
+    public String toStringById(Payment[] payments){
29
+        StringBuilder desc = new StringBuilder();
30
+        Arrays.sort(payments, (Payment o1, Payment o2)->Long.compare(o2.getId(), o1.getId()));
31
+        for(Payment pay: payments){
32
+            desc.append(pay.getShortDescrption()+"\n");
33
+        }
34
+        return desc.toString();
35
+    }
36
+
37
+
5 38
 
6 39
 }

+ 11
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java Просмотреть файл

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

+ 27
- 0
src/test/java/com/zipcoder/payment/CheckTest.java Просмотреть файл

@@ -0,0 +1,27 @@
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
+    Check check;
9
+    @Before
10
+    public void CheckTest(){
11
+        check = new Check(1234, "Roy", "1234567", "89122356");
12
+    }
13
+
14
+    @Test
15
+    public void getIdTest(){
16
+        long expected = 1234;
17
+        long actual = check.getId();
18
+        Assert.assertEquals(actual, expected);
19
+    }
20
+
21
+    @Test
22
+    public void getShortDescriptiotest(){
23
+        String expected = "Check Roy ***2356";
24
+        String actual = check.getShortDescrption();
25
+        Assert.assertEquals(actual, expected);
26
+    }
27
+}

+ 36
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java Просмотреть файл

@@ -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 CreditCardTest {
8
+
9
+    CreditCard card;
10
+    @Before
11
+    public void CheckTest(){
12
+        card = new CreditCard(1234, "Roy", 6, 22);
13
+    }
14
+
15
+    @Test
16
+    public void getExpireYear(){
17
+        int actual = 22;
18
+        int expected = card.expireYear;
19
+        Assert.assertEquals(actual, expected);
20
+    }
21
+
22
+    @Test
23
+    public void getExprireMonthTest(){
24
+        int actual = 6;
25
+        int expected = card.expiredMonth;
26
+        Assert.assertEquals(actual, expected);
27
+    }
28
+
29
+    @Test
30
+    public void getShortDescriptiotest(){
31
+        card.setNumber("1234567890");
32
+        String actual = "CC Roy 7890 6/22";
33
+        String expected = card.getShortDescrption();
34
+        Assert.assertEquals(expected, actual);
35
+    }
36
+}

+ 36
- 0
src/test/java/com/zipcoder/payment/PayPalTest.java Просмотреть файл

@@ -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
+
9
+    PayPal payPal;
10
+    @Before
11
+    public void payPayTes(){
12
+        payPal = new PayPal(12345, "Roy", "wjievenjie@ymail.com");
13
+    }
14
+
15
+    @Test
16
+    public void getEmailTest(){
17
+        String actual = "wjievenjie@ymail.com";
18
+        String expected = payPal.getEmail();
19
+        Assert.assertEquals(actual, expected);
20
+    }
21
+
22
+    @Test
23
+    public void getShortDescriptiotest(){
24
+        String actual = "PayPal Roy wjievenjie@ymail.com";
25
+        String expected = payPal.getShortDescrption();
26
+        Assert.assertEquals(actual, expected);
27
+    }
28
+
29
+    @Test
30
+    public void setNameTest(){
31
+        payPal.setPayerName("Josh");
32
+        String actual = "Josh";
33
+        String expected = payPal.getPayerName();
34
+        Assert.assertEquals(actual, expected);
35
+    }
36
+}

+ 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 sortDescriptionTest(){
10
+        Payment[] payments = new Payment[2];
11
+        Payment paypal = new PayPal(4L, "Tia Mowry", "tia@mowry.com");
12
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
13
+
14
+        payments[0] = paypal;
15
+        payments[1] = check;
16
+
17
+        PaymentPresenter presenter = new PaymentPresenter();
18
+        String expected = "Check Tia Mowry ***4551\nPayPal Tia Mowry tia@mowry.com\n";
19
+
20
+        String actual = presenter.toString(payments);
21
+        Assert.assertEquals(expected, actual);
22
+    }
23
+
24
+    @Test
25
+    public void toStringTest(){
26
+        Payment[] payments = new Payment[0];
27
+        PaymentPresenter presenter = new PaymentPresenter();
28
+        String expected = "";
29
+
30
+        String actual = presenter.toString(payments);
31
+
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 toStringByIdTest(){
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 = "PayPal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
62
+
63
+        String actual = presenter.toStringById(payments);
64
+        Assert.assertEquals(expected, actual);
65
+    }
66
+
4 67
 }

+ 31
- 0
src/test/java/com/zipcoder/payment/PaymentSortByPayerTest.java Просмотреть файл

@@ -0,0 +1,31 @@
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 PaymentSortByPayerTest {
8
+
9
+    PaymentSortByPayer sort;
10
+    Payment pay1;
11
+    Payment pay2;
12
+    @Before
13
+    public void payPayTest() {
14
+        sort = new PaymentSortByPayer();
15
+        pay1 = new CreditCard(1234, "Roy", 6, 22);
16
+        pay2 = new PayPal(12345, "Roy", "wjievenjie@ymail.com");
17
+    }
18
+
19
+//    @Test
20
+//    public void compareTest(){
21
+//        //pay1.set
22
+//        int actual = "CC Roy ";
23
+//        int expected = sort.compare(pay1, pay2);
24
+//        Assert.assertEquals(expected, actual);
25
+//    }
26
+
27
+
28
+
29
+
30
+
31
+}