|
@@ -0,0 +1,138 @@
|
|
1
|
+# Polymorphism Payment Lab
|
|
2
|
+
|
|
3
|
+## Objectives
|
|
4
|
+
|
|
5
|
+- To learn [interface](https://docs.oracle.com/javase/tutorial/java/concepts/interface.html)
|
|
6
|
+- To learn `Comparable` interface
|
|
7
|
+- To learn [lambda function](https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html)
|
|
8
|
+
|
|
9
|
+# Section 1 - Setting up the classes
|
|
10
|
+## Part 1 - Payment
|
|
11
|
+1. Create a `Payment` interface
|
|
12
|
+ - The class should define a `getId()` method which returns a Long
|
|
13
|
+ - The class should define a `getPayerName()` method which returns a String
|
|
14
|
+ - The class should define a `getShortDescription` method which returns a String
|
|
15
|
+
|
|
16
|
+## Part 2 - Credit Card
|
|
17
|
+1. Create a `CreditCardTest` test class
|
|
18
|
+2. Create a `CreditCard` class which implements the `Payment` interface
|
|
19
|
+3. Add the required methods from the interface
|
|
20
|
+4. Add getter and setter test for an id
|
|
21
|
+ - Add a Long id field
|
|
22
|
+ - Add a getter and setter method to make the test pass
|
|
23
|
+5. Repeat step 4 for the following fields:
|
|
24
|
+ - String payerName
|
|
25
|
+ - String number
|
|
26
|
+ - int expiredMonth
|
|
27
|
+ - int expiredYear
|
|
28
|
+6. Implement the `getShortDescription` to return `CC [payerName] [last 4 digit of the number] [expiredMonth]/[expiredYear]`
|
|
29
|
+ - ex: `CC Tia Mowry 4551 10/2019`
|
|
30
|
+7. You may create any type of constructor or methods that will with this lab
|
|
31
|
+
|
|
32
|
+## Part 3 - Check
|
|
33
|
+1. Create a `CheckTest` test class
|
|
34
|
+2. Create a `Check` class which implements the `Payment` interface
|
|
35
|
+3. Add the required methods from the interface
|
|
36
|
+4. Add getter and setter test for an id
|
|
37
|
+ - Add a Long id field
|
|
38
|
+ - Add a getter and setter method to make the test pass
|
|
39
|
+5. Repeat step 4 for the following fields:
|
|
40
|
+ - String payerName
|
|
41
|
+ - String routing number
|
|
42
|
+ - String accountNumber
|
|
43
|
+6. Implement the `getShortDescription` to return `Check [payerName] ***[last 4 digit of the account]`
|
|
44
|
+ - ex: `Check Tia Mowry ***4551`
|
|
45
|
+7. You may create any type of constructor or methods that will with this lab
|
|
46
|
+
|
|
47
|
+## Part 4 - Paypal
|
|
48
|
+1. Create a `PayPalTest` test class
|
|
49
|
+2. Create a `PayPal` class which implements the `Payment` interface
|
|
50
|
+3. Add the required methods from the interface
|
|
51
|
+4. Add getter and setter test for an id
|
|
52
|
+ - Add a Long id field
|
|
53
|
+ - Add a getter and setter method to make the test pass
|
|
54
|
+5. Repeat step 4 for the following fields:
|
|
55
|
+ - String payerName
|
|
56
|
+ - String email
|
|
57
|
+6. Implement the `getShortDescription` to return `Paypal [payerName] [email]`
|
|
58
|
+ - ex: `Paypal Tia Mowry tia@mowry.com`
|
|
59
|
+7. You may create any type of constructor or methods that will with this lab
|
|
60
|
+
|
|
61
|
+# Section 2 - Comparable
|
|
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>`
|
|
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
|
|
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
|
|
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
|
|
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
|
|
73
|
+5. Create a new `PaymentSortByPayer` class which implements `java.util.Comparator`
|
|
74
|
+ - This class will sort by payer name
|
|
75
|
+ - `public class PaymentSortByPayer implements Comparator<Payment>`
|
|
76
|
+6. Add the required `compare` method from the Comparator
|
|
77
|
+7. Create a test to compare two payment
|
|
78
|
+ - The `compare` method will return number larger than 0 if payment1 comes after payment2
|
|
79
|
+ - The `compare` method will return number 0 if the payment has the same information
|
|
80
|
+ - The `compare` method will return number less than 0 if payment1 comes before payment2
|
|
81
|
+
|
|
82
|
+# Section 3 - PaymentPresenter
|
|
83
|
+1. Create a `PaymentPresenterTest`
|
|
84
|
+2. Create a test case to test the `toString` method of the PaymentPresenter class
|
|
85
|
+ - When there is no payment
|
|
86
|
+ ```
|
|
87
|
+ Payment[] payments = new Payment[0];
|
|
88
|
+ PaymentPresenter presenter = new PaymentPresenter();
|
|
89
|
+ String expected = "";
|
|
90
|
+
|
|
91
|
+ String actual = presenter.toString(payments);
|
|
92
|
+
|
|
93
|
+ assertEquals(expected, actual);
|
|
94
|
+ ```
|
|
95
|
+ - When there are multiple payments, you need to sort it first, then build the string using the short description
|
|
96
|
+ ```
|
|
97
|
+ Payment[] payments = new Payment[2];
|
|
98
|
+ Payment paypal = new PayPayl(4L, "Tia Mowry", "tia@mowry.com");
|
|
99
|
+ Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551")
|
|
100
|
+
|
|
101
|
+ payment[0] = paypal;
|
|
102
|
+
|
|
103
|
+ PaymentPresenter presenter = new PaymentPresenter();
|
|
104
|
+ String expected = "Check Tia Mowry ***4551\nPaypal Tia Mowry tia@mowry.com\n";
|
|
105
|
+
|
|
106
|
+ String actual = presenter.toString(payments);
|
|
107
|
+ assertEquals(expected, actual);
|
|
108
|
+ ```
|
|
109
|
+ 3. Create a test case to test the `toStringByPayerName` method of the PaymentPresenter class
|
|
110
|
+ - When there are multiple payments, you need to sort by calling the `PaymentSortByPayer` class to sort, then build the string using the short description
|
|
111
|
+ ```
|
|
112
|
+ Payment[] payments = new Payment[2];
|
|
113
|
+ Payment paypal = new PayPayl(4L, "Tamara Mowry", "tamara@mowry.com");
|
|
114
|
+ Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551")
|
|
115
|
+
|
|
116
|
+ payment[0] = paypal;
|
|
117
|
+
|
|
118
|
+ PaymentPresenter presenter = new PaymentPresenter();
|
|
119
|
+ String expected = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
|
|
120
|
+
|
|
121
|
+ String actual = presenter.toString(payments);
|
|
122
|
+ assertEquals(expected, actual);
|
|
123
|
+ ```
|
|
124
|
+ 4. Create a test case to test the `toStringById` method of the PaymentPresenter class
|
|
125
|
+ - 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
|
|
126
|
+ ```
|
|
127
|
+ Payment[] payments = new Payment[2];
|
|
128
|
+ Payment paypal = new PayPayl(120L, "Tamara Mowry", "tamara@mowry.com");
|
|
129
|
+ Payment check = new Check(81L, "Tia Mowry", "11432543", "134344551")
|
|
130
|
+
|
|
131
|
+ payment[0] = paypal;
|
|
132
|
+
|
|
133
|
+ PaymentPresenter presenter = new PaymentPresenter();
|
|
134
|
+ String expected = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
|
|
135
|
+
|
|
136
|
+ String actual = presenter.toString(payments);
|
|
137
|
+ assertEquals(expected, actual);
|
|
138
|
+ ```
|