BlackJack 8 anni fa
parent
commit
626b3f74e3

+ 5
- 2
README.MD Vedi File

@@ -65,10 +65,13 @@ Note: You may use the String/Long compareTo methods in your code
65 65
   - This indicate that payment can be comparable, which means it can be sorted
66 66
 2. Fix the syntax error in your CreditCard, Check, and Paypal class by adding the compare method
67 67
 3. Create a test for the `compareTo` method. Compare using the getShortDescription()
68
-  - Example: Given Payment of type Paypal with the short description of `Paypal Tia Mowry tia@mowry.com` and a Check with the short description of `Check Tia Mowry ***4551`, then the `compareTo` method should return a number larger than 0
68
+  - Example: Given Payment of type Paypal with the short description of `Paypal Tia Mowry tia@mowry.com` and a Check 
69
+with the short description of `Check Tia Mowry ***4551`, then the `compareTo` method should return a number larger than 0
70
+
69 71
   - The `compareTo` method will return number larger than 0 if this payment comes after payment2
70 72
   - The `compareTo` method will return number 0 if this payment has the same information
71 73
   - The `compareTo` method will return number less than 0 if this payment comes before payment2
74
+  
72 75
 4. Add the code necessary to make the test pass
73 76
 5. Create a new `PaymentSortByPayer` class which implements `java.util.Comparator`
74 77
   - This class will sort by payer name
@@ -134,7 +137,7 @@ Note: You may use the String/Long compareTo methods in your code
134 137
 
135 138
     ```
136 139
       Payment[] payments = new Payment[2];
137
-      Payment paypal = new PayPayl(120L, "Tamara Mowry", "tamara@mowry.com");
140
+      Payment paypal = new PayPal(120L, "Tamara Mowry", "tamara@mowry.com");
138 141
       Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551")
139 142
 
140 143
       payment[0] = paypal;

+ 26
- 0
pom.xml Vedi File

@@ -7,6 +7,32 @@
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>
22
+    <dependencies>
23
+        <dependency>
24
+            <groupId>junit</groupId>
25
+            <artifactId>junit</artifactId>
26
+            <version>RELEASE</version>
27
+            <scope>test</scope>
28
+        </dependency>
29
+        <dependency>
30
+            <groupId>org.junit.jupiter</groupId>
31
+            <artifactId>junit-jupiter-api</artifactId>
32
+            <version>RELEASE</version>
33
+            <scope>test</scope>
34
+        </dependency>
35
+    </dependencies>
10 36
 
11 37
 
12 38
 </project>

BIN
production/Labs/Check.class Vedi File


BIN
production/Labs/CreditCard.class Vedi File


BIN
production/Labs/Payment.class Vedi File


BIN
production/Labs/Paypal.class Vedi File


BIN
production/Labs/com/zipcoder/payment/PaymentPresenter.class Vedi File


+ 73
- 0
src/main/java/Check.java Vedi File

@@ -0,0 +1,73 @@
1
+public class Check implements Payment {
2
+    private String payerName;
3
+   private String routingNumber;
4
+  private String accountNumber;
5
+    private long id;
6
+
7
+    public Check(){
8
+
9
+    }
10
+
11
+    public Check( long id, String payerName, String routingNumber, String accountNumber){
12
+this.id = id;
13
+this.payerName = payerName;
14
+this.accountNumber = getLastFourAccountNumber(accountNumber);
15
+this.routingNumber = routingNumber;
16
+
17
+
18
+    }
19
+
20
+   public void setId(long id){
21
+
22
+       this.id = id;
23
+   }
24
+    public long getId() {
25
+        return id;
26
+    }
27
+
28
+    public String getShortDescription() {
29
+        return "Check " + getPayerName()  + " " + "***" + getAccountNumber();
30
+    }
31
+
32
+
33
+    public String getLastFourAccountNumber(String accountNumber){
34
+
35
+            return accountNumber.replace(accountNumber.substring(0, accountNumber.length()-4), "");
36
+
37
+        }
38
+
39
+
40
+    public void setPayerName(String payerName) {
41
+       this.payerName = payerName;
42
+    }
43
+
44
+    public String getPayerName() {
45
+        return payerName;
46
+    }
47
+
48
+    public void setRoutingNumber(String routingNumber) {
49
+        this.routingNumber = routingNumber;
50
+    }
51
+
52
+    public String getRoutingNumber() {
53
+       return routingNumber;
54
+    }
55
+
56
+    public void setAccountNumber(String accountNumber){
57
+       this.accountNumber = accountNumber;
58
+    }
59
+
60
+    public String getAccountNumber(){
61
+       return accountNumber;
62
+    }
63
+
64
+    public int compareTo(Payment o) {
65
+
66
+
67
+     if (this.getShortDescription().compareTo(o.getShortDescription()) > 0) return 1;
68
+
69
+     else  if (this.getShortDescription().compareTo(o.getShortDescription()) < 0)return -1;
70
+
71
+        else return 0;
72
+    }
73
+}

+ 84
- 0
src/main/java/CreditCard.java Vedi File

@@ -0,0 +1,84 @@
1
+
2
+public class CreditCard implements Payment {
3
+
4
+private long id;
5
+private String payerName;
6
+private String number;
7
+private int expiredMonth;
8
+private int expiredYear;
9
+
10
+public CreditCard(){
11
+
12
+
13
+}
14
+
15
+public CreditCard( long id, String payerName, String number,  int expiredMonth,  int expiredYear){
16
+
17
+//id = 0;
18
+//payerName = "";
19
+//number = "";
20
+//expiredMonth = 0;
21
+//expiredYear = 0;
22
+
23
+    }
24
+
25
+
26
+
27
+    public void setId(long id){
28
+        this.id = id;
29
+    }
30
+
31
+    public void setPayerName(String payerName){
32
+        this.payerName = payerName;
33
+    }
34
+
35
+    public void setNumber(String number){
36
+        this.number = number;
37
+    }
38
+
39
+    public void setExpiredMonth(int expiredMonth){
40
+        this.expiredMonth = expiredMonth;
41
+    }
42
+
43
+    public void setExpiredYear(int expiredYear){
44
+        this.expiredYear = expiredYear;
45
+    }
46
+
47
+    public long getId() {
48
+        return id;
49
+    }
50
+
51
+    public String getPayerName() {
52
+        return payerName;
53
+    }
54
+
55
+    public String getShortDescription() {
56
+        return "CC " + payerName + " "+ number+" " + expiredMonth +
57
+                "/" + expiredYear;
58
+    }
59
+
60
+
61
+    public int getExpiredMonth() {
62
+        return expiredMonth;
63
+    }
64
+
65
+    public int getExpiredYear() {
66
+        return expiredYear;
67
+    }
68
+
69
+    public String getNumber() {
70
+        return number;
71
+    }
72
+
73
+
74
+    public int compareTo(Payment o) {
75
+
76
+        if (this.getShortDescription().compareTo(o.getShortDescription()) < 0) return -1;
77
+
78
+        else if (this.getShortDescription().compareTo(o.getShortDescription()) > 0) return 1;
79
+
80
+        else return 0;
81
+
82
+    }
83
+}
84
+

+ 9
- 0
src/main/java/Payment.java Vedi File

@@ -0,0 +1,9 @@
1
+public interface Payment extends Comparable<Payment>{
2
+
3
+    public long getId();
4
+
5
+    public String getPayerName();
6
+
7
+    public String getShortDescription();
8
+
9
+}

+ 45
- 0
src/main/java/PaymentPresenter.java Vedi File

@@ -0,0 +1,45 @@
1
+import java.util.*;
2
+
3
+public class PaymentPresenter {
4
+
5
+
6
+    public String toString(Payment[] payments) {
7
+        Arrays.sort(payments);
8
+
9
+        StringBuilder string = new StringBuilder();
10
+        for (Payment o : payments) {
11
+
12
+            string.append(o.getShortDescription() + "\n");
13
+        }
14
+
15
+
16
+        return string.toString();
17
+
18
+
19
+    }
20
+
21
+    public String toStringByPayerName(Payment[] payments) {
22
+        Arrays.sort(payments, new paymentSortByPayer());
23
+
24
+        StringBuilder string = new StringBuilder();
25
+        for (Payment o : payments) {
26
+
27
+            o.getPayerName();
28
+
29
+            string.append(o.getShortDescription() + "\n");
30
+        }
31
+        return string.toString();
32
+    }
33
+
34
+    public String toStringById(Payment[] payments) {
35
+        Arrays.sort(payments, (Payment a, Payment b) -> Long.compare(a.getId(), b.getId()));
36
+
37
+        StringBuilder string = new StringBuilder();
38
+        for (Payment o : payments) {
39
+
40
+
41
+            string.append(o.getShortDescription() + "\n");
42
+        }
43
+        return string.toString();
44
+    }
45
+}

+ 55
- 0
src/main/java/Paypal.java Vedi File

@@ -0,0 +1,55 @@
1
+public class Paypal implements Payment {
2
+    private long id;
3
+    private String payerName;
4
+    private String email;
5
+
6
+    public Paypal(){
7
+
8
+    }
9
+
10
+    public Paypal(long id, String payerName,  String email){
11
+this.id = id;
12
+this.payerName = payerName;
13
+this.email= email;
14
+    }
15
+
16
+    public long getId() {
17
+        return id;
18
+    }
19
+
20
+    public String getPayerName() {
21
+        return payerName;
22
+    }
23
+
24
+    public String getShortDescription() {
25
+        return "Paypal " + getPayerName() + " " + getEmail();
26
+    }
27
+
28
+    public void setId(long id) {
29
+        this.id = id;
30
+    }
31
+
32
+    public void setPayerName(String payerName) {
33
+        this.payerName = payerName;
34
+    }
35
+
36
+    public String getEmail() {
37
+        return email;
38
+    }
39
+
40
+    public void setEmail(String email) {
41
+        this.email = email;
42
+    }
43
+
44
+
45
+    public int compareTo(Payment o) {
46
+
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
+    }
55
+}

+ 0
- 6
src/main/java/com/zipcoder/payment/PaymentPresenter.java Vedi File

@@ -1,6 +0,0 @@
1
-package com.zipcoder.payment;
2
-
3
-public class PaymentPresenter {
4
-
5
-
6
-}

+ 13
- 0
src/main/java/paymentSortByPayer.java Vedi File

@@ -0,0 +1,13 @@
1
+import java.util.Comparator;
2
+
3
+public class paymentSortByPayer implements Comparator<Payment> {
4
+
5
+    public paymentSortByPayer(){
6
+
7
+    }
8
+
9
+    @Override
10
+    public int compare(Payment o1, Payment o2) {
11
+        return o1.getPayerName().compareTo(o2.getPayerName());
12
+    }
13
+}

+ 107
- 0
src/test/java/CheckTest.java Vedi File

@@ -0,0 +1,107 @@
1
+import org.junit.Test;
2
+
3
+import static org.junit.Assert.*;
4
+
5
+public class CheckTest {
6
+    Check check = new Check();
7
+    Paypal paypal = new Paypal();
8
+    CreditCard credit = new CreditCard();
9
+
10
+    @Test
11
+    public void getId() {
12
+
13
+
14
+        long expectedId= 35;
15
+        check.setId(expectedId);
16
+
17
+        long actualId = check.getId();
18
+
19
+        assertEquals(expectedId, actualId);
20
+
21
+    }
22
+
23
+    @Test
24
+    public void getPayerName() {
25
+
26
+
27
+        String expectedPayerName= "Tia Mowry";
28
+        check.setPayerName(expectedPayerName);
29
+
30
+        String actualPayerName = check.getPayerName();
31
+
32
+        assertEquals(expectedPayerName, actualPayerName);
33
+    }
34
+
35
+    @Test
36
+    public void getRoutingNumber() {
37
+
38
+
39
+        String expectedRoutingNumber = "023456890";
40
+        check.setRoutingNumber(expectedRoutingNumber);
41
+
42
+        String actualRoutingNumber = check.getRoutingNumber();
43
+
44
+        assertEquals(expectedRoutingNumber, actualRoutingNumber);
45
+    }
46
+
47
+
48
+
49
+    @Test
50
+    public void getShortDescription() {
51
+
52
+
53
+        String name = "Tia Mowry";
54
+        String number = "4552";
55
+
56
+
57
+        check.setPayerName(name);
58
+
59
+        check.setAccountNumber(number);
60
+        String expected = "Check " + name  + " " + "***" + number;
61
+
62
+        System.out.println(check.getShortDescription());
63
+        String actual = check.getShortDescription();
64
+
65
+        assertEquals(expected, actual);
66
+    }
67
+
68
+    @Test
69
+    public void lastFour(){
70
+        String x = "1234567";
71
+
72
+        String expected = "4567";
73
+
74
+        String actual = check.getLastFourAccountNumber(x);
75
+
76
+        assertEquals(expected, actual);
77
+
78
+    }
79
+
80
+    @Test
81
+    public void compareTo() {
82
+        int expected = 0;
83
+
84
+        int actual = check.compareTo(check) ;
85
+
86
+        assertEquals(expected,actual);
87
+    }
88
+
89
+    @Test
90
+    public void compareTo1() {
91
+        int expected = 1;
92
+
93
+        int actual = check.compareTo(credit) ;
94
+
95
+        assertEquals(expected,actual);
96
+    }
97
+
98
+    @Test
99
+    public void compareTo2() {
100
+        int expected = -1;
101
+
102
+        int actual = check.compareTo(paypal) ;
103
+
104
+        assertEquals(expected,actual);
105
+    }
106
+
107
+}

+ 134
- 0
src/test/java/CreditCardTest.java Vedi File

@@ -0,0 +1,134 @@
1
+import org.junit.Test;
2
+
3
+import static org.junit.Assert.*;
4
+
5
+public class CreditCardTest {
6
+
7
+    CreditCard credit = new CreditCard();
8
+    Payment paypal = new Paypal();
9
+    Check check = new Check();
10
+
11
+    @org.junit.Test
12
+    public void getId() {
13
+
14
+        long expectedId = 35;
15
+        credit.setId(expectedId);
16
+
17
+        long actual = credit.getId();
18
+
19
+        assertEquals(expectedId, actual);
20
+    }
21
+
22
+    @org.junit.Test
23
+    public void getPayerName() {
24
+
25
+        String expectedPayerName = "Tia Mowry";
26
+        credit.setPayerName(expectedPayerName);
27
+
28
+        String actual = credit.getPayerName();
29
+
30
+        assertEquals(expectedPayerName, actual);
31
+    }
32
+
33
+    @org.junit.Test
34
+    public void getNumber() {
35
+
36
+        String expectedNumber = "4551";
37
+        credit.setNumber(expectedNumber);
38
+
39
+        String actual = credit.getNumber();
40
+
41
+        assertEquals(expectedNumber, actual);
42
+    }
43
+
44
+
45
+    @org.junit.Test
46
+    public void getShortDescription() {
47
+
48
+        String name = "Tia Mowry";
49
+        String email = "tiamowry&yahoo.com";
50
+        String number = "4552";
51
+        int expiredMonth = 9;
52
+        int expiredYear = 2019;
53
+
54
+        credit.setPayerName(name);
55
+    credit.setExpiredYear(expiredYear);
56
+    credit.setExpiredMonth(expiredMonth);
57
+    credit.setNumber(number);
58
+        String expected = "CC " + name + " " + number + " " + expiredMonth +
59
+                "/" + expiredYear;
60
+
61
+        System.out.println(credit.getShortDescription());
62
+        String actual = credit.getShortDescription();
63
+
64
+        assertEquals(expected, actual);
65
+    }
66
+
67
+    @org.junit.Test
68
+    public void getExpiredMonth(){
69
+
70
+        int expectedExpiredMonth = 10;
71
+        credit.setExpiredMonth(expectedExpiredMonth);
72
+
73
+        int actual = credit.getExpiredMonth();
74
+
75
+        assertEquals(expectedExpiredMonth, actual);
76
+
77
+    }
78
+
79
+    @org.junit.Test
80
+    public void getExpiredYear(){
81
+
82
+        int expectedExpiredYear = 2019;
83
+        credit.setExpiredYear(expectedExpiredYear);
84
+
85
+        int actual = credit.getExpiredYear();
86
+
87
+        assertEquals(expectedExpiredYear, actual);
88
+
89
+    }
90
+
91
+    @Test
92
+    public void compareToItself() {
93
+        int expected = 0;
94
+        credit.setPayerName("A");
95
+
96
+        int actual = credit.compareTo(credit) ;
97
+
98
+        assertEquals(expected,actual);
99
+    }
100
+
101
+    @Test
102
+    public void compareToAnotherCreditCard() {
103
+        int expected = 0;
104
+        credit.setPayerName("A");
105
+        credit.setNumber("1235454");
106
+
107
+        CreditCard bCreditCard = new CreditCard();
108
+        bCreditCard.setPayerName("A");
109
+        bCreditCard.setNumber("1235454");
110
+
111
+        int actual =  bCreditCard.compareTo(credit) ;
112
+
113
+        assertEquals(expected,actual);
114
+    }
115
+
116
+    @Test
117
+    public void compareTo1() {
118
+        int expected = -1;
119
+
120
+        int actual = credit.compareTo(check) ;
121
+
122
+        assertEquals(expected,actual);
123
+    }
124
+
125
+    @Test
126
+    public void compareTo2() {
127
+
128
+        int expected = -1;
129
+
130
+        int actual = credit.compareTo(paypal) ;
131
+
132
+        assertEquals(expected,actual);
133
+    }
134
+}

+ 72
- 0
src/test/java/PaymentPresenterTest.java Vedi File

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

+ 90
- 0
src/test/java/PaypalTest.java Vedi File

@@ -0,0 +1,90 @@
1
+import org.junit.Test;
2
+
3
+import static org.junit.Assert.*;
4
+
5
+public class PaypalTest {
6
+    Paypal paypal = new Paypal();
7
+    Check check = new Check();
8
+    CreditCard credit = new CreditCard();
9
+
10
+    @Test
11
+    public void getId() {
12
+
13
+        long expectedId = 2;
14
+        paypal.setId(expectedId);
15
+
16
+        long actualId = paypal.getId();
17
+        assertEquals(expectedId,actualId);
18
+    }
19
+
20
+    @Test
21
+    public void getPayerName() {
22
+
23
+        String expectedPayerName = "Tia Mowry";
24
+        paypal.setPayerName(expectedPayerName);
25
+
26
+        String actualPayerName = paypal.getPayerName();
27
+    assertEquals(expectedPayerName,actualPayerName);
28
+
29
+    }
30
+
31
+    @Test
32
+    public void getEmail() {
33
+
34
+        String expectedEmail = "tiaM@yahoo.com";
35
+        paypal.setEmail(expectedEmail);
36
+
37
+        String actualEmail = paypal.getEmail();
38
+        assertEquals(expectedEmail,actualEmail);
39
+    }
40
+
41
+    @Test
42
+    public void getShortDescription() {
43
+
44
+        String name = "Tia Mowry";
45
+        String email = "tiamowry@yahoo.com";
46
+
47
+        paypal.setEmail(email);
48
+        paypal.setPayerName(name);
49
+
50
+        String expected = "Paypal " + name + " " + email;
51
+
52
+        System.out.println(paypal.getShortDescription());
53
+        String actual = paypal.getShortDescription();
54
+
55
+        assertEquals(expected, actual);
56
+
57
+
58
+    }
59
+
60
+    @Test
61
+    public void compareTo() {
62
+
63
+       int expected = 1;
64
+
65
+       int actual = paypal.compareTo(check) ;
66
+
67
+        assertEquals(expected,actual);
68
+    }
69
+
70
+    @Test
71
+    public void compareTo1() {
72
+
73
+        int expected = 1;
74
+
75
+        int actual = paypal.compareTo(credit) ;
76
+
77
+
78
+        assertEquals(expected,actual);
79
+    }
80
+
81
+    @Test
82
+    public void compareTo2() {
83
+
84
+        int expected = 0;
85
+
86
+        int actual = paypal.compareTo(paypal) ;
87
+
88
+        assertEquals(expected,actual);
89
+    }
90
+}

+ 0
- 4
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Vedi File

@@ -1,4 +0,0 @@
1
-package com.zipcoder.payment;
2
-
3
-public class PaymentPresenterTest {
4
-}

BIN
test/Labs/CheckTest.class Vedi File


BIN
test/Labs/CreditCardTest.class Vedi File


BIN
test/Labs/PaypalTest.class Vedi File


BIN
test/Labs/com/zipcoder/payment/PaymentPresenterTest.class Vedi File


BIN
test/Labs/pass.class Vedi File