Skip to content

Commit 2bc152c

Browse files
committed
http://exercism.io/exercises/java/robot-name/readme
1 parent 3a24ab1 commit 2bc152c

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

Semester1/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@
123123
</dependency>
124124
-->
125125

126+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-text -->
127+
<dependency>
128+
<groupId>org.apache.commons</groupId>
129+
<artifactId>commons-text</artifactId>
130+
<version>1.1</version>
131+
</dependency>
132+
126133
</dependencies>
127134

128135
<build>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package Robot;
2+
3+
import org.apache.commons.text.CharacterPredicates;
4+
import org.apache.commons.text.RandomStringGenerator;
5+
6+
import java.util.ArrayList;
7+
8+
public class Robot {
9+
10+
/**
11+
* The first time you boot them up, a random name is generated in the format of two uppercase letters
12+
* followed by three digits, such as RX837 or BC811.
13+
* Every once in a while we need to reset a robot to its factory settings, which means that their
14+
* name gets wiped. The next time you ask (getName), it will respond with a new random name.
15+
* Robots need to have a unique name.
16+
*/
17+
18+
private static ArrayList<String> listOfUsedRobotNames = new ArrayList<>();
19+
private String name;
20+
21+
public Robot() {
22+
setName(generateNewValidName());
23+
}
24+
25+
public String getName() {
26+
if (this.name != null) {
27+
return name;
28+
}
29+
else {
30+
setName(generateNewValidName());
31+
return name;
32+
}
33+
}
34+
35+
private void setName(String name) {
36+
this.name = name;
37+
listOfUsedRobotNames.add(name);
38+
}
39+
40+
public void reset() {
41+
this.name = null;
42+
}
43+
44+
private String generateNewValidName(){
45+
String newRandomName;
46+
do {
47+
newRandomName = generateRandomNameString();
48+
} while (listOfUsedRobotNames.contains(newRandomName));
49+
return newRandomName;
50+
}
51+
52+
private static String generateRandomNameString() {
53+
String randomString;
54+
55+
RandomStringGenerator randomStringGenerator =
56+
new RandomStringGenerator.Builder()
57+
.withinRange('A', 'Z')
58+
.filteredBy(CharacterPredicates.LETTERS)
59+
.build();
60+
randomString = randomStringGenerator.generate(2).toUpperCase();
61+
62+
randomStringGenerator =
63+
new RandomStringGenerator.Builder()
64+
.withinRange('0', '9')
65+
.filteredBy(CharacterPredicates.DIGITS)
66+
.build();
67+
randomString += randomStringGenerator.generate(3);
68+
69+
return randomString;
70+
}
71+
72+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package Robot;
2+
3+
import org.junit.Test;
4+
import org.junit.Ignore;
5+
import org.junit.Before;
6+
7+
import static org.hamcrest.CoreMatchers.equalTo;
8+
import static org.hamcrest.core.Is.is;
9+
import static org.hamcrest.core.IsNot.not;
10+
import static org.junit.Assert.assertThat;
11+
12+
public class RobotTest {
13+
14+
private static final String EXPECTED_ROBOT_NAME_PATTERN = "[A-Z]{2}\\d{3}";
15+
private Robot robot;
16+
17+
@Before
18+
public void setUp() {
19+
robot = new Robot();
20+
}
21+
22+
@Test
23+
public void hasName() {
24+
assertIsValidName(robot.getName());
25+
}
26+
27+
//@Ignore("Remove to run test")
28+
@Test
29+
public void differentRobotsHaveDifferentNames() {
30+
assertThat(robot.getName(), not(equalTo(new Robot().getName())));
31+
}
32+
33+
//@Ignore("Remove to run test")
34+
@Test
35+
public void resetName() {
36+
final String name = robot.getName();
37+
robot.reset();
38+
final String name2 = robot.getName();
39+
assertThat(name, not(equalTo(name2)));
40+
assertIsValidName(name2);
41+
}
42+
43+
private static void assertIsValidName(String name) {
44+
assertThat(name.matches(EXPECTED_ROBOT_NAME_PATTERN), is(true));
45+
}
46+
47+
// @Ignore("Remove to run test")
48+
// @Test
49+
// public void test(){
50+
// System.out.println(robot.generateNewValidName());
51+
// }
52+
}

0 commit comments

Comments
 (0)