File tree 2 files changed +24
-3
lines changed
exercises/practice/robot-name
2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .HashSet ;
1
2
import java .util .Random ;
3
+ import java .util .Set ;
2
4
3
5
public class Robot {
4
6
5
7
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
6
8
private String name ;
7
9
private final Random random ;
10
+ private static Set <String > nameLog = new HashSet <>();
8
11
9
12
public Robot () {
10
13
random = new Random ();
11
14
assignNewName ();
12
15
}
13
16
14
17
private void assignNewName () {
15
- name = String .format ("%s%d" , prefix (), suffix ());
18
+ String newName = String .format ("%s%d" , prefix (), suffix ());
19
+ while (!nameLog .add (newName )) {
20
+ newName = String .format ("%s%d" , prefix (), suffix ());
21
+ }
22
+ this .name = newName ;
16
23
}
17
24
18
25
public String getName () {
Original file line number Diff line number Diff line change 1
1
import static org .assertj .core .api .Assertions .assertThat ;
2
2
3
- import org .junit .Test ;
4
- import org .junit .Ignore ;
3
+ import java .util .HashSet ;
4
+ import java .util .Set ;
5
+
5
6
import org .junit .Before ;
7
+ import org .junit .Ignore ;
8
+ import org .junit .Test ;
6
9
7
10
public class RobotTest {
8
11
@@ -34,6 +37,17 @@ public void resetName() {
34
37
assertThat (name ).isNotEqualTo (name2 );
35
38
assertIsValidName (name2 );
36
39
}
40
+
41
+ @ Ignore ("Remove to run test" )
42
+ @ Test
43
+ public void robotNamesAreUnique () {
44
+ Set <String > robotNames = new HashSet <>();
45
+ int sampleSize = 5000 ;
46
+ for (int i = 0 ; i < sampleSize ; i ++) {
47
+ robotNames .add (new Robot ().getName ());
48
+ }
49
+ assertThat (robotNames ).hasSize (sampleSize );
50
+ }
37
51
38
52
private static void assertIsValidName (String name ) {
39
53
assertThat (name ).matches (EXPECTED_ROBOT_NAME_PATTERN );
You can’t perform that action at this time.
0 commit comments