Skip to content

Commit 3a07394

Browse files
committed
feat: refactor Comedians class structure and add validation for net worth
1 parent 07f3cf2 commit 3a07394

File tree

5 files changed

+211
-61
lines changed

5 files changed

+211
-61
lines changed

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

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.codedifferently.lesson16.comedian;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public class Comedians {
7+
private final String fullName;
8+
private final String nationality;
9+
private final String activeYears;
10+
private final List<String> famousWorks;
11+
private final double netWorth;
12+
private final Map<String, Integer> socialMediaFollowers; // e.g., {"Instagram": 1000000}
13+
private final boolean isStillActive;
14+
private final ComedyGenre comedyGenre;
15+
16+
public enum ComedyGenre {
17+
OBSERVATIONAL,
18+
SATIRE,
19+
IMPROV,
20+
SLAPSTICK,
21+
DARK,
22+
PARODY
23+
}
24+
25+
public Comedians(
26+
String fullName,
27+
String nationality,
28+
String activeYears,
29+
List<String> famousWorks,
30+
double netWorth,
31+
Map<String, Integer> socialMediaFollowers,
32+
boolean isStillActive,
33+
ComedyGenre comedyGenre) {
34+
this.fullName = fullName;
35+
this.nationality = nationality;
36+
this.activeYears = activeYears;
37+
this.famousWorks = famousWorks;
38+
this.netWorth = netWorth;
39+
this.socialMediaFollowers = socialMediaFollowers;
40+
this.isStillActive = isStillActive;
41+
this.comedyGenre = comedyGenre;
42+
}
43+
44+
public String getFullName() {
45+
return fullName;
46+
}
47+
48+
public String getNationality() {
49+
return nationality;
50+
}
51+
52+
public String getActiveYears() {
53+
return activeYears;
54+
}
55+
56+
public ComedyGenre getGenre() {
57+
return comedyGenre;
58+
}
59+
60+
public List<String> getFamousWorks() {
61+
return famousWorks;
62+
}
63+
64+
public double getNetWorth() {
65+
return netWorth;
66+
}
67+
68+
public Map<String, Integer> getSocialMediaFollowers() {
69+
return socialMediaFollowers;
70+
}
71+
72+
public boolean isStillActive() {
73+
return isStillActive;
74+
}
75+
76+
public void validateNetWorth() throws InvalidNetWorthException {
77+
if (netWorth < 0) {
78+
throw new InvalidNetWorthException("Net worth cannot be negative!");
79+
}
80+
}
81+
82+
public String getCareerStatus() {
83+
return isStillActive ? "Still active" : "Retired";
84+
}
85+
86+
public void printFamousWorks() {
87+
System.out.println("Famous Works:");
88+
for (String work : famousWorks) {
89+
System.out.println("- " + work);
90+
}
91+
}
92+
93+
public int totalFollowers() {
94+
int total = 0;
95+
for (int count : socialMediaFollowers.values()) {
96+
total += count;
97+
}
98+
return total;
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.comedian;
2+
3+
public class InvalidNetWorthException extends Exception {
4+
public InvalidNetWorthException(String message) {
5+
super(message);
6+
}
7+
}

lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/ComedianTest/ComediansTest.java

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.codedifferently.lesson16.comedianstest;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import com.codedifferently.lesson16.comedian.Comedians;
7+
import com.codedifferently.lesson16.comedian.Comedians.ComedyGenre;
8+
import com.codedifferently.lesson16.comedian.InvalidNetWorthException;
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.PrintStream;
11+
import java.util.List;
12+
import java.util.Map;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
15+
16+
public class ComediansTest {
17+
18+
private Comedians testComedian;
19+
20+
@BeforeEach
21+
public void setUp() {
22+
testComedian =
23+
new Comedians(
24+
"Dave Chappelle",
25+
"USA",
26+
"1991–present",
27+
List.of("Killing Them Softly", "Sticks & Stones"),
28+
50000000,
29+
Map.of("Instagram", 1000000, "Twitter", 500000),
30+
true,
31+
ComedyGenre.SATIRE);
32+
}
33+
34+
@Test
35+
public void testFullName() {
36+
assertEquals("Dave Chappelle", testComedian.getFullName());
37+
}
38+
39+
@Test
40+
public void testCareerStatus() {
41+
assertEquals("Still active", testComedian.getCareerStatus());
42+
}
43+
44+
@Test
45+
public void testTotalFollowers() {
46+
assertEquals(1500000, testComedian.totalFollowers());
47+
}
48+
49+
@Test
50+
public void testFamousWorksSize() {
51+
assertEquals(2, testComedian.getFamousWorks().size());
52+
}
53+
54+
@Test
55+
public void testInvalidNetWorthThrowsException() {
56+
Comedians brokeComedian =
57+
new Comedians(
58+
"Test Comic",
59+
"USA",
60+
"2010–present",
61+
List.of("Set A"),
62+
-1000.0,
63+
Map.of("Instagram", 10000),
64+
true,
65+
ComedyGenre.DARK);
66+
67+
assertThrows(InvalidNetWorthException.class, brokeComedian::validateNetWorth);
68+
}
69+
70+
@Test
71+
public void testPrintFamousWorksOutput() {
72+
// comments for CH to explain method from chatGPT Set up to capture System.out
73+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
74+
PrintStream originalOut = System.out;
75+
System.setOut(new PrintStream(outContent));
76+
77+
// Call the method
78+
testComedian.printFamousWorks();
79+
80+
// Restore original System.out
81+
System.setOut(originalOut);
82+
83+
// Expected output (make sure to include newline characters properly)
84+
String expectedOutput = "Famous Works:\n- Killing Them Softly\n- Sticks & Stones\n";
85+
86+
// Compare output
87+
assertEquals(expectedOutput, outContent.toString());
88+
}
89+
90+
@Test
91+
public void testGetNationality() {
92+
assertEquals("USA", testComedian.getNationality());
93+
}
94+
95+
@Test
96+
public void testGetActiveYears() {
97+
assertEquals("1991–present", testComedian.getActiveYears());
98+
}
99+
100+
@Test
101+
public void testGetGenre() {
102+
assertEquals(ComedyGenre.SATIRE, testComedian.getGenre());
103+
}
104+
}

0 commit comments

Comments
 (0)