Explorar el Código

the rest of the lab

yauhenip hace 8 años
padre
commit
26e7e8973a

+ 20
- 0
src/main/java/com/zipcoder/payment/Check.java Ver fichero

7
     private String routingNumber;
7
     private String routingNumber;
8
     private String accountNumber;
8
     private String accountNumber;
9
 
9
 
10
+    public Check() {
11
+
12
+    }
13
+
14
+    public Check(String payerName, String accountNumber) {
15
+        this.payerName = payerName;
16
+        this.accountNumber = accountNumber;
17
+    }
18
+
19
+    public Check(Long id, String payerName, String routingNumber, String accountNumber) {
20
+        this.id = id;
21
+        this.payerName = payerName;
22
+        this.accountNumber = accountNumber;
23
+        this.routingNumber = routingNumber;
24
+    }
25
+
10
     public Long getId() {
26
     public Long getId() {
11
         return this.id;
27
         return this.id;
12
     }
28
     }
43
     public void setAccountNumber(String accountNumber) {
59
     public void setAccountNumber(String accountNumber) {
44
         this.accountNumber = accountNumber;
60
         this.accountNumber = accountNumber;
45
     }
61
     }
62
+
63
+    public int compareTo(Payment other) {
64
+        return this.getShortDescription().compareTo(other.getShortDescription());
65
+    }
46
 }
66
 }

+ 4
- 0
src/main/java/com/zipcoder/payment/CreditCard.java Ver fichero

53
     public void setExpiredYear(int expiredYear) {
53
     public void setExpiredYear(int expiredYear) {
54
         this.expiredYear = expiredYear;
54
         this.expiredYear = expiredYear;
55
     }
55
     }
56
+
57
+    public int compareTo(Payment other) {
58
+        return this.getShortDescription().compareTo(other.getShortDescription());
59
+    }
56
 }
60
 }

+ 10
- 0
src/main/java/com/zipcoder/payment/PayPal.java Ver fichero

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

+ 3
- 1
src/main/java/com/zipcoder/payment/Payment.java Ver fichero

1
 package com.zipcoder.payment;
1
 package com.zipcoder.payment;
2
 
2
 
3
-public interface Payment {
3
+public interface Payment extends Comparable<Payment>{
4
 
4
 
5
     public Long getId();
5
     public Long getId();
6
 
6
 
7
     public String getPayerName();
7
     public String getPayerName();
8
 
8
 
9
     public String getShortDescription();
9
     public String getShortDescription();
10
+
11
+    public int compareTo(Payment other);
10
 }
12
 }

+ 32
- 1
src/main/java/com/zipcoder/payment/PaymentPresenter.java Ver fichero

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

+ 10
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java Ver fichero

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 first, Payment second) {
8
+        return first.getPayerName().compareTo(second.getPayerName());
9
+    }
10
+}

+ 33
- 0
src/test/java/com/zipcoder/payment/CompareToTest.java Ver fichero

1
+package com.zipcoder.payment;
2
+
3
+import org.junit.jupiter.api.Test;
4
+
5
+import static org.junit.jupiter.api.Assertions.assertEquals;
6
+import static org.junit.jupiter.api.Assertions.assertTrue;
7
+
8
+public class CompareToTest {
9
+
10
+    @Test
11
+    public void compareToNegativeTest(){
12
+        Check myCheck = new Check("Dan Rivas", "1243567895");
13
+        PayPal pp = new PayPal("Shane McNaulty", "shane@loews.com");
14
+        int actual = myCheck.compareTo(pp);
15
+        assertTrue(actual < 0);
16
+    }
17
+
18
+    @Test
19
+    public void compareToPositiveTest(){
20
+        Check myCheck = new Check("Dan Rivas", "1243567895");
21
+        PayPal pp = new PayPal("Shane McNaulty", "shane@loews.com");
22
+        int actual = pp.compareTo(myCheck);
23
+        assertTrue(actual > 0);
24
+    }
25
+
26
+    @Test
27
+    public void compareToZeroTest(){
28
+        Payment myCheck = new Check("Dan Rivas", "1243567876");
29
+        Payment yourCheck = new Check("Dan Rivas", "1243567876");
30
+        int actual = myCheck.compareTo(yourCheck);
31
+        assertEquals(0, actual);
32
+    }
33
+}

+ 60
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Ver fichero

1
 package com.zipcoder.payment;
1
 package com.zipcoder.payment;
2
 
2
 
3
+import org.junit.jupiter.api.Test;
4
+
5
+import static org.junit.jupiter.api.Assertions.assertEquals;
6
+
3
 public class PaymentPresenterTest {
7
 public class PaymentPresenterTest {
8
+
9
+    @Test
10
+    public void paymentPresenterNoPaymentTest() {
11
+        Payment[] payments = new Payment[0];
12
+        PaymentPresenter presenter = new PaymentPresenter();
13
+        String expected = "";
14
+        String actual = presenter.toString(payments);
15
+        assertEquals(expected, actual);
16
+    }
17
+
18
+    @Test
19
+    public void paymentPresenterMultiplePaymentsTest() {
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
+        String actual = presenter.toString(payments);
30
+        assertEquals(expected, actual);
31
+    }
32
+
33
+    @Test
34
+    public void toStringByPayerNameTest() {
35
+        Payment[] payments = new Payment[2];
36
+        Payment paypal = new PayPal(4L, "Tamara Mowry", "tamara@mowry.com");
37
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
38
+
39
+        payments[0] = paypal;
40
+        payments[1] = check;
41
+
42
+        PaymentPresenter presenter = new PaymentPresenter();
43
+        String expected = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
44
+
45
+        String actual = presenter.toStringByPayerName(payments);
46
+        assertEquals(expected, actual);
47
+    }
48
+
49
+    @Test
50
+    public void toStringByIdTest() {
51
+        Payment[] payments = new Payment[2];
52
+        Payment paypal = new PayPal(120L, "Tamara Mowry", "tamara@mowry.com");
53
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
54
+
55
+        payments[0] = paypal;
56
+        payments[1] = check;
57
+
58
+        PaymentPresenter presenter = new PaymentPresenter();
59
+        String expected = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
60
+
61
+        String actual = presenter.toStringById(payments);
62
+        assertEquals(expected, actual);
63
+    }
4
 }
64
 }

+ 36
- 0
src/test/java/com/zipcoder/payment/PaymentSortByPayerTest.java Ver fichero

1
+package com.zipcoder.payment;
2
+
3
+import org.junit.jupiter.api.Test;
4
+
5
+import static org.junit.jupiter.api.Assertions.assertEquals;
6
+import static org.junit.jupiter.api.Assertions.assertTrue;
7
+
8
+public class PaymentSortByPayerTest {
9
+
10
+    PaymentSortByPayer pmnt = new PaymentSortByPayer();
11
+
12
+    @Test
13
+    public void compareNegativeTest(){
14
+        Check myCheck = new Check("Dan Rivas", "1243567895");
15
+        PayPal pp = new PayPal("Shane McNaulty", "shane@loews.com");
16
+        int actual = pmnt.compare(myCheck, pp);
17
+        assertTrue(actual < 0);
18
+    }
19
+
20
+    @Test
21
+    public void comparePositiveTest(){
22
+        Check myCheck = new Check("Dan Rivas", "1243567895");
23
+        PayPal pp = new PayPal("Shane McNaulty", "shane@loews.com");
24
+        int actual = pmnt.compare(pp, myCheck);
25
+        assertTrue(actual > 0);
26
+    }
27
+
28
+    @Test
29
+    public void compareZeroTest(){
30
+        Payment myCheck = new Check("Dan Rivas", "1243567876");
31
+        Payment cc = new CreditCard();
32
+        ((CreditCard) cc).setPayerName("Dan Rivas");
33
+        int actual = pmnt.compare(cc, myCheck);
34
+        assertEquals(0, actual);
35
+    }
36
+}