Khalil Malik Saboor 8 lat temu
rodzic
commit
9fa0f1bef7

+ 9
- 14
src/main/java/com/zipcoder/payment/Check.java Wyświetl plik

@@ -25,20 +25,6 @@ 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
-
42 28
     public String getRoutingNumber() {
43 29
         return routingNumber;
44 30
     }
@@ -57,4 +43,13 @@ public class Check implements Payment {
57 43
 
58 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 Wyświetl plik

@@ -34,34 +34,18 @@ public class CreditCard implements Payment {
34 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 39
     public int getExpiredMonth() {
56 40
         return expiredMonth;
57 41
     }
58 42
 
59 43
     public void setExpiredMonth(int expiredMonth) {
60
-    this.expiredMonth = expiredMonth;
44
+        this.expiredMonth = expiredMonth;
61 45
     }
62 46
 
63 47
     public void setExpiredYear(int expiredYear) {
64
-    this.expiredYear = expiredYear;
48
+        this.expiredYear = expiredYear;
65 49
     }
66 50
 
67 51
     public int getExpiredYear() {
@@ -69,8 +53,15 @@ public class CreditCard implements Payment {
69 53
     }
70 54
 
71 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 Wyświetl plik

@@ -31,17 +31,17 @@ public class Paypal implements Payment {
31 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 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 Wyświetl plik

@@ -1,11 +1,21 @@
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 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 19
     @Test
10 20
     public void getID() {
11 21
         //creditCard object
@@ -74,4 +84,25 @@ public class CheckTest {
74 84
         String actual = check.getShortDescription();
75 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 Wyświetl plik

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

+ 16
- 11
src/test/java/com/zipcoder/payment/PaypalTest.java Wyświetl plik

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