Aleena Rose-Mathew 8 lat temu
rodzic
commit
c3c0dd5bef

+ 12
- 0
pom.xml Wyświetl plik

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcoder</groupId>
8 8
     <artifactId>payment</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>8</source>
17
+                    <target>8</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
     <dependencies>
11 23
         <dependency>
12 24
             <groupId>org.junit.jupiter</groupId>

+ 21
- 7
src/main/java/com/zipcoder/payment/Check.java Wyświetl plik

@@ -9,10 +9,10 @@ public class Check implements Payment {
9 9
         this.accountNumber=accountNumber;
10 10
 
11 11
     }
12
-    long id;
13
-    String payerName;
14
-    String routingNumber;
15
-    String accountNumber;
12
+    private long id;
13
+    private String payerName;
14
+    private String routingNumber;
15
+    private String accountNumber;
16 16
     public long getId()
17 17
     {
18 18
         return this.id;
@@ -49,12 +49,26 @@ public class Check implements Payment {
49 49
     {
50 50
         String number=getaccountNumber();
51 51
         StringBuilder br =new StringBuilder();
52
-        br.append("Check").append(" ").append(this.getPayerName()).append(" ").
53
-                append(" ").append("****").append(number.substring(number.length()-4));
52
+        br.append("Check").append(" ").append(this.getPayerName()).append(" ")
53
+                .append("***").append(number.substring(number.length()-4));
54 54
         System.out.println(br.toString());
55 55
         return br.toString();
56 56
     }
57 57
     public int compareTo(Payment o) {
58
-       return this.getShortDescription().compareTo(o.getShortDescription());
58
+        int number=0;
59
+
60
+       if(this.getShortDescription().compareTo(o.getShortDescription())==0)
61
+       {
62
+           number=0;
63
+       }
64
+        if(this.getShortDescription().compareTo(o.getShortDescription())<0)
65
+        {
66
+            number=-1;
67
+        }
68
+        if(this.getShortDescription().compareTo(o.getShortDescription())>0)
69
+        {
70
+            number=1;
71
+        }
72
+        return number;
59 73
     }
60 74
 }

+ 15
- 1
src/main/java/com/zipcoder/payment/CreditCard.java Wyświetl plik

@@ -67,6 +67,20 @@ public class CreditCard implements Payment{
67 67
     }
68 68
 
69 69
     public int compareTo(Payment o) {
70
-        return this.getShortDescription().compareTo(o.getShortDescription());
70
+        int number=0;
71
+
72
+        if(this.getShortDescription().compareTo(o.getShortDescription())==0)
73
+        {
74
+            number=0;
75
+        }
76
+        if(this.getShortDescription().compareTo(o.getShortDescription())<0)
77
+        {
78
+            number=-1;
79
+        }
80
+        if(this.getShortDescription().compareTo(o.getShortDescription())>0)
81
+        {
82
+            number=1;
83
+        }
84
+        return number;
71 85
     }
72 86
 }

+ 37
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Wyświetl plik

@@ -1,6 +1,43 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 public class PaymentPresenter {
6
+    PaymentSortByPayer sorter;
7
+    public String toString(Payment[] pay)
8
+    {
9
+        Arrays.sort(pay);
10
+        StringBuilder br=new StringBuilder();
11
+        for(Payment o:pay)//For each Payment object in pay array
12
+        {
13
+
14
+            br.append(o.getShortDescription()+"\n");
15
+        }
16
+        return br.toString();
17
+    }
18
+    public String toStringName(Payment[] pay)
19
+    {
20
+        Arrays.sort(pay,new PaymentSortByPayer());
21
+        StringBuilder br=new StringBuilder();
22
+        for(int i=0;i<pay.length;i++)
23
+        {
24
+
25
+            br.append(pay[i].getShortDescription()+"\n");
26
+        }
27
+        return br.toString();
28
+    }
29
+    public String toStringById(Payment[] pay)
30
+    {
31
+        Arrays.sort(pay, (Payment o1,Payment o2) -> Long.compare(o1.getId(),o2.getId()));
32
+        StringBuilder br;
33
+        br = new StringBuilder();
34
+        for(int i=0;i<pay.length;i++)
35
+        {
36
+            br.append(pay[i].getShortDescription()+"\n");
37
+        }
38
+        return br.toString();
39
+    }
40
+
4 41
 
5 42
 
6 43
 }

+ 8
- 6
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java Wyświetl plik

@@ -1,12 +1,14 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
-public class PaymentSortByPayer implements Comparable<Payment> {
4
-    public int compareTo(Payment o) {
5
-        return 0;
6
-    }
7
-    public int compare(Payment o1,Payment o2)
3
+import java.util.Comparator;
8 4
 
5
+public class PaymentSortByPayer implements Comparator<Payment> {
6
+//string-comparable
7
+    //Comparator->can compare long,int(two objects)
8
+    public int compare(Payment o1,Payment o2)
9 9
     {
10
-     return o1.getPayerName().compareTo(o2.getPayerName());
10
+        return o1.getPayerName().compareTo(o2.getPayerName());
11 11
     }
12
+
13
+
12 14
 }

+ 15
- 1
src/main/java/com/zipcoder/payment/Paypal.java Wyświetl plik

@@ -46,6 +46,20 @@ public class Paypal implements Payment {
46 46
     }
47 47
 
48 48
     public int compareTo(Payment o) {
49
-        return this.getShortDescription().compareTo(o.getShortDescription());
49
+        int number=0;
50
+
51
+        if(this.getShortDescription().compareTo(o.getShortDescription())==0)
52
+        {
53
+            number=0;
54
+        }
55
+        if(this.getShortDescription().compareTo(o.getShortDescription())<0)
56
+        {
57
+            number=-1;
58
+        }
59
+        if(this.getShortDescription().compareTo(o.getShortDescription())>0)
60
+        {
61
+            number=1;
62
+        }
63
+        return number;
50 64
     }
51 65
 }

+ 64
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Wyświetl plik

@@ -1,4 +1,68 @@
1 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 7
 public class PaymentPresenterTest {
8
+    @Test
9
+    public void testnoPayment()
10
+    {
11
+        Payment[] payments = new Payment[0];
12
+        PaymentPresenter presenter = new PaymentPresenter();
13
+        String expected = "";
14
+
15
+        String actual = presenter.toString(payments);
16
+
17
+        assertEquals(expected, actual);
18
+    }
19
+    @Test
20
+    public void multiplepayments()
21
+    {
22
+        Payment[] payments = new Payment[2];
23
+        Payment paypal = new Paypal(4, "Tia Mowry", "tia@mowry.com");
24
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
25
+
26
+        payments[0] = paypal;
27
+        payments[1] = check;
28
+
29
+        PaymentPresenter presenter = new PaymentPresenter();
30
+        String expected = "Check Tia Mowry ***4551\nPaypal Tia Mowry tia@mowry.com\n";
31
+
32
+        String actual = presenter.toString(payments);
33
+        assertEquals(expected, actual);
34
+    }
35
+
36
+    @Test
37
+    public void testtoStringByPayerName()
38
+    {
39
+        Payment[] payments = new Payment[2];
40
+        Payment paypal = new Paypal(4, "Tamara Mowry", "tamara@mowry.com");
41
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
42
+
43
+        payments[0] = paypal;
44
+        payments[1] = check;
45
+
46
+        PaymentPresenter presenter = new PaymentPresenter();
47
+        String expected = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
48
+
49
+        String actual = presenter.toStringName(payments);
50
+        assertEquals(expected, actual);
51
+    }
52
+    @Test
53
+    public void testtoStringById()
54
+    {
55
+        Payment[] payments = new Payment[2];
56
+        Payment paypal = new Paypal(120, "Tamara Mowry", "tamara@mowry.com");
57
+        Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551");
58
+
59
+        payments[0] = paypal;
60
+        payments[1] = check;
61
+
62
+        PaymentPresenter presenter = new PaymentPresenter();
63
+        String expected = "Check Tia Mowry ***4551\nPaypal Tamara Mowry tamara@mowry.com\n";
64
+
65
+        String actual = presenter.toStringById(payments);
66
+        assertEquals(expected, actual);
67
+    }
4 68
 }