Khalil Malik Saboor 8 lat temu
rodzic
commit
9f56129420

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

7
     private String accountNumber;
7
     private String accountNumber;
8
 
8
 
9
     Check(){
9
     Check(){
10
-        ID = 123;
10
+        ID = 1234567891;
11
         payerName =  "Tia Mowry";
11
         payerName =  "Tia Mowry";
12
         routingNumber = "34343456";
12
         routingNumber = "34343456";
13
         accountNumber = "***4551";
13
         accountNumber = "***4551";
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
+
28
     public String getRoutingNumber() {
42
     public String getRoutingNumber() {
29
         return routingNumber;
43
         return routingNumber;
30
     }
44
     }

+ 20
- 2
src/main/java/com/zipcoder/payment/CreditCard.java Wyświetl plik

3
 public class CreditCard implements Payment {
3
 public class CreditCard implements Payment {
4
     //Define
4
     //Define
5
     private String payerNumber;
5
     private String payerNumber;
6
+    private String payerName;
6
     private String number;
7
     private String number;
7
-    private long ID;
8
     private int expiredMonth;
8
     private int expiredMonth;
9
     private int expiredYear;
9
     private int expiredYear;
10
-    private String payerName;
10
+    private long ID;
11
+
12
+
11
 
13
 
12
     public CreditCard(){
14
     public CreditCard(){
13
         ID = 1234567891;
15
         ID = 1234567891;
36
         return "CC " + payerName + " " + payerNumber + " " + expiredMonth + "/" + expiredYear;
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
     public int getExpiredMonth() {
55
     public int getExpiredMonth() {
40
         return expiredMonth;
56
         return expiredMonth;
41
     }
57
     }
55
     public void setNumber(String number) {
71
     public void setNumber(String number) {
56
         this.number = this.number;
72
         this.number = this.number;
57
     }
73
     }
74
+
75
+
58
 }
76
 }

+ 2
- 2
src/main/java/com/zipcoder/payment/Payment.java Wyświetl plik

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

+ 6
- 0
src/main/java/com/zipcoder/payment/PaymentsSortByPayer.java Wyświetl plik

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 Wyświetl plik

6
     private String email;
6
     private String email;
7
 
7
 
8
     Paypal (){
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
     public Long getID() {
14
     public Long getID() {
20
     }
20
     }
21
 
21
 
22
     public String getShortDescription() {
22
     public String getShortDescription() {
23
-        return "Paypal" + payerName +" "+ email;
23
+        return "Paypal " + payerName +" "+ email;
24
     }
24
     }
25
 
25
 
26
     public void setID(long ID) {
26
     public void setID(long ID) {
27
         this.ID = ID;
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 Wyświetl plik

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 CreditCardTest {
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
     @Test
20
     @Test
10
     public void getID() {
21
     public void getID() {
11
-        //creditCard object
12
-        CreditCard cc = new CreditCard();
13
-        //long ID we are testing
14
-
15
         //Given
22
         //Given
16
         long ExpectedID = 1234567891;
23
         long ExpectedID = 1234567891;
17
         //When
24
         //When
23
 
30
 
24
     @Test
31
     @Test
25
     public void getPayerName() {
32
     public void getPayerName() {
26
-        //creditCard object
27
-        CreditCard cc = new CreditCard();
28
-
29
-
30
         //Given
33
         //Given
31
         String ExpectedPayerName = "Tia Mowry";
34
         String ExpectedPayerName = "Tia Mowry";
32
         //When
35
         //When
39
     @Test
42
     @Test
40
     public void getExpiredMonth(){
43
     public void getExpiredMonth(){
41
         int expiredMonth = 10;
44
         int expiredMonth = 10;
42
-        //creditCard object
43
-        CreditCard cc = new CreditCard();
44
-
45
-
46
         //Given
45
         //Given
47
         int expectedExpiredMonth = expiredMonth;
46
         int expectedExpiredMonth = expiredMonth;
48
         //When
47
         //When
53
     }
52
     }
54
     @Test
53
     @Test
55
     public void getExpiredYear(){
54
     public void getExpiredYear(){
56
-        //creditCard object
57
-        CreditCard cc = new CreditCard();
58
-
59
-
60
         //Given
55
         //Given
61
         int ExpectedExpireYear = 2019;
56
         int ExpectedExpireYear = 2019;
62
         //When
57
         //When
67
     }
62
     }
68
     @Test
63
     @Test
69
     public void getShortDescription() {
64
     public void getShortDescription() {
70
-        CreditCard cc = new CreditCard();
71
-
72
         String payerName = "Khalil Saboor ";
65
         String payerName = "Khalil Saboor ";
73
         int expiredMonth = 12;
66
         int expiredMonth = 12;
74
         int expiredYear = 2020;
67
         int expiredYear = 2020;
75
-        String payerNumber = " 4551 ";
68
+        String payerNumber = "4551 ";
76
         cc.setPayerName(payerName);
69
         cc.setPayerName(payerName);
77
         cc.setExpiredMonth(expiredMonth);
70
         cc.setExpiredMonth(expiredMonth);
78
         cc.setExpiredYear(expiredYear);
71
         cc.setExpiredYear(expiredYear);
79
         cc.setNumber(payerNumber);
72
         cc.setNumber(payerNumber);
80
 
73
 
81
-        String expected = "CC " + payerName + " " + payerNumber + "  " + expiredMonth + "/" + expiredYear;
74
+        String expected = "CC " + payerName + " " + payerNumber + " " + expiredMonth + "/" + expiredYear;
82
         System.out.println(cc.getShortDescription());
75
         System.out.println(cc.getShortDescription());
83
         String actual = cc.getShortDescription();
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 Wyświetl plik

5
 import static org.junit.Assert.*;
5
 import static org.junit.Assert.*;
6
 
6
 
7
 public class PaypalTest {
7
 public class PaypalTest {
8
-
8
+    CreditCard cc = new CreditCard();
9
+    Paypal pay = new Paypal();
10
+    Check ch = new Check();
9
     @Test
11
     @Test
10
     public void getID() {
12
     public void getID() {
11
         Paypal pay = new Paypal();
13
         Paypal pay = new Paypal();
22
 
24
 
23
     @Test
25
     @Test
24
     public void getPayerName() {
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
     @Test
37
     @Test
28
     public void getShortDescription() {
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
 }