Skip to content

Commit bc5ba46

Browse files
committed
feat: implement State class with population management and exception handling
1 parent dbd5296 commit bc5ba46

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Lesson16.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ public static void main(String[] args) {
1313
application.run(args);
1414
}
1515
}
16+
Lines changed: 7 additions & 0 deletions
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.codedifferently.lesson16.states;
2+
import java.util.ArrayList;
3+
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, MIDWEST, SOUTH, WEST, PACIFIC
15+
}
16+
public State(String name, String capital, int population, double area, Region region) {
17+
this.name = name;
18+
this.capital = capital;
19+
this.population = population;
20+
this.area = area;
21+
this.region = region;
22+
this.majorCities = new ArrayList<>();
23+
}
24+
public void updatePopulation(int newPopulation) throws PopulationTooLowException {
25+
if (newPopulation < 10000) {
26+
throw new PopulationTooLowException("Population too low for a state!");
27+
}
28+
this.population = newPopulation;
29+
System.out.println("Population updated to " + newPopulation);
30+
}
31+
public void addMajorCity(String city) {
32+
majorCities.add(city);
33+
System.out.println(city = "added to major cities of " + name);
34+
}
35+
public void listMajorCities() {
36+
System.out.println("Major cities in " + name + ":");
37+
for (String city : majorCities) {
38+
System.out.println("- " + city);
39+
}
40+
}
41+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.codedifferently.lesson16.states;
2+
public class StateTest {
3+
4+
}

0 commit comments

Comments
 (0)