Skip to content

Commit 804ffc3

Browse files
committed
Added lottery service interface
1 parent 035b14f commit 804ffc3

3 files changed

Lines changed: 201 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.hexagonal.domain;
24+
25+
/**
26+
*
27+
* Interface for submitting and checking lottery tickets.
28+
*
29+
*/
30+
public interface LotteryService {
31+
32+
LotteryTicketSubmitResult submitTicket(LotteryTicket ticket);
33+
34+
void checkTicketForPrize(LotteryTicket ticket);
35+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.hexagonal.domain;
24+
25+
import java.util.EnumSet;
26+
27+
/**
28+
*
29+
* Immutable value object for passing lottery ticket submit results.
30+
*
31+
*/
32+
public class LotteryTicketSubmitResult {
33+
34+
/**
35+
*
36+
* Indicates if the submit was successful
37+
*
38+
*/
39+
public enum Result {OK, ERROR};
40+
41+
/**
42+
*
43+
* Indicates the fields in the lottery ticket that have errors.
44+
*
45+
*/
46+
public enum Fields {EMAIL, BANK_ACCOUNT, PHONE, NUMBERS};
47+
48+
private final Result result;
49+
50+
private final EnumSet<Fields> fields;
51+
52+
/**
53+
* Constructor.
54+
*/
55+
public LotteryTicketSubmitResult(Result submitResult) {
56+
result = submitResult;
57+
fields = EnumSet.noneOf(Fields.class);
58+
}
59+
60+
/**
61+
* Constructor.
62+
*/
63+
public LotteryTicketSubmitResult(Result submitResult, EnumSet<Fields> errorFields) {
64+
result = submitResult;
65+
fields = EnumSet.copyOf(errorFields);
66+
}
67+
68+
/**
69+
* @return submit result
70+
*/
71+
public Result getResult() {
72+
return result;
73+
}
74+
75+
/**
76+
* @return fields that have errors
77+
*/
78+
public EnumSet<Fields> getErrorFields() {
79+
return EnumSet.copyOf(fields);
80+
}
81+
82+
@Override
83+
public int hashCode() {
84+
final int prime = 31;
85+
int result = 1;
86+
result = prime * result + ((fields == null) ? 0 : fields.hashCode());
87+
result = prime * result + ((this.result == null) ? 0 : this.result.hashCode());
88+
return result;
89+
}
90+
91+
@Override
92+
public boolean equals(Object obj) {
93+
if (this == obj) {
94+
return true;
95+
}
96+
if (obj == null) {
97+
return false;
98+
}
99+
if (getClass() != obj.getClass()) {
100+
return false;
101+
}
102+
LotteryTicketSubmitResult other = (LotteryTicketSubmitResult) obj;
103+
if (fields == null) {
104+
if (other.fields != null) {
105+
return false;
106+
}
107+
} else if (!fields.equals(other.fields)) {
108+
return false;
109+
}
110+
if (result != other.result) {
111+
return false;
112+
}
113+
return true;
114+
}
115+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.hexagonal.domain;
24+
25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertFalse;
27+
28+
import java.util.EnumSet;
29+
30+
import org.junit.Test;
31+
32+
import com.iluwatar.hexagonal.domain.LotteryTicketSubmitResult.Fields;
33+
import com.iluwatar.hexagonal.domain.LotteryTicketSubmitResult.Result;
34+
35+
/**
36+
*
37+
* Unit tests for {@link LotteryTicketSubmitResult}
38+
*
39+
*/
40+
public class LotteryTicketSubmitResultTest {
41+
42+
@Test
43+
public void testEquals() {
44+
LotteryTicketSubmitResult submitResult1 = new LotteryTicketSubmitResult(Result.OK);
45+
LotteryTicketSubmitResult submitResult2 = new LotteryTicketSubmitResult(Result.OK);
46+
assertEquals(submitResult1, submitResult2);
47+
LotteryTicketSubmitResult submitResult3 = new LotteryTicketSubmitResult(
48+
Result.ERROR, EnumSet.of(Fields.EMAIL, Fields.BANK_ACCOUNT));
49+
assertFalse(submitResult1.equals(submitResult3));
50+
}
51+
}

0 commit comments

Comments
 (0)