Jamez-s 8 лет назад
Родитель
Сommit
152db494ba

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

1
 package com.zipcoder.payment;
1
 package com.zipcoder.payment;
2
 
2
 
3
+		import java.util.ArrayList;
3
 		import java.util.Arrays;
4
 		import java.util.Arrays;
4
 		import java.util.Comparator;
5
 		import java.util.Comparator;
5
 
6
 
6
 public class PaymentPresenter {
7
 public class PaymentPresenter {
7
 
8
 
9
+
8
 	public String toString(Payments[] payments) {
10
 	public String toString(Payments[] payments) {
9
 		StringBuilder sb = new StringBuilder();
11
 		StringBuilder sb = new StringBuilder();
10
 		Arrays.sort(payments);
12
 		Arrays.sort(payments);
11
 		for (int i = 0; i < payments.length; i++) {
13
 		for (int i = 0; i < payments.length; i++) {
12
-			payments[i].getShortDescription().toString();
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");
13
 		}
25
 		}
14
-		return null;
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
+	}
15
 	}
41
 	}
16
-}

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

9
 
9
 
10
 		return o1.getPayerName().compareTo(o2.getPayerName());
10
 		return o1.getPayerName().compareTo(o2.getPayerName());
11
 	}
11
 	}
12
+
12
 }
13
 }

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

40
 
40
 
41
 	@Override
41
 	@Override
42
 	public String getShortDescription() {
42
 	public String getShortDescription() {
43
-		return "Paypal "+ payerName+ email;
43
+		return "Paypal "+ payerName +" "+email;
44
 	}
44
 	}
45
 
45
 
46
 	@Override
46
 	@Override

+ 53
- 2
src/test/java/com/zipcoder/payment/PaymentPresenterTest.java Просмотреть файл

6
 import org.junit.Test;
6
 import org.junit.Test;
7
 
7
 
8
 import static org.junit.Assert.assertEquals;
8
 import static org.junit.Assert.assertEquals;
9
+import static org.junit.Assert.assertTrue;
9
 
10
 
10
 public class PaymentPresenterTest {
11
 public class PaymentPresenterTest {
11
 
12
 
13
+	Check check;
14
+	Paypal paypal;
15
+
16
+
17
+	PaymentPresenter presenter;
12
 
18
 
13
 	@Before
19
 	@Before
14
 	public void setup() {
20
 	public void setup() {
15
 		StringBuilder sb = new StringBuilder();
21
 		StringBuilder sb = new StringBuilder();
16
-	}
17
 
22
 
23
+	}
18
 
24
 
19
 
25
 
20
 	@Test
26
 	@Test
31
 
37
 
32
 	@Test
38
 	@Test
33
 
39
 
34
-	public void multiplesToStringById() {
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
+
35
 
54
 
36
 	}
55
 	}
37
 
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
+
38
 }
89
 }

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

51
 
51
 
52
 		String expect = "Paypal Tia Mowry tia@mowry.com";
52
 		String expect = "Paypal Tia Mowry tia@mowry.com";
53
 		String actual = test.getShortDescription();
53
 		String actual = test.getShortDescription();
54
+		Assert.assertEquals(expect, actual);
55
+
54
 	}
56
 	}
55
 
57
 
56
 	@Test
58
 	@Test