Kaynağa Gözat

finished lab

Eric Foster 8 yıl önce
ebeveyn
işleme
98c3591e91

+ 10
- 0
pom.xml Dosyayı Görüntüle

8
     <artifactId>payment</artifactId>
8
     <artifactId>payment</artifactId>
9
     <version>1.0-SNAPSHOT</version>
9
     <version>1.0-SNAPSHOT</version>
10
 
10
 
11
+    <dependencies>
12
+        <!-- https://mvnrepository.com/artifact/junit/junit -->
13
+        <dependency>
14
+            <groupId>junit</groupId>
15
+            <artifactId>junit</artifactId>
16
+            <version>4.12</version>
17
+            <scope>test</scope>
18
+        </dependency>
19
+    </dependencies>
20
+
11
 
21
 
12
 </project>
22
 </project>

+ 66
- 0
src/main/java/com/zipcoder/payment/Check.java Dosyayı Görüntüle

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(){
10
+    }
11
+
12
+    public Check(Long id, String payerName, String routingNumber, String accountNumber){
13
+        this.id = id;
14
+        this.payerName = payerName;
15
+        this.routingNumber = routingNumber;
16
+        this.accountNumber = accountNumber;
17
+    }
18
+
19
+    public Long getId() {
20
+        return id;
21
+    }
22
+
23
+    public void setId(long id) {
24
+        this.id = id;
25
+    }
26
+
27
+    public String getPayerName() {
28
+        return payerName;
29
+    }
30
+
31
+    public void setPayerName(String payerName) {
32
+        this.payerName = payerName;
33
+    }
34
+
35
+    public String getRoutingNumber() {
36
+        return routingNumber;
37
+    }
38
+
39
+    public void setRoutingNumber(String routingNumber) {
40
+        this.routingNumber = routingNumber;
41
+    }
42
+
43
+    public String getAccountNumber() {
44
+        return accountNumber;
45
+    }
46
+
47
+    public void setAccountNumber(String accountNumber) {
48
+        this.accountNumber = accountNumber;
49
+    }
50
+
51
+    public String getShortDescription() {
52
+        return ("Check " + getPayerName() + " ***" + getLast4OfAccountNumber());
53
+    }
54
+
55
+    public String getLast4OfAccountNumber() {
56
+        StringBuilder last4OfAccount = new StringBuilder();
57
+        for (int i = accountNumber.length()-4; i<accountNumber.length(); i++){
58
+            last4OfAccount.append(accountNumber.charAt(i));
59
+        }
60
+        return last4OfAccount.toString();
61
+    }
62
+
63
+    public int compareTo(Payment o) {
64
+        return this.id.compareTo(o.getId());
65
+    }
66
+}

+ 65
- 0
src/main/java/com/zipcoder/payment/CreditCard.java Dosyayı Görüntüle

1
+package com.zipcoder.payment;
2
+
3
+public class CreditCard implements Payment {
4
+    Long id;
5
+    String payerName;
6
+    String number;
7
+    int expiredMonth;
8
+    int expiredYear;
9
+
10
+    public Long getId() {
11
+        return id;
12
+    }
13
+
14
+    public void setId(long id){
15
+        this.id = id;
16
+    }
17
+
18
+    public String getPayerName() {
19
+        return payerName;
20
+    }
21
+
22
+    public void setPayerName(String payerName) {
23
+        this.payerName = payerName;
24
+    }
25
+
26
+    public String getNumber() {
27
+        return number;
28
+    }
29
+
30
+    public void setNumber(String number) {
31
+        this.number = number;
32
+    }
33
+
34
+    public String getExpiredMonth() {
35
+        return String.format("%02d", expiredMonth);
36
+    }
37
+
38
+    public void setExpiredMonth(int expiredMonth) {
39
+        this.expiredMonth = expiredMonth;
40
+    }
41
+
42
+    public String getExpiredYear() {
43
+        return String.format("%02d", expiredYear);
44
+    }
45
+
46
+    public void setExpiredYear(int expiredYear) {
47
+        this.expiredYear = expiredYear;
48
+    }
49
+
50
+    public String getShortDescription() {
51
+        return ("CC " + payerName + " " + getLast4OfNumber() + " " + getExpiredMonth() + "/" + getExpiredYear());
52
+    }
53
+
54
+    private String getLast4OfNumber(){
55
+        StringBuilder last4OfNumber = new StringBuilder();
56
+        for (int i = number.length()-4; i<number.length(); i++){
57
+            last4OfNumber.append(number.charAt(i));
58
+        }
59
+        return last4OfNumber.toString();
60
+    }
61
+
62
+    public int compareTo(Payment o) {
63
+        return this.id.compareTo(o.getId());
64
+    }
65
+}

+ 7
- 0
src/main/java/com/zipcoder/payment/Payment.java Dosyayı Görüntüle

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

+ 29
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Dosyayı Görüntüle

1
 package com.zipcoder.payment;
1
 package com.zipcoder.payment;
2
+import java.util.Arrays;
2
 
3
 
3
 public class PaymentPresenter {
4
 public class PaymentPresenter {
5
+    public String toString(Payment[] payments){
6
+        Arrays.sort(payments, new PaymentSortByShortDescription());
7
+        StringBuilder s = new StringBuilder();
8
+        for (Payment p : payments){
9
+            s.append(p.getShortDescription());
10
+            s.append("\n");
11
+        }
12
+        return s.toString();
13
+    }
4
 
14
 
15
+    public String toStringByPayerName(Payment[] payments){
16
+        Arrays.sort(payments, new PaymentSortByPayer());
17
+        StringBuilder s = new StringBuilder();
18
+        for (Payment p : payments){
19
+            s.append(p.getShortDescription());
20
+            s.append("\n");
21
+        }
22
+        return s.toString();
23
+    }
24
+
25
+    public String toStringById(Payment[] payments){
26
+        Arrays.sort(payments, new PaymentSortById());
27
+        StringBuilder s = new StringBuilder();
28
+        for (Payment p : payments){
29
+            s.append(p.getShortDescription());
30
+            s.append("\n");
31
+        }
32
+        return s.toString();
33
+    }
5
 
34
 
6
 }
35
 }

+ 9
- 0
src/main/java/com/zipcoder/payment/PaymentSortById.java Dosyayı Görüntüle

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

+ 8
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java Dosyayı Görüntüle

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

+ 10
- 0
src/main/java/com/zipcoder/payment/PaymentSortByShortDescription.java Dosyayı Görüntüle

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

+ 48
- 0
src/main/java/com/zipcoder/payment/Paypal.java Dosyayı Görüntüle

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

+ 87
- 0
src/test/java/com/zipcoder/payment/CheckTest.java Dosyayı Görüntüle

1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.*;
6
+
7
+public class CheckTest {
8
+
9
+    @Test
10
+    public void getSetIDTest() {
11
+        Check check = new Check();
12
+        long expected = 12345;
13
+        check.setId(expected);
14
+        long actual = check.getId();
15
+        assertEquals(expected, actual);
16
+    }
17
+
18
+    @Test
19
+    public void getSetPayerNameTest() {
20
+        Check check = new Check();
21
+        String expected = "John Doe";
22
+        check.setPayerName(expected);
23
+        String actual = check.getPayerName();
24
+        assertEquals(expected, actual);
25
+    }
26
+
27
+    @Test
28
+    public void getSetRoutingNumberTest() {
29
+        Check check = new Check();
30
+        String expected = "00012345";
31
+        check.setRoutingNumber(expected);
32
+        String actual = check.getRoutingNumber();
33
+        assertEquals(expected, actual);
34
+    }
35
+
36
+    @Test
37
+    public void getSetAccountNumberTest() {
38
+        Check check = new Check();
39
+        String expected = "123456789";
40
+        check.setAccountNumber(expected);
41
+        String actual = check.getAccountNumber();
42
+        assertEquals(expected, actual);
43
+    }
44
+
45
+    @Test
46
+    public void getShortDescription() {
47
+        Check check = new Check();
48
+        String expected = "Check John Doe ***6789";
49
+        check.setPayerName("John Doe");
50
+        check.setAccountNumber("123456789");
51
+        String actual = check.getShortDescription();
52
+        assertEquals(expected, actual);
53
+    }
54
+
55
+    @Test
56
+    public void compareToPaypalTest() {
57
+        Paypal paypal = new Paypal();
58
+        paypal.setId(2);
59
+        Check check = new Check();
60
+        check.setId(1);
61
+        int actual = check.compareTo(paypal);
62
+        int expected = -1;
63
+        assertEquals(expected, actual);
64
+    }
65
+
66
+    @Test
67
+    public void compareToCreditCardTest() {
68
+        Paypal paypal = new Paypal();
69
+        paypal.setId(1);
70
+        Check check = new Check();
71
+        check.setId(1);
72
+        int actual = check.compareTo(paypal);
73
+        int expected = 0;
74
+        assertEquals(expected, actual);
75
+    }
76
+
77
+    @Test
78
+    public void compareToCreditCardTest2() {
79
+        Check check = new Check();
80
+        check.setId(1);
81
+        CreditCard creditCard = new CreditCard();
82
+        creditCard.setId(0);
83
+        int actual = check.compareTo(creditCard);
84
+        int expected = 1;
85
+        assertEquals(expected, actual);
86
+    }
87
+}

+ 95
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java Dosyayı Görüntüle

1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.*;
6
+
7
+public class CreditCardTest {
8
+
9
+    @Test
10
+    public void getSetIDTest() {
11
+        CreditCard creditCard = new CreditCard();
12
+        long expected = 12345;
13
+        creditCard.setId(expected);
14
+        long actual = creditCard.getId();
15
+        assertEquals(expected, actual);
16
+    }
17
+    @Test
18
+    public void getSetPayerNameTest() {
19
+        CreditCard creditCard = new CreditCard();
20
+        String expected = "John Doe";
21
+        creditCard.setPayerName(expected);
22
+        String actual = creditCard.getPayerName();
23
+        assertEquals(expected, actual);
24
+    }
25
+    @Test
26
+    public void getSetNumberTest() {
27
+        CreditCard creditCard = new CreditCard();
28
+        String expected = "1234567890123456";
29
+        creditCard.setNumber(expected);
30
+        String actual = creditCard.getNumber();
31
+        assertEquals(expected, actual);
32
+    }
33
+    @Test
34
+    public void getSetExpiredMonthTest() {
35
+        CreditCard creditCard = new CreditCard();
36
+        int expectedInt = 01;
37
+        String expected = "01";
38
+        creditCard.setExpiredMonth(expectedInt);
39
+        String actual = creditCard.getExpiredMonth();
40
+        assertEquals(expected, actual);
41
+    }
42
+    @Test
43
+    public void getSetExpiredYearTest() {
44
+        CreditCard creditCard = new CreditCard();
45
+        int expectedInt = 20;
46
+        String expected = "20";
47
+        creditCard.setExpiredYear(expectedInt);
48
+        String actual = creditCard.getExpiredYear();
49
+        assertEquals(expected, actual);
50
+    }
51
+    @Test
52
+    public void getShortDescriptionTest() {
53
+        CreditCard creditCard = new CreditCard();
54
+        creditCard.setPayerName("John Doe");
55
+        creditCard.setNumber("1234567890123456");
56
+        creditCard.setExpiredMonth(01);
57
+        creditCard.setExpiredYear(20);
58
+        String actual = creditCard.getShortDescription();
59
+        String expected = "CC John Doe 3456 01/20";
60
+        assertEquals(expected, actual);
61
+    }
62
+
63
+    @Test
64
+    public void compareToCheckTest() {
65
+        CreditCard creditCard = new CreditCard();
66
+        creditCard.setId(1);
67
+        Check check = new Check();
68
+        check.setId(2);
69
+        int actual = creditCard.compareTo(check);
70
+        int expected = -1;
71
+        assertEquals(expected, actual);
72
+    }
73
+
74
+    @Test
75
+    public void compareToPaypalTest() {
76
+        Paypal paypal = new Paypal();
77
+        paypal.setId(1);
78
+        CreditCard creditCard = new CreditCard();
79
+        creditCard.setId(1);
80
+        int actual = creditCard.compareTo(paypal);
81
+        int expected = 0;
82
+        assertEquals(expected, actual);
83
+    }
84
+
85
+    @Test
86
+    public void compareToPaypalTest2() {
87
+        Paypal paypal = new Paypal();
88
+        paypal.setId(0);
89
+        CreditCard creditCard = new CreditCard();
90
+        creditCard.setId(1);
91
+        int actual = creditCard.compareTo(paypal);
92
+        int expected = 1;
93
+        assertEquals(expected, actual);
94
+    }
95
+}

+ 63
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Dosyayı Görüntüle

1
 package com.zipcoder.payment;
1
 package com.zipcoder.payment;
2
 
2
 
3
+import org.junit.Test;
4
+import static org.junit.Assert.*;
5
+
3
 public class PaymentPresenterTest {
6
 public class PaymentPresenterTest {
7
+
8
+    @Test
9
+    public void toStringTest(){
10
+        Payment[] payments = new Payment[0];
11
+        PaymentPresenter presenter = new PaymentPresenter();
12
+        String expected = "";
13
+
14
+        String actual = presenter.toString(payments);
15
+
16
+        assertEquals(expected, actual);
17
+    }
18
+
19
+    @Test
20
+    public void toStringMultiplePaymentsTest(){
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
+        assertEquals(expected, actual);
33
+    }
34
+
35
+    @Test
36
+    public void toStringByPayerNameTest(){
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
+        assertEquals(expected, actual);
49
+    }
50
+
51
+    @Test
52
+    public void toStringById(){
53
+        Payment[] payments = new Payment[2];
54
+        Payment paypal = new Paypal(81L, "Tamara Mowry", "tamara@mowry.com");
55
+        Payment check = new Check(120L, "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
+        assertEquals(expected, actual);
65
+    }
66
+
4
 }
67
 }

+ 44
- 0
src/test/java/com/zipcoder/payment/PaymentSortByPayerTest.java Dosyayı Görüntüle

1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.*;
6
+
7
+public class PaymentSortByPayerTest {
8
+
9
+    @Test
10
+    public void compareBeforePayment() {
11
+        PaymentSortByPayer paymentSortByPayer = new PaymentSortByPayer();
12
+        Payment o1 = new CreditCard();
13
+        ((CreditCard) o1).setId(1);
14
+        Payment o2 = new CreditCard();
15
+        ((CreditCard) o2).setId(2);
16
+        int actual = paymentSortByPayer.compare(o1, o2);
17
+        int expected = -1;
18
+        assertEquals(expected, actual);
19
+    }
20
+
21
+    @Test
22
+    public void compareSimultaneousPayment() {
23
+        PaymentSortByPayer paymentSortByPayer = new PaymentSortByPayer();
24
+        Payment o1 = new CreditCard();
25
+        ((CreditCard) o1).setId(1);
26
+        Payment o2 = new CreditCard();
27
+        ((CreditCard) o2).setId(1);
28
+        int actual = paymentSortByPayer.compare(o1, o2);
29
+        int expected = 0;
30
+        assertEquals(expected, actual);
31
+    }
32
+
33
+    @Test
34
+    public void compareAfterPayment() {
35
+        PaymentSortByPayer paymentSortByPayer = new PaymentSortByPayer();
36
+        Payment o1 = new CreditCard();
37
+        ((CreditCard) o1).setId(1);
38
+        Payment o2 = new CreditCard();
39
+        ((CreditCard) o2).setId(0);
40
+        int actual = paymentSortByPayer.compare(o1, o2);
41
+        int expected = 1;
42
+        assertEquals(expected, actual);
43
+    }
44
+}

+ 78
- 0
src/test/java/com/zipcoder/payment/PaypalTest.java Dosyayı Görüntüle

1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.*;
6
+
7
+public class PaypalTest {
8
+
9
+    @Test
10
+    public void getSetId() {
11
+        Paypal paypal = new Paypal();
12
+        long expected = 12345;
13
+        paypal.setId(expected);
14
+        long actual = paypal.getId();
15
+        assertEquals(expected, actual);
16
+    }
17
+
18
+    @Test
19
+    public void getSetPayerName() {
20
+        Paypal paypal = new Paypal();
21
+        String expected = "John Doe";
22
+        paypal.setPayerName(expected);
23
+        String actual = paypal.getPayerName();
24
+        assertEquals(expected, actual);
25
+    }
26
+
27
+    @Test
28
+    public void getSetEmail() {
29
+        Paypal paypal = new Paypal();
30
+        String expected = "johndoe@alias.com";
31
+        paypal.setEmail(expected);
32
+        String actual = paypal.getEmail();
33
+        assertEquals(expected, actual);
34
+    }
35
+
36
+    @Test
37
+    public void getShortDescription() {
38
+        Paypal paypal = new Paypal();
39
+        paypal.setPayerName("John Doe");
40
+        paypal.setEmail("johndoe@alias.com");
41
+        String expected = "Paypal John Doe johndoe@alias.com";
42
+        String actual = paypal.getShortDescription();
43
+        assertEquals(expected, actual);
44
+    }
45
+
46
+    @Test
47
+    public void compareToCheckTest() {
48
+        Paypal paypal = new Paypal();
49
+        paypal.setId(1);
50
+        Check check = new Check();
51
+        check.setId(2);
52
+        int actual = paypal.compareTo(check);
53
+        int expected = -1;
54
+        assertEquals(expected, actual);
55
+    }
56
+
57
+    @Test
58
+    public void compareToCheckTest2() {
59
+        Paypal paypal = new Paypal();
60
+        paypal.setId(1);
61
+        Check check = new Check();
62
+        check.setId(1);
63
+        int actual = paypal.compareTo(check);
64
+        int expected = 0;
65
+        assertEquals(expected, actual);
66
+    }
67
+
68
+    @Test
69
+    public void compareToCreditCardTest() {
70
+        Paypal paypal = new Paypal();
71
+        paypal.setId(1);
72
+        CreditCard creditCard = new CreditCard();
73
+        creditCard.setId(0);
74
+        int actual = paypal.compareTo(creditCard);
75
+        int expected = 1;
76
+        assertEquals(expected, actual);
77
+    }
78
+}