Khalil Malik Saboor 8 лет назад
Родитель
Сommit
9f56129420

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

@@ -7,7 +7,7 @@ public class Check implements Payment {
7 7
     private String accountNumber;
8 8
 
9 9
     Check(){
10
-        ID = 123;
10
+        ID = 1234567891;
11 11
         payerName =  "Tia Mowry";
12 12
         routingNumber = "34343456";
13 13
         accountNumber = "***4551";
@@ -25,6 +25,20 @@ public class Check implements Payment {
25 25
         return "Check " + payerName + " " + accountNumber;
26 26
     }
27 27
 
28
+    public int compareTo(Payment o) {
29
+        String origin = this.getShortDescription();
30
+        String compared = o.getShortDescription();
31
+        if (origin.length() == compared.length()) {
32
+            int difference = 0;
33
+            for (int i = 0; i < origin.length(); i++) {
34
+                difference += origin.charAt(i) - compared.charAt(i);
35
+            }
36
+            return difference;
37
+        } else {
38
+            return origin.length() - compared.length();
39
+        }
40
+    }
41
+
28 42
     public String getRoutingNumber() {
29 43
         return routingNumber;
30 44
     }

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

@@ -3,11 +3,13 @@ package com.zipcoder.payment;
3 3
 public class CreditCard implements Payment {
4 4
     //Define
5 5
     private String payerNumber;
6
+    private String payerName;
6 7
     private String number;
7
-    private long ID;
8 8
     private int expiredMonth;
9 9
     private int expiredYear;
10
-    private String payerName;
10
+    private long ID;
11
+
12
+
11 13
 
12 14
     public CreditCard(){
13 15
         ID = 1234567891;
@@ -36,6 +38,20 @@ public class CreditCard implements Payment {
36 38
         return "CC " + payerName + " " + payerNumber + " " + expiredMonth + "/" + expiredYear;
37 39
     }
38 40
 
41
+    public int compareTo(Payment o) {
42
+        String origin = this.getShortDescription();
43
+        String compared = o.getShortDescription();
44
+        if (origin.length() == compared.length()) {
45
+            int difference = 0;
46
+            for (int i = 0; i < origin.length(); i++) {
47
+                difference += origin.charAt(i) - compared.charAt(i);
48
+            }
49
+            return difference;
50
+        } else {
51
+            return origin.length() - compared.length();
52
+        }
53
+    }
54
+
39 55
     public int getExpiredMonth() {
40 56
         return expiredMonth;
41 57
     }
@@ -55,4 +71,6 @@ public class CreditCard implements Payment {
55 71
     public void setNumber(String number) {
56 72
         this.number = this.number;
57 73
     }
74
+
75
+
58 76
 }

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

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

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

@@ -0,0 +1,6 @@
1
+package com.zipcoder.payment;
2
+import java.util.Comparator;
3
+
4
+public class PaymentsSortByPayer {
5
+
6
+}

+ 22
- 4
src/main/java/com/zipcoder/payment/Paypal.java Просмотреть файл

@@ -6,9 +6,9 @@ public class Paypal implements Payment {
6 6
     private String email;
7 7
 
8 8
     Paypal (){
9
-        ID = 212131;
10
-        email = "khalil@gmail.com";
11
-        payerName = "Khalil";
9
+        ID = 1234567891;
10
+        email = "tia@mowry.com";
11
+        payerName = "Tia Mowry";
12 12
     }
13 13
 
14 14
     public Long getID() {
@@ -20,10 +20,28 @@ public class Paypal implements Payment {
20 20
     }
21 21
 
22 22
     public String getShortDescription() {
23
-        return "Paypal" + payerName +" "+ email;
23
+        return "Paypal " + payerName +" "+ email;
24 24
     }
25 25
 
26 26
     public void setID(long ID) {
27 27
         this.ID = ID;
28 28
     }
29
+
30
+    public void setPayerName(String payerName) {
31
+        this.payerName = payerName;
32
+    }
33
+
34
+    public int compareTo(Payment o) {
35
+        String origin = this.getShortDescription();
36
+        String compared = o.getShortDescription();
37
+        if (origin.length() == compared.length()) {
38
+            int difference = 0;
39
+            for (int i = 0; i < origin.length(); i++) {
40
+                difference += origin.charAt(i) - compared.charAt(i);
41
+            }
42
+            return difference;
43
+        } else {
44
+            return origin.length() - compared.length();
45
+        }
46
+    }
29 47
 }

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

@@ -1,17 +1,24 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import org.junit.Before;
3 4
 import org.junit.Test;
4 5
 
5 6
 import static org.junit.Assert.*;
6 7
 
7 8
 public class CreditCardTest {
9
+    private CreditCard cc;
10
+    private Paypal pay;
11
+    private Check ch;
12
+
13
+    @Before
14
+    public void setUp() throws Exception {
15
+        ch = new Check();
16
+        pay = new Paypal();
17
+        cc = new CreditCard();
18
+    }
8 19
 
9 20
     @Test
10 21
     public void getID() {
11
-        //creditCard object
12
-        CreditCard cc = new CreditCard();
13
-        //long ID we are testing
14
-
15 22
         //Given
16 23
         long ExpectedID = 1234567891;
17 24
         //When
@@ -23,10 +30,6 @@ public class CreditCardTest {
23 30
 
24 31
     @Test
25 32
     public void getPayerName() {
26
-        //creditCard object
27
-        CreditCard cc = new CreditCard();
28
-
29
-
30 33
         //Given
31 34
         String ExpectedPayerName = "Tia Mowry";
32 35
         //When
@@ -39,10 +42,6 @@ public class CreditCardTest {
39 42
     @Test
40 43
     public void getExpiredMonth(){
41 44
         int expiredMonth = 10;
42
-        //creditCard object
43
-        CreditCard cc = new CreditCard();
44
-
45
-
46 45
         //Given
47 46
         int expectedExpiredMonth = expiredMonth;
48 47
         //When
@@ -53,10 +52,6 @@ public class CreditCardTest {
53 52
     }
54 53
     @Test
55 54
     public void getExpiredYear(){
56
-        //creditCard object
57
-        CreditCard cc = new CreditCard();
58
-
59
-
60 55
         //Given
61 56
         int ExpectedExpireYear = 2019;
62 57
         //When
@@ -67,19 +62,39 @@ public class CreditCardTest {
67 62
     }
68 63
     @Test
69 64
     public void getShortDescription() {
70
-        CreditCard cc = new CreditCard();
71
-
72 65
         String payerName = "Khalil Saboor ";
73 66
         int expiredMonth = 12;
74 67
         int expiredYear = 2020;
75
-        String payerNumber = " 4551 ";
68
+        String payerNumber = "4551 ";
76 69
         cc.setPayerName(payerName);
77 70
         cc.setExpiredMonth(expiredMonth);
78 71
         cc.setExpiredYear(expiredYear);
79 72
         cc.setNumber(payerNumber);
80 73
 
81
-        String expected = "CC " + payerName + " " + payerNumber + "  " + expiredMonth + "/" + expiredYear;
74
+        String expected = "CC " + payerName + " " + payerNumber + " " + expiredMonth + "/" + expiredYear;
82 75
         System.out.println(cc.getShortDescription());
83 76
         String actual = cc.getShortDescription();
77
+        assertEquals(expected,actual);
78
+    }
79
+    @Test
80
+    public void compareTo1(){
81
+
82
+        int expected = 0;
83
+        int actual = cc.compareTo(cc);
84
+        assertEquals(expected,actual);
85
+    }
86
+    @Test
87
+    public void compareTo2(){
88
+
89
+        int expected = -1;
90
+        int actual = cc.compareTo(pay);
91
+        assertEquals(expected,actual);
92
+    }
93
+    @Test
94
+    public void compareTo3(){
95
+
96
+        int expected = -1;
97
+        int actual = cc.compareTo(ch);
98
+        assertEquals(expected,actual);
84 99
     }
85 100
 }

+ 43
- 1
src/test/java/com/zipcoder/payment/PaypalTest.java Просмотреть файл

@@ -5,7 +5,9 @@ import org.junit.Test;
5 5
 import static org.junit.Assert.*;
6 6
 
7 7
 public class PaypalTest {
8
-
8
+    CreditCard cc = new CreditCard();
9
+    Paypal pay = new Paypal();
10
+    Check ch = new Check();
9 11
     @Test
10 12
     public void getID() {
11 13
         Paypal pay = new Paypal();
@@ -22,9 +24,49 @@ public class PaypalTest {
22 24
 
23 25
     @Test
24 26
     public void getPayerName() {
27
+        Paypal pay = new Paypal();
28
+        //Given
29
+        String PayerName = "Tia Mowry";
30
+        //When
31
+        pay.setPayerName(PayerName);
32
+        //Then
33
+        String actualID = pay.getPayerName();
34
+        assertEquals(PayerName,actualID);
25 35
     }
26 36
 
27 37
     @Test
28 38
     public void getShortDescription() {
39
+        Paypal pay = new Paypal();
40
+        String payerName = "Khalil Saboor ";
41
+        String accountNumber = "***4551";
42
+
43
+        pay.setPayerName(payerName);
44
+
45
+        String email = "khalil@gmail.com";
46
+        String expected = "Paypal " + payerName + " " + email;
47
+        System.out.println(pay.getShortDescription());
48
+        String actual = pay.getShortDescription();
49
+        assertEquals(expected,actual);
50
+    }
51
+    @Test
52
+    public void compareTo1(){
53
+
54
+        int expected = 0;
55
+        int actual = pay.compareTo(pay);
56
+        assertEquals(expected,actual);
57
+    }
58
+    @Test
59
+    public void compareTo2(){
60
+
61
+        int expected = -1;
62
+        int actual = pay.compareTo(cc);
63
+        assertEquals(expected,actual);
64
+    }
65
+    @Test
66
+    public void compareTo3(){
67
+
68
+        int expected = -1;
69
+        int actual = ch.compareTo(cc);
70
+        assertEquals(expected,actual);
29 71
     }
30 72
 }