Nicholas Maidanos 8 лет назад
Родитель
Сommit
5e7048682a

+ 22
- 22
README.MD Просмотреть файл

1
-# Polymorphism Payment Lab
1
+# Polymorphism com.zipcoder.payment.Payment Lab
2
 
2
 
3
 ## Objectives
3
 ## Objectives
4
 
4
 
7
 - To learn [lambda function](https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html)
7
 - To learn [lambda function](https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html)
8
 
8
 
9
 # Section 1 - Setting up the classes
9
 # Section 1 - Setting up the classes
10
-## Part 1 - Payment
11
-1. Create a `Payment` interface
10
+## Part 1 - com.zipcoder.payment.Payment
11
+1. Create a `com.zipcoder.payment.Payment` interface
12
   - The class should define a `getId()` method which returns a Long
12
   - The class should define a `getId()` method which returns a Long
13
   - The class should define a `getPayerName()` method which returns a String
13
   - The class should define a `getPayerName()` method which returns a String
14
   - The class should define a `getShortDescription` method which returns a String
14
   - The class should define a `getShortDescription` method which returns a String
15
 
15
 
16
 ## Part 2 - Credit Card
16
 ## Part 2 - Credit Card
17
-1. Create a `CreditCardTest` test class
18
-2. Create a `CreditCard` class which implements the `Payment` interface
17
+1. Create a `com.zipcoder.payment.CreditCardTest` test class
18
+2. Create a `com.zipcoder.payment.CreditCard` class which implements the `com.zipcoder.payment.Payment` interface
19
 3. Add the required methods from the interface
19
 3. Add the required methods from the interface
20
 4. Add getter and setter test for an id
20
 4. Add getter and setter test for an id
21
   - Add a Long id field
21
   - Add a Long id field
31
 
31
 
32
 ## Part 3 - Check
32
 ## Part 3 - Check
33
 1. Create a `CheckTest` test class
33
 1. Create a `CheckTest` test class
34
-2. Create a `Check` class which implements the `Payment` interface
34
+2. Create a `Check` class which implements the `com.zipcoder.payment.Payment` interface
35
 3. Add the required methods from the interface
35
 3. Add the required methods from the interface
36
 4. Add getter and setter test for an id
36
 4. Add getter and setter test for an id
37
   - Add a Long id field
37
   - Add a Long id field
46
 
46
 
47
 ## Part 4 - Paypal
47
 ## Part 4 - Paypal
48
 1. Create a `PayPalTest` test class
48
 1. Create a `PayPalTest` test class
49
-2. Create a `PayPal` class which implements the `Payment` interface
49
+2. Create a `PayPal` class which implements the `com.zipcoder.payment.Payment` interface
50
 3. Add the required methods from the interface
50
 3. Add the required methods from the interface
51
 4. Add getter and setter test for an id
51
 4. Add getter and setter test for an id
52
   - Add a Long id field
52
   - Add a Long id field
60
 
60
 
61
 # Section 2 - Comparable
61
 # Section 2 - Comparable
62
 Note: You may use the String/Long compareTo methods in your code
62
 Note: You may use the String/Long compareTo methods in your code
63
-1. Edit the `Payment` class to extends the Comparable interface
64
-  - `public class Payment extends Comparable<Payment>`
63
+1. Edit the `com.zipcoder.payment.Payment` class to extends the Comparable interface
64
+  - `public class com.zipcoder.payment.Payment extends Comparable<com.zipcoder.payment.Payment>`
65
   - This indicate that payment can be comparable, which means it can be sorted
65
   - This indicate that payment can be comparable, which means it can be sorted
66
-2. Fix the syntax error in your CreditCard, Check, and Paypal class by adding the compare method
66
+2. Fix the syntax error in your com.zipcoder.payment.CreditCard, Check, and Paypal class by adding the compare method
67
 3. Create a test for the `compareTo` method. Compare using the getShortDescription()
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 com.zipcoder.payment.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
69
   - The `compareTo` method will return number larger than 0 if this payment comes after payment2
69
   - The `compareTo` method will return number larger than 0 if this payment comes after payment2
70
   - The `compareTo` method will return number 0 if this payment has the same information
70
   - The `compareTo` method will return number 0 if this payment has the same information
71
   - The `compareTo` method will return number less than 0 if this payment comes before payment2
71
   - The `compareTo` method will return number less than 0 if this payment comes before payment2
72
 4. Add the code necessary to make the test pass
72
 4. Add the code necessary to make the test pass
73
 5. Create a new `PaymentSortByPayer` class which implements `java.util.Comparator`
73
 5. Create a new `PaymentSortByPayer` class which implements `java.util.Comparator`
74
   - This class will sort by payer name
74
   - This class will sort by payer name
75
-  - `public class PaymentSortByPayer implements Comparator<Payment>`
75
+  - `public class PaymentSortByPayer implements Comparator<com.zipcoder.payment.Payment>`
76
 6. Add the required `compare` method from the Comparator
76
 6. Add the required `compare` method from the Comparator
77
 7. Create a test to compare two payment
77
 7. Create a test to compare two payment
78
     - The `compare` method will return number larger than 0 if payment1 comes after payment2
78
     - The `compare` method will return number larger than 0 if payment1 comes after payment2
85
   - When there is no payment
85
   - When there is no payment
86
     
86
     
87
     ```
87
     ```
88
-      Payment[] payments = new Payment[0];
88
+      com.zipcoder.payment.Payment[] payments = new com.zipcoder.payment.Payment[0];
89
       PaymentPresenter presenter = new PaymentPresenter();
89
       PaymentPresenter presenter = new PaymentPresenter();
90
       String expected = "";
90
       String expected = "";
91
 
91
 
97
   - When there are multiple payments, you need to sort it first, then build the string using the short description
97
   - When there are multiple payments, you need to sort it first, then build the string using the short description
98
     
98
     
99
   ```
99
   ```
100
-    Payment[] payments = new Payment[2];
101
-    Payment paypal = new PayPayl(4L, "Tia Mowry", "tia@mowry.com");
102
-    Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551")
100
+    com.zipcoder.payment.Payment[] payments = new com.zipcoder.payment.Payment[2];
101
+    com.zipcoder.payment.Payment paypal = new PayPayl(4L, "Tia Mowry", "tia@mowry.com");
102
+    com.zipcoder.payment.Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551")
103
 
103
 
104
     payment[0] = paypal;
104
     payment[0] = paypal;
105
 
105
 
114
     - When there are multiple payments, you need to sort by calling the `PaymentSortByPayer` class to sort, then build the string using the short description
114
     - When there are multiple payments, you need to sort by calling the `PaymentSortByPayer` class to sort, then build the string using the short description
115
 
115
 
116
     ```
116
     ```
117
-      Payment[] payments = new Payment[2];
118
-      Payment paypal = new PayPayl(4L, "Tamara Mowry", "tamara@mowry.com");
119
-      Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551")
117
+      com.zipcoder.payment.Payment[] payments = new com.zipcoder.payment.Payment[2];
118
+      com.zipcoder.payment.Payment paypal = new PayPayl(4L, "Tamara Mowry", "tamara@mowry.com");
119
+      com.zipcoder.payment.Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551")
120
 
120
 
121
       payment[0] = paypal;
121
       payment[0] = paypal;
122
 
122
 
131
     - When there are multiple payments, you need to sort by creating a lambda to sort the payment by id, then build the string using the short description
131
     - When there are multiple payments, you need to sort by creating a lambda to sort the payment by id, then build the string using the short description
132
 
132
 
133
     ```
133
     ```
134
-      Payment[] payments = new Payment[2];
135
-      Payment paypal = new PayPayl(120L, "Tamara Mowry", "tamara@mowry.com");
136
-      Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551")
134
+      com.zipcoder.payment.Payment[] payments = new com.zipcoder.payment.Payment[2];
135
+      com.zipcoder.payment.Payment paypal = new PayPayl(120L, "Tamara Mowry", "tamara@mowry.com");
136
+      com.zipcoder.payment.Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551")
137
 
137
 
138
       payment[0] = paypal;
138
       payment[0] = paypal;
139
 
139
 

+ 14
- 0
pom.xml Просмотреть файл

7
     <groupId>com.zipcoder</groupId>
7
     <groupId>com.zipcoder</groupId>
8
     <artifactId>payment</artifactId>
8
     <artifactId>payment</artifactId>
9
     <version>1.0-SNAPSHOT</version>
9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>org.junit.jupiter</groupId>
13
+            <artifactId>junit-jupiter-api</artifactId>
14
+            <version>RELEASE</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+        <dependency>
18
+            <groupId>junit</groupId>
19
+            <artifactId>junit</artifactId>
20
+            <version>RELEASE</version>
21
+            <scope>test</scope>
22
+        </dependency>
23
+    </dependencies>
10
 
24
 
11
 
25
 
12
 </project>
26
 </project>

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

1
+package com.zipcoder.payment;
2
+
3
+import java.lang.ref.SoftReference;
4
+
5
+public class CreditCard implements Payment {
6
+
7
+    private long id;
8
+    private String payerName;
9
+    private long number;
10
+    private int expiredMonth;
11
+    private int expiredYear;
12
+
13
+    public CreditCard() {
14
+
15
+    }
16
+
17
+    public CreditCard(int id, long number, String payerName, int expiredMonth, int expiredYear ) {
18
+        this.id = id;
19
+        this.number = number;
20
+        this.payerName = payerName;
21
+        this.expiredMonth = expiredMonth;
22
+        this.expiredYear = expiredYear;
23
+    }
24
+
25
+    public static void main(String[] args){
26
+       // this(12,"Nicholas Maidanos", 4, 11);
27
+
28
+    }
29
+
30
+    public void setId(long id){
31
+        this.id = id;
32
+    }
33
+
34
+    public long getId() {
35
+        return this.id;
36
+    }
37
+
38
+    public String setPayerName(String payerName) {
39
+        return this.payerName = payerName;
40
+    }
41
+
42
+    public String getPayerName() {
43
+        return this.payerName;
44
+    }
45
+
46
+    public long getNumber() {
47
+        return number;
48
+    }
49
+
50
+    public void setNumber(long number) {
51
+        this.number = number;
52
+    }
53
+
54
+    public int getExpiredYear() {
55
+        return expiredYear;
56
+    }
57
+
58
+    public void setExpiredYear(int expiredYear) {
59
+        this.expiredYear = expiredYear;
60
+    }
61
+
62
+    public int getExpiredMonth() {
63
+        return expiredMonth;
64
+    }
65
+
66
+    public void setExpiredMonth(int expiredMonth) {
67
+        this.expiredMonth = expiredMonth;
68
+    }
69
+
70
+    public String getShortDescription() {
71
+        StringBuilder sb = new StringBuilder();
72
+
73
+        sb.append("CC ");
74
+        sb.append(this.payerName + " ");
75
+        sb.append(lastFourDigits() + " ");
76
+        sb.append(this.expiredMonth + "/" + this.expiredYear);
77
+
78
+        return sb.toString();
79
+    }
80
+
81
+    private int lastFourDigits(){
82
+        //String numToString = new StringBuilder(Long.toString(this.number)).reverse().toString();
83
+        char[] numToChar = Long.toString(this.number).toCharArray();
84
+        String lastFourString = "";
85
+        for(int i = numToChar.length - 4; i < numToChar.length; i++){
86
+            lastFourString += Character.toString(numToChar[i]);
87
+        }
88
+        return Integer.parseInt(lastFourString);
89
+    }
90
+
91
+
92
+}

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

1
+package com.zipcoder.payment;
2
+
3
+public interface Payment {
4
+
5
+    long getId();
6
+
7
+    String getPayerName();
8
+
9
+    String getShortDescription();
10
+
11
+
12
+}

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

3
 public class PaymentPresenter {
3
 public class PaymentPresenter {
4
 
4
 
5
 
5
 
6
+
6
 }
7
 }

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

1
+package com.zipcoder.payment;
2
+
3
+import static org.junit.Assert.*;
4
+import org.junit.After;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+public class CreditCardTest {
9
+
10
+    @Test
11
+    public void getAndSetIdTest(){
12
+        //When
13
+        CreditCard cc = new CreditCard();
14
+        cc.setId(100);
15
+
16
+        //Expect
17
+        long expected = 100;
18
+
19
+        //Actual
20
+        long actual =  cc.getId();
21
+
22
+        assertEquals(expected, actual);
23
+    }
24
+
25
+
26
+
27
+    @Test
28
+    public void getAndSetPayerNameTest(){
29
+        //When
30
+        CreditCard cc = new CreditCard();
31
+        cc.setPayerName("Nicholas Maidanos");
32
+
33
+        //Expect
34
+        String expected = "Nicholas Maidanos";
35
+
36
+        //Actual
37
+        String actual =  cc.getPayerName();
38
+
39
+        assertEquals(expected, actual);
40
+    }
41
+
42
+    @Test
43
+    public void getAndSetNum(){
44
+        //When
45
+        CreditCard cc = new CreditCard();
46
+        cc.setNumber(411111111);
47
+
48
+        //Expect
49
+        long expected = 411111111;
50
+
51
+        //Actual
52
+        long actual =  cc.getNumber();
53
+
54
+        assertEquals(expected, actual);
55
+    }
56
+
57
+    @Test
58
+    public void getAndSetExMonth(){
59
+        //When
60
+        CreditCard cc = new CreditCard();
61
+        cc.setExpiredMonth(12);
62
+
63
+        //Expect
64
+        int expected = 12;
65
+
66
+        //Actual
67
+        int actual =  cc.getExpiredMonth();
68
+
69
+        assertEquals(expected, actual);
70
+    }
71
+
72
+    @Test
73
+    public void getAndSetExYear(){
74
+        //When
75
+        CreditCard cc = new CreditCard();
76
+        cc.setExpiredYear(1989);
77
+
78
+        //Expect
79
+        int expected = 1989;
80
+
81
+        //Actual
82
+        int actual =  cc.getExpiredYear();
83
+
84
+        assertEquals(expected, actual);
85
+    }
86
+
87
+    @Test
88
+    public void getAndShortDescriptionTest(){
89
+        //When
90
+        CreditCard cc = new CreditCard(12, 4111131,"Nicholas Maidanos", 4, 11);
91
+
92
+        //Expect
93
+        String expected = "";
94
+
95
+        //Actual
96
+        String actual = cc.getShortDescription();
97
+        assertEquals(expected, actual);
98
+    }
99
+
100
+//    @Test
101
+//    public void getAndSetPayerShortDescriptionTest(){
102
+//        //When
103
+//        CreditCard cc = new CreditCard();
104
+//        cc.setShortDescription("This is a Credit Card");
105
+//
106
+//
107
+//
108
+//        //Actual
109
+//        String actual =  cc.getShortDescription();
110
+//
111
+//        assertEquals(expected, actual);
112
+//    }
113
+
114
+}