Skip to content

Commit b0f96ad

Browse files
committed
Added class for lottery numbers and unit tests for it
1 parent 06546ae commit b0f96ad

File tree

4 files changed

+223
-8
lines changed

4 files changed

+223
-8
lines changed

hexagonal/src/main/java/com/iluwatar/hexagonal/App.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
package com.iluwatar.hexagonal;
2424

2525
/**
26-
* Hello world!
26+
*
27+
* Example application demonstrating Hexagonal Architecture
2728
*
2829
*/
2930
public class App {
3031
public static void main(String[] args) {
31-
System.out.println("Hello World!");
32+
System.out.println("Running hexagonal example");
3233
}
3334
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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.Collections;
26+
import java.util.HashSet;
27+
import java.util.PrimitiveIterator;
28+
import java.util.Random;
29+
import java.util.Set;
30+
31+
/**
32+
*
33+
* Value object representing lottery numbers. This lottery uses sets of 4 numbers. The numbers must be unique and
34+
* between 1 and 20.
35+
*
36+
*/
37+
public class LotteryNumbers {
38+
39+
private final Set<Integer> numbers;
40+
41+
public static final int MIN_NUMBER = 1;
42+
public static final int MAX_NUMBER = 20;
43+
public static final int NUM_NUMBERS = 4;
44+
45+
/**
46+
* Constructor. Creates random lottery numbers.
47+
*/
48+
private LotteryNumbers() {
49+
numbers = new HashSet<>();
50+
generateRandomNumbers();
51+
}
52+
53+
/**
54+
* Constructor. Uses given numbers.
55+
*/
56+
private LotteryNumbers(Set<Integer> givenNumbers) {
57+
numbers = new HashSet<>();
58+
numbers.addAll(givenNumbers);
59+
}
60+
61+
/**
62+
* @return random LotteryNumbers
63+
*/
64+
public static LotteryNumbers createRandom() {
65+
return new LotteryNumbers();
66+
}
67+
68+
/**
69+
* @return given LotteryNumbers
70+
*/
71+
public static LotteryNumbers create(Set<Integer> givenNumbers) {
72+
return new LotteryNumbers(givenNumbers);
73+
}
74+
75+
/**
76+
* @return lottery numbers
77+
*/
78+
public Set<Integer> getNumbers() {
79+
return Collections.unmodifiableSet(numbers);
80+
}
81+
82+
@Override
83+
public int hashCode() {
84+
final int prime = 31;
85+
int result = 1;
86+
result = prime * result + ((numbers == null) ? 0 : numbers.hashCode());
87+
return result;
88+
}
89+
90+
@Override
91+
public boolean equals(Object obj) {
92+
if (this == obj) {
93+
return true;
94+
}
95+
if (obj == null) {
96+
return false;
97+
}
98+
if (getClass() != obj.getClass()) {
99+
return false;
100+
}
101+
LotteryNumbers other = (LotteryNumbers) obj;
102+
if (numbers == null) {
103+
if (other.numbers != null) {
104+
return false;
105+
}
106+
} else if (!numbers.equals(other.numbers)) {
107+
return false;
108+
}
109+
return true;
110+
}
111+
112+
/**
113+
* Generates 4 unique random numbers between 1-20 into numbers set.
114+
*/
115+
private void generateRandomNumbers() {
116+
numbers.clear();
117+
RandomNumberGenerator generator = new RandomNumberGenerator(MIN_NUMBER, MAX_NUMBER);
118+
while (numbers.size() < NUM_NUMBERS) {
119+
int num = generator.nextInt();
120+
if (!numbers.contains(num)) {
121+
numbers.add(num);
122+
}
123+
}
124+
}
125+
126+
/**
127+
*
128+
* Helper class for generating random numbers.
129+
*
130+
*/
131+
private static class RandomNumberGenerator {
132+
133+
private PrimitiveIterator.OfInt randomIterator;
134+
135+
/**
136+
* Initialize a new random number generator that generates random numbers in the range [min, max]
137+
*
138+
* @param min the min value (inclusive)
139+
* @param max the max value (inclusive)
140+
*/
141+
public RandomNumberGenerator(int min, int max) {
142+
randomIterator = new Random().ints(min, max + 1).iterator();
143+
}
144+
145+
/**
146+
* @return a random number in the range (min, max)
147+
*/
148+
public int nextInt() {
149+
return randomIterator.nextInt();
150+
}
151+
}
152+
}

hexagonal/src/test/java/com/iluwatar/hexagonal/AppTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,16 @@
2222
*/
2323
package com.iluwatar.hexagonal;
2424

25-
import static org.junit.Assert.assertTrue;
26-
2725
import org.junit.Test;
2826

2927
/**
3028
* Unit test for simple App.
3129
*/
3230
public class AppTest {
3331

34-
/**
35-
* Rigourous Test :-)
36-
*/
3732
@Test
3833
public void testApp() {
39-
assertTrue(true);
34+
String[] args = {};
35+
App.main(args);
4036
}
4137
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.assertTrue;
27+
28+
import java.util.Arrays;
29+
import java.util.HashSet;
30+
31+
import org.junit.Test;
32+
33+
/**
34+
*
35+
* Unit tests for {@link LotteryNumbers}
36+
*
37+
*/
38+
public class LotteryNumbersTest {
39+
40+
private static final int NUM_RANDOM_NUMBER_ROUNDS = 1000;
41+
42+
@Test
43+
public void testGivenNumbers() {
44+
LotteryNumbers numbers = LotteryNumbers.create(
45+
new HashSet<>(Arrays.asList(1, 2, 3, 4)));
46+
assertEquals(numbers.getNumbers().size(), 4);
47+
assertTrue(numbers.getNumbers().contains(1));
48+
assertTrue(numbers.getNumbers().contains(2));
49+
assertTrue(numbers.getNumbers().contains(3));
50+
assertTrue(numbers.getNumbers().contains(4));
51+
}
52+
53+
@Test(expected = UnsupportedOperationException.class)
54+
public void testNumbersCantBeModified() {
55+
LotteryNumbers numbers = LotteryNumbers.create(
56+
new HashSet<>(Arrays.asList(1, 2, 3, 4)));
57+
numbers.getNumbers().add(5);
58+
}
59+
60+
@Test
61+
public void testRandomNumbers() {
62+
for (int i = 0; i < NUM_RANDOM_NUMBER_ROUNDS; i++) {
63+
LotteryNumbers numbers = LotteryNumbers.createRandom();
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)