Skip to content

Commit ba5326d

Browse files
authored
feat: adds Nia's custom state object (#498)
* feat: implement State class with population management and exception handling * feat: reintroduce State class with population management and exception handling
1 parent c11a986 commit ba5326d

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.states;
2+
3+
class PopulationTooLowException extends Exception {
4+
public PopulationTooLowException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.codedifferently.lesson16.states;
2+
3+
import java.util.ArrayList;
4+
5+
public class State {
6+
private String name;
7+
private String capital;
8+
private int population;
9+
private double area;
10+
private Region region;
11+
private ArrayList<String> majorCities;
12+
13+
enum Region {
14+
NORTHEAST,
15+
MIDWEST,
16+
SOUTH,
17+
WEST,
18+
PACIFIC
19+
}
20+
21+
public State(String name, String capital, int population, double area, Region region) {
22+
this.name = name;
23+
this.capital = capital;
24+
this.population = population;
25+
this.area = area;
26+
this.region = region;
27+
this.majorCities = new ArrayList<>();
28+
}
29+
30+
public void updatePopulation(int newPopulation) throws PopulationTooLowException {
31+
if (newPopulation < 10000) {
32+
throw new PopulationTooLowException("Population too low for a state!");
33+
}
34+
this.population = newPopulation;
35+
System.out.println("Population updated to " + newPopulation);
36+
}
37+
38+
public void addMajorCity(String city) {
39+
majorCities.add(city);
40+
System.out.println(city = "added to major cities of " + name);
41+
}
42+
43+
public void listMajorCities() {
44+
System.out.println("Major cities in " + name + ":");
45+
for (String city : majorCities) {
46+
System.out.println("- " + city);
47+
}
48+
}
49+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.codedifferently.lesson16.states;
2+
3+
import static org.assertj.core.api.Assertions.*;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.PrintStream;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class StateTest {
11+
12+
private State state;
13+
14+
@BeforeEach
15+
public void setUp() {
16+
state = new State("California", "Sacramento", 39500000, 423967, State.Region.WEST);
17+
}
18+
19+
@Test
20+
public void constructor_ShouldInitializeFieldsCorrectly() {
21+
assertThat(state)
22+
.extracting("name", "capital", "population", "area", "region")
23+
.containsExactly("California", "Sacramento", 39500000, 423967.0, State.Region.WEST);
24+
}
25+
26+
@Test
27+
public void updatePopulation_ShouldUpdateCorrectly_WhenValid() throws PopulationTooLowException {
28+
state.updatePopulation(5000000);
29+
assertThat(state).extracting("population").isEqualTo(5000000);
30+
}
31+
32+
@Test
33+
public void updatePopulation_ShouldThrowException_WhenTooLow() {
34+
assertThatThrownBy(() -> state.updatePopulation(9000))
35+
.isInstanceOf(PopulationTooLowException.class)
36+
.hasMessage("Population too low for a state!");
37+
}
38+
39+
@Test
40+
public void addMajorCity_ShouldAddCityToList() {
41+
state.addMajorCity("Los Angeles");
42+
state.addMajorCity("San Francisco");
43+
44+
// Check if the cities were added (accessing via reflection for private field)
45+
assertThat(state)
46+
.extracting("majorCities")
47+
.asList()
48+
.containsExactly("Los Angeles", "San Francisco");
49+
}
50+
51+
@Test
52+
public void listMajorCities_ShouldPrintCityList() {
53+
state.addMajorCity("Los Angeles");
54+
state.addMajorCity("San Diego");
55+
56+
// Capture the output
57+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
58+
System.setOut(new PrintStream(outContent));
59+
60+
state.listMajorCities();
61+
62+
System.setOut(System.out); // Reset back
63+
64+
String expectedOutput = "Major cities in California:\n- Los Angeles\n- San Diego\n";
65+
assertThat(outContent.toString().trim()).isEqualToIgnoringNewLines(expectedOutput.trim());
66+
}
67+
}

0 commit comments

Comments
 (0)