Nhu Nguyen 6 jaren geleden
commit
5b28386710

+ 55
- 0
.gitignore Bestand weergeven

@@ -0,0 +1,55 @@
1
+# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
2
+# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
3
+
4
+# User-specific stuff:
5
+.idea/**
6
+.idea/**/tasks.xml
7
+.idea/dictionaries
8
+
9
+# Sensitive or high-churn files:
10
+.idea/**/dataSources/
11
+.idea/**/dataSources.ids
12
+.idea/**/dataSources.xml
13
+.idea/**/dataSources.local.xml
14
+.idea/**/sqlDataSources.xml
15
+.idea/**/dynamic.xml
16
+.idea/**/uiDesigner.xml
17
+
18
+# Gradle:
19
+.idea/**/gradle.xml
20
+.idea/**/libraries
21
+
22
+*.iml
23
+# CMake
24
+cmake-build-debug/
25
+
26
+# Mongo Explorer plugin:
27
+.idea/**/mongoSettings.xml
28
+
29
+## File-based project format:
30
+*.iws
31
+
32
+## Plugin-specific files:
33
+
34
+# IntelliJ
35
+/out/
36
+
37
+# mpeltonen/sbt-idea plugin
38
+.idea_modules/
39
+
40
+# JIRA plugin
41
+atlassian-ide-plugin.xml
42
+
43
+# Cursive Clojure plugin
44
+.idea/replstate.xml
45
+
46
+# Crashlytics plugin (for Android Studio and IntelliJ)
47
+com_crashlytics_export_strings.xml
48
+crashlytics.properties
49
+crashlytics-build.properties
50
+fabric.properties
51
+
52
+.project
53
+.classpath
54
+.settings
55
+target

+ 138
- 0
README.MD Bestand weergeven

@@ -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
+    ```

+ 12
- 0
pom.xml Bestand weergeven

@@ -0,0 +1,12 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <modelVersion>4.0.0</modelVersion>
6
+
7
+    <groupId>com.zipcoder</groupId>
8
+    <artifactId>payment</artifactId>
9
+    <version>1.0-SNAPSHOT</version>
10
+
11
+
12
+</project>

+ 6
- 0
src/main/java/com/zipcoder/payment/PaymentPresenter.java Bestand weergeven

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

+ 4
- 0
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Bestand weergeven

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