Khalil Malik Saboor hace 8 años
padre
commit
9fa0f1bef7

+ 9
- 14
src/main/java/com/zipcoder/payment/Check.java Ver fichero

25
         return "Check " + payerName + " " + accountNumber;
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
-
42
     public String getRoutingNumber() {
28
     public String getRoutingNumber() {
43
         return routingNumber;
29
         return routingNumber;
44
     }
30
     }
57
 
43
 
58
     public void setRoutingNumber(String routingNumber) { this.routingNumber = routingNumber; }
44
     public void setRoutingNumber(String routingNumber) { this.routingNumber = routingNumber; }
59
 
45
 
46
+    public int compareTo(Payment o) {
47
+        if (this.getShortDescription().compareTo(o.getShortDescription()) < 0) return -1;
48
+
49
+        else if (this.getShortDescription().compareTo(o.getShortDescription()) > 0) return 1;
50
+
51
+        else return 0;
52
+
53
+    }
54
+
60
 }
55
 }

+ 11
- 20
src/main/java/com/zipcoder/payment/CreditCard.java Ver fichero

34
     this.payerName = payerName;
34
     this.payerName = payerName;
35
     }
35
     }
36
 
36
 
37
-    public String getShortDescription() {
38
-        return "CC " + payerName + " " + payerNumber + " " + expiredMonth + "/" + expiredYear;
39
-    }
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
-    }
37
+    public String getShortDescription() { return "CC " + payerName + " " + payerNumber + " " + expiredMonth + "/" + expiredYear; }
54
 
38
 
55
     public int getExpiredMonth() {
39
     public int getExpiredMonth() {
56
         return expiredMonth;
40
         return expiredMonth;
57
     }
41
     }
58
 
42
 
59
     public void setExpiredMonth(int expiredMonth) {
43
     public void setExpiredMonth(int expiredMonth) {
60
-    this.expiredMonth = expiredMonth;
44
+        this.expiredMonth = expiredMonth;
61
     }
45
     }
62
 
46
 
63
     public void setExpiredYear(int expiredYear) {
47
     public void setExpiredYear(int expiredYear) {
64
-    this.expiredYear = expiredYear;
48
+        this.expiredYear = expiredYear;
65
     }
49
     }
66
 
50
 
67
     public int getExpiredYear() {
51
     public int getExpiredYear() {
69
     }
53
     }
70
 
54
 
71
     public void setNumber(String number) {
55
     public void setNumber(String number) {
72
-        this.number = this.number;
56
+        this.number = number;
73
     }
57
     }
74
 
58
 
59
+    public int compareTo(Payment o) {
60
+        if (this.getShortDescription().compareTo(o.getShortDescription()) < 0) return -1;
61
+
62
+        else if (this.getShortDescription().compareTo(o.getShortDescription()) > 0) return 1;
75
 
63
 
64
+        else return 0;
65
+
66
+    }
76
 }
67
 }

+ 11
- 11
src/main/java/com/zipcoder/payment/Paypal.java Ver fichero

31
         this.payerName = payerName;
31
         this.payerName = payerName;
32
     }
32
     }
33
 
33
 
34
+    public void setEmail(String email) { this.email = email; }
35
+
36
+    public String getEmail() { return email; }
37
+
34
     public int compareTo(Payment o) {
38
     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
-        }
39
+
40
+        if (this.getShortDescription().compareTo(o.getShortDescription()) < 0) return -1;
41
+
42
+        else if (this.getShortDescription().compareTo(o.getShortDescription()) > 0) return 1;
43
+
44
+        else return 0;
45
+
46
     }
46
     }
47
 }
47
 }

+ 32
- 1
src/test/java/com/zipcoder/payment/CheckTest.java Ver fichero

1
 package com.zipcoder.payment;
1
 package com.zipcoder.payment;
2
 
2
 
3
+import org.junit.Before;
3
 import org.junit.Test;
4
 import org.junit.Test;
4
 
5
 
5
 import static org.junit.Assert.*;
6
 import static org.junit.Assert.*;
6
 
7
 
7
 public class CheckTest {
8
 public class CheckTest {
8
-
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
+    }
9
     @Test
19
     @Test
10
     public void getID() {
20
     public void getID() {
11
         //creditCard object
21
         //creditCard object
74
         String actual = check.getShortDescription();
84
         String actual = check.getShortDescription();
75
         assertEquals(expected,actual);
85
         assertEquals(expected,actual);
76
     }
86
     }
87
+    @Test
88
+    public void compareTo1(){
89
+
90
+        int expected = 0;
91
+        int actual = cc.compareTo(cc);
92
+        assertEquals(expected,actual);
93
+    }
94
+    @Test
95
+    public void compareTo2(){
96
+
97
+        int expected = -1;
98
+        int actual = cc.compareTo(ch);
99
+        assertEquals(expected,actual);
100
+    }
101
+    @Test
102
+    public void compareTo3(){
103
+
104
+        int expected = -1;
105
+        int actual = cc.compareTo(pay);
106
+        assertEquals(expected,actual);
107
+    }
77
 }
108
 }

+ 2
- 2
src/test/java/com/zipcoder/payment/CreditCardTest.java Ver fichero

87
     public void compareTo2(){
87
     public void compareTo2(){
88
 
88
 
89
         int expected = -1;
89
         int expected = -1;
90
-        int actual = cc.compareTo(pay);
90
+        int actual = cc.compareTo(ch);
91
         assertEquals(expected,actual);
91
         assertEquals(expected,actual);
92
     }
92
     }
93
     @Test
93
     @Test
94
     public void compareTo3(){
94
     public void compareTo3(){
95
 
95
 
96
         int expected = -1;
96
         int expected = -1;
97
-        int actual = cc.compareTo(ch);
97
+        int actual = cc.compareTo(pay);
98
         assertEquals(expected,actual);
98
         assertEquals(expected,actual);
99
     }
99
     }
100
 }
100
 }

+ 16
- 11
src/test/java/com/zipcoder/payment/PaypalTest.java Ver fichero

5
 import static org.junit.Assert.*;
5
 import static org.junit.Assert.*;
6
 
6
 
7
 public class PaypalTest {
7
 public class PaypalTest {
8
-    CreditCard cc = new CreditCard();
9
-    Paypal pay = new Paypal();
10
-    Check ch = new Check();
8
+    private CreditCard cc = new CreditCard();
9
+    private Paypal pay = new Paypal();
10
+    private Check ch = new Check();
11
     @Test
11
     @Test
12
     public void getID() {
12
     public void getID() {
13
-        Paypal pay = new Paypal();
14
-        //long ID we are testing
15
-
16
         //Given
13
         //Given
17
         long ID = 1234567891;
14
         long ID = 1234567891;
18
         //When
15
         //When
24
 
21
 
25
     @Test
22
     @Test
26
     public void getPayerName() {
23
     public void getPayerName() {
27
-        Paypal pay = new Paypal();
28
         //Given
24
         //Given
29
         String PayerName = "Tia Mowry";
25
         String PayerName = "Tia Mowry";
30
         //When
26
         //When
33
         String actualID = pay.getPayerName();
29
         String actualID = pay.getPayerName();
34
         assertEquals(PayerName,actualID);
30
         assertEquals(PayerName,actualID);
35
     }
31
     }
32
+    @Test
33
+    public void testEmail(){
34
+        String email = "tia@mowry.com";
35
+
36
+        pay.setEmail(email);
37
+
38
+        String actualEmail = pay.getEmail();
39
+        assertEquals(email,actualEmail);
40
+    }
36
 
41
 
37
     @Test
42
     @Test
38
     public void getShortDescription() {
43
     public void getShortDescription() {
39
-        Paypal pay = new Paypal();
44
+
40
         String payerName = "Khalil Saboor ";
45
         String payerName = "Khalil Saboor ";
41
-        String accountNumber = "***4551";
46
+        String email = "khalil@gmail.com";
42
 
47
 
48
+        pay.setEmail(email);
43
         pay.setPayerName(payerName);
49
         pay.setPayerName(payerName);
44
 
50
 
45
-        String email = "khalil@gmail.com";
46
         String expected = "Paypal " + payerName + " " + email;
51
         String expected = "Paypal " + payerName + " " + email;
47
         System.out.println(pay.getShortDescription());
52
         System.out.println(pay.getShortDescription());
48
         String actual = pay.getShortDescription();
53
         String actual = pay.getShortDescription();
66
     public void compareTo3(){
71
     public void compareTo3(){
67
 
72
 
68
         int expected = -1;
73
         int expected = -1;
69
-        int actual = ch.compareTo(cc);
74
+        int actual = pay.compareTo(ch);
70
         assertEquals(expected,actual);
75
         assertEquals(expected,actual);
71
     }
76
     }
72
 }
77
 }