#35 Jamez-s InterfaceComparableLab

Open
Jamez-s wants to merge 6 commits from Jamez-s/InterfaceComparableLab:master into master

BIN
.DS_Store View File


+ 8
- 0
pom.xml View File

@@ -7,6 +7,14 @@
7 7
     <groupId>com.zipcoder</groupId>
8 8
     <artifactId>payment</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>RELEASE</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
     <properties>
11 19
         <maven.compiler.source>1.8</maven.compiler.source>
12 20
         <maven.compiler.target>1.8</maven.compiler.target>

+ 69
- 0
src/main/java/com/zipcoder/payment/Check.java View File

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

+ 81
- 0
src/main/java/com/zipcoder/payment/CreditCard.java View File

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

+ 36
- 1
src/main/java/com/zipcoder/payment/PaymentPresenter.java View File

@@ -1,6 +1,41 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+		import java.util.ArrayList;
4
+		import java.util.Arrays;
5
+		import java.util.Comparator;
6
+
3 7
 public class PaymentPresenter {
4 8
 
5 9
 
6
-}
10
+	public String toString(Payments[] payments) {
11
+		StringBuilder sb = new StringBuilder();
12
+		Arrays.sort(payments);
13
+		for (int i = 0; i < payments.length; i++) {
14
+			sb.append(payments[i].getShortDescription()+"\n");
15
+		}
16
+		return sb.toString();
17
+	}
18
+
19
+
20
+	public String toStringByPayerName(Payments[] payments) {
21
+		StringBuilder sb = new StringBuilder();
22
+		Arrays.sort(payments, new PaymentSortByPayer());
23
+		for (int i = 0; i < payments.length; i++) {
24
+			sb.append(payments[i].getShortDescription()+"\n");
25
+		}
26
+		return sb.toString();
27
+
28
+	}
29
+
30
+	public String toStringById(Payments[] payments) {
31
+
32
+		Comparator<Payments> id = ((o1, o2) -> o1.getID().compareTo(o2.getID()));
33
+				StringBuilder sb = new StringBuilder();
34
+		Arrays.sort(payments, id);
35
+		for (int i = 0; i < payments.length; i++) {
36
+			sb.append(payments[i].getShortDescription()+"\n");
37
+		}
38
+		return sb.toString();
39
+
40
+	}
41
+	}

+ 13
- 0
src/main/java/com/zipcoder/payment/PaymentSortByPayer.java View File

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

+ 13
- 0
src/main/java/com/zipcoder/payment/Payments.java View File

@@ -0,0 +1,13 @@
1
+package com.zipcoder.payment;
2
+
3
+public interface Payments extends Comparable<Payments>{
4
+
5
+	public Long getID();
6
+	public void setId(Long id);
7
+	public String getPayerName();
8
+	public String getShortDescription();
9
+
10
+	void setPayerName(String payerName);
11
+}
12
+
13
+

+ 56
- 0
src/main/java/com/zipcoder/payment/Paypal.java View File

@@ -0,0 +1,56 @@
1
+package com.zipcoder.payment;
2
+
3
+public class Paypal implements Payments {
4
+
5
+	Long id;
6
+	String payerName;
7
+	String email;
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
+	Paypal(){}
17
+
18
+	public String getEmail() {
19
+		return email;
20
+	}
21
+
22
+	public void setEmail(String email) {
23
+		this.email = email;
24
+	}
25
+
26
+	@Override
27
+	public Long getID() {
28
+		return id;
29
+	}
30
+
31
+	@Override
32
+	public void setId(Long id) {
33
+		this.id = id;
34
+	}
35
+
36
+	@Override
37
+	public String getPayerName() {
38
+		return payerName;
39
+	}
40
+
41
+	@Override
42
+	public String getShortDescription() {
43
+		return "Paypal "+ payerName +" "+email;
44
+	}
45
+
46
+	@Override
47
+	public void setPayerName(String payerName) {
48
+		this.payerName = payerName;
49
+	}
50
+
51
+	@Override
52
+	public int compareTo(Payments o) {
53
+
54
+		return this.getShortDescription().compareTo(o.getShortDescription());
55
+	}
56
+}

+ 99
- 0
src/test/java/com/zipcoder/payment/CheckTest.java View File

@@ -0,0 +1,99 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class CheckTest {
8
+	Check test = new Check();
9
+
10
+
11
+	@Before
12
+	public void setup() {
13
+	}
14
+
15
+	@Test
16
+	public void getIdTest() {
17
+		//When
18
+		test.setId(23L);
19
+
20
+		// Expect
21
+		Long expect = 23L;
22
+
23
+		//Actual
24
+		Long actual = test.getID();
25
+
26
+		Assert.assertEquals(expect, actual);
27
+	}
28
+
29
+	@Test
30
+	public void setIdTest() {
31
+		test.setId(30L);
32
+
33
+		Long expect = 30L;
34
+
35
+		Long actual = test.getID();
36
+
37
+		Assert.assertEquals(expect, actual);
38
+	}
39
+
40
+
41
+	@Test
42
+	public void getPayerName() {
43
+		test.setPayerName("Tia Mowry");
44
+
45
+		String expect = "Tia Mowry";
46
+		String actual = test.getPayerName();
47
+
48
+		Assert.assertEquals(expect, actual);
49
+	}
50
+
51
+	@Test
52
+	public void getShortDescription() {
53
+		test.setPayerName("Tia Mowry");
54
+		test.setAccountNumber("4551");
55
+
56
+		String expect = "Check Tia Mowry ***4551";
57
+		String actual = test.getShortDescription();
58
+
59
+		Assert.assertEquals(expect, actual);
60
+	}
61
+
62
+	@Test
63
+	public void getAccountNumber() {
64
+		test.setAccountNumber("4551");
65
+
66
+		String expect = "4551";
67
+		String actual = test.getAccountNumber();
68
+
69
+		Assert.assertEquals(expect, actual);
70
+
71
+	}
72
+
73
+	@Test
74
+	public void getRoutingNumber() {
75
+		test.setRoutingNumber("904551");
76
+
77
+		String expect = "904551";
78
+		String actual = test.getRoutingNumber();
79
+
80
+		Assert.assertEquals(expect, actual);
81
+
82
+	}
83
+
84
+
85
+	@Test
86
+	public void compareToTest() {
87
+
88
+		test.setPayerName("Tia Mowry");
89
+		test.setAccountNumber("904551");
90
+
91
+		Check test1 = new Check();
92
+		test1.setPayerName("Tia Mowry");
93
+		test1.setAccountNumber("904551");
94
+
95
+		int actual = test.getShortDescription().compareTo(test1.getShortDescription());
96
+		Assert.assertTrue(actual == 0);
97
+
98
+	}
99
+}

+ 100
- 0
src/test/java/com/zipcoder/payment/CreditCardTest.java View File

@@ -0,0 +1,100 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class CreditCardTest {
8
+	CreditCard test = new CreditCard();
9
+
10
+
11
+	@Before
12
+	public void setup() {
13
+	}
14
+
15
+	@Test
16
+	public void getIdTest() {
17
+		//When
18
+		test.setId(23L);
19
+
20
+		// Expect
21
+		Long expect = 23L;
22
+
23
+		//Actual
24
+		Long actual = test.getID();
25
+
26
+		Assert.assertEquals(expect, actual);
27
+	}
28
+
29
+	@Test
30
+	public void setIdTest() {
31
+		test.setId(30L);
32
+
33
+		Long expect = 30L;
34
+
35
+		Long actual = test.getID();
36
+
37
+		Assert.assertEquals(expect, actual);
38
+	}
39
+
40
+
41
+	@Test
42
+	public void getPayerName() {
43
+		test.setPayerName("Tia Mowry");
44
+
45
+		String expect = "Tia Mowry";
46
+		String actual = test.getPayerName();
47
+
48
+		Assert.assertEquals(expect, actual);
49
+	}
50
+
51
+	@Test
52
+	public void getShortDescription() {
53
+		test.setPayerName("Tia Mowry");
54
+		test.setNumber("4551");
55
+		test.setExpiredMonth(10);
56
+		test.setExpiredYear(2019);
57
+
58
+
59
+		String expect = "CC Tia Mowry 4551 10/2019";
60
+		String actual = test.getShortDescription();
61
+
62
+		Assert.assertEquals(expect, actual);
63
+	}
64
+
65
+	@Test
66
+	public void getNumberTest() {
67
+		test.setNumber("4551");
68
+
69
+		String expect = "4551";
70
+		String actual = test.getNumber();
71
+
72
+		Assert.assertEquals(expect, actual);
73
+
74
+	}
75
+
76
+
77
+	@Test
78
+	public void getExpiredMonth() {
79
+		test.setExpiredMonth(9);
80
+
81
+		int expect = 9;
82
+		int actual = test.getExpiredMonth();
83
+
84
+		Assert.assertEquals(expect, actual);
85
+	}
86
+
87
+	@Test
88
+	public void compareToTest() {
89
+
90
+		test.setPayerName("Tia Mowry");
91
+		test.setNumber("4551");
92
+
93
+		Check test1 = new Check();
94
+		test1.setPayerName("Tia Mowry");
95
+		test1.setAccountNumber("***4551");
96
+
97
+		int actual = test.getShortDescription().compareTo(test1.getShortDescription());
98
+		Assert.assertTrue(actual < 0);
99
+	}
100
+}

+ 86
- 1
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java View File

@@ -1,4 +1,89 @@
1 1
 package com.zipcoder.payment;
2 2
 
3
+import java.util.Arrays;
4
+import org.junit.Assert;
5
+import org.junit.Before;
6
+import org.junit.Test;
7
+
8
+import static org.junit.Assert.assertEquals;
9
+import static org.junit.Assert.assertTrue;
10
+
3 11
 public class PaymentPresenterTest {
4
-}
12
+
13
+	Check check;
14
+	Paypal paypal;
15
+
16
+
17
+	PaymentPresenter presenter;
18
+
19
+	@Before
20
+	public void setup() {
21
+		StringBuilder sb = new StringBuilder();
22
+
23
+	}
24
+
25
+
26
+	@Test
27
+	public void toStringNoPayment() {
28
+
29
+		Payments[] payments = new Payments[0];
30
+		PaymentPresenter presenter = new PaymentPresenter();
31
+		String expected = "";
32
+
33
+		String actual = presenter.toString(payments);
34
+
35
+		assertEquals(expected, actual);
36
+	}
37
+
38
+	@Test
39
+
40
+	public void toStringByPayerName() {
41
+		Payments[] payments = new Payments[2];
42
+		Payments paypal = new Paypal(4L, "Tia Mowry", "tia@mowry.com");
43
+		Payments check = new Check(81L, "Tia Mowry", "11432543", "134344551");
44
+
45
+		payments[0] = paypal;
46
+		payments[1] = check;
47
+
48
+		PaymentPresenter presenter = new PaymentPresenter();
49
+		String expected = "Check Tia Mowry ***4551\nPaypal Tia Mowry tia@mowry.com\n";
50
+
51
+		String actual = presenter.toString(payments);
52
+		assertEquals(expected, actual);
53
+
54
+
55
+	}
56
+
57
+	@Test
58
+	public void toStringByPayer() {
59
+		Payments[] payments = new Payments[2];
60
+		Payments paypal = new Paypal(4L, "Tamara Mowry", "tamara@mowry.com");
61
+		Payments 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 = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
68
+
69
+		String actual = presenter.toStringByPayerName(payments);
70
+		assertEquals(expected, actual);
71
+	}
72
+
73
+	@Test
74
+	public void toStringById(){
75
+		Payments[] payments = new Payments[2];
76
+		Payments paypal = new Paypal(81L, "Tamara Mowry", "tamara@mowry.com");
77
+		Payments check = new Check(120L, "Tia Mowry", "11432543", "134344551");
78
+
79
+		payments[0] = paypal;
80
+		payments[1] = check;
81
+
82
+		PaymentPresenter presenter = new PaymentPresenter();
83
+		String expected = "Paypal Tamara Mowry tamara@mowry.com\nCheck Tia Mowry ***4551\n";
84
+
85
+		String actual = presenter.toStringById(payments);
86
+		assertEquals(expected, actual);
87
+	}
88
+
89
+}

+ 29
- 0
src/test/java/com/zipcoder/payment/PaymentSortByPayerTest.java View File

@@ -0,0 +1,29 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class PaymentSortByPayerTest {
8
+
9
+	CreditCard check1;
10
+	Check check2;
11
+
12
+	PaymentSortByPayer paymentSortByPayer;
13
+
14
+	@Before
15
+
16
+	public void setup(){
17
+		paymentSortByPayer = new PaymentSortByPayer();
18
+		check1 = new CreditCard(0L,"James","4033",10,2000);
19
+		check2 = new Check(201111L, "James", "334344", "343423434");
20
+	}
21
+
22
+	@Test
23
+	public void compareTest(){
24
+
25
+		int actual = paymentSortByPayer.compare(check1, check2);
26
+
27
+		Assert.assertTrue(actual ==0);
28
+	}
29
+}

+ 71
- 0
src/test/java/com/zipcoder/payment/PaypalTest.java View File

@@ -0,0 +1,71 @@
1
+package com.zipcoder.payment;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+
8
+public class PaypalTest {
9
+
10
+	Paypal test;
11
+
12
+	@Before
13
+	public void setup() {
14
+		test = new Paypal();
15
+	}
16
+
17
+	@Test
18
+	public void getID() {
19
+		//When
20
+		test.setId(23L);
21
+
22
+		// Expect
23
+		Long expect = 23L;
24
+
25
+		//Actual
26
+		Long actual = test.getID();
27
+
28
+		Assert.assertEquals(expect, actual);
29
+	}
30
+
31
+
32
+	@Test
33
+	public void getPayerName() {
34
+		//When
35
+		test.setPayerName("Tia Mowry");
36
+
37
+		// Expect
38
+		String expect = "Tia Mowry";
39
+
40
+		//Actual
41
+		String actual = test.getPayerName();
42
+
43
+		Assert.assertEquals(expect, actual);
44
+	}
45
+
46
+
47
+	@Test
48
+	public void getShortDescription() {
49
+		test.setPayerName("Tia Mowry");
50
+		test.setEmail("tia@mowry.com");
51
+
52
+		String expect = "Paypal Tia Mowry tia@mowry.com";
53
+		String actual = test.getShortDescription();
54
+		Assert.assertEquals(expect, actual);
55
+
56
+	}
57
+
58
+	@Test
59
+	public void compareTo() {
60
+		test.setPayerName("Tia Mowry");
61
+		test.setEmail("tia@mowry.com");
62
+
63
+		Check test1 = new Check();
64
+		test1.setPayerName("Tia Mowry");
65
+		test1.setAccountNumber("***4551");
66
+
67
+		int actual = test.getShortDescription().compareTo(test1.getShortDescription());
68
+		Assert.assertTrue(actual > 0);
69
+
70
+	}
71
+}