Skip to content

Commit 319a77c

Browse files
authored
feat: adds Trinitie's lesson_16 homework about custom data types (#567)
* feat: adds lesson_16 homework about Custom Data Types with all tests passed * chore: adds/plays with more tests, constructors, functions, and variables * chore: fixing startCall() to make more sense
1 parent 2faa8b1 commit 319a77c

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.trinitiejackson;
2+
3+
public class CriticalBatteryException extends RuntimeException {
4+
public CriticalBatteryException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.codedifferently.lesson16.trinitiejackson;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Phone {
7+
private int batteryLevel;
8+
private boolean isOn;
9+
private boolean callInProgress;
10+
private Volume currentVolume;
11+
private final List<String> textReady;
12+
private final String brand;
13+
14+
public Phone(int batteryLevel, List<String> textReady, String brand) {
15+
this.batteryLevel = batteryLevel;
16+
this.isOn = false;
17+
this.callInProgress = false;
18+
this.currentVolume = Volume.LOW;
19+
this.textReady = new ArrayList<>(textReady);
20+
this.brand = brand;
21+
}
22+
23+
public boolean isOn() {
24+
return isOn;
25+
}
26+
27+
public int getBatteryLevel() {
28+
return batteryLevel;
29+
}
30+
31+
public String getBrand() {
32+
return brand;
33+
}
34+
35+
public boolean isCallInProgress() {
36+
return callInProgress;
37+
}
38+
39+
public Volume getCurrentVolume() {
40+
return currentVolume;
41+
}
42+
43+
public List<String> getTextReady() {
44+
return new ArrayList<>(textReady);
45+
}
46+
47+
public void powerOn() {
48+
isOn = true;
49+
}
50+
51+
public void powerOff() {
52+
isOn = false;
53+
}
54+
55+
public void setCurrentVolume(Volume volume) {
56+
this.currentVolume = volume;
57+
}
58+
59+
public boolean lowBattery() {
60+
return batteryLevel < 21;
61+
}
62+
63+
public void startCall() throws CriticalBatteryException {
64+
if (batteryLevel <= 5) {
65+
throw new CriticalBatteryException("Battery too low to start a call");
66+
}
67+
68+
if (isOn && batteryLevel > 5) {
69+
batteryLevel -= 2;
70+
callInProgress = true;
71+
} else {
72+
callInProgress = false;
73+
}
74+
}
75+
76+
public String printAllVolumeSettings() {
77+
StringBuilder sb = new StringBuilder();
78+
79+
for (Volume v : Volume.values()) {
80+
if (sb.length() > 0) {
81+
sb.append(", ");
82+
}
83+
sb.append(v.name());
84+
}
85+
86+
return sb.toString();
87+
}
88+
89+
public void addTextReady(String text) {
90+
textReady.add(text);
91+
}
92+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.codedifferently.lesson16.trinitiejackson;
2+
3+
public enum Volume {
4+
MUTE,
5+
LOW,
6+
MEDIUM,
7+
HIGH
8+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.codedifferently.trinitiejackson;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
6+
import com.codedifferently.lesson16.trinitiejackson.CriticalBatteryException;
7+
import com.codedifferently.lesson16.trinitiejackson.Phone;
8+
import com.codedifferently.lesson16.trinitiejackson.Volume;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
public class PhoneTest {
15+
private Phone phone1;
16+
private Phone phone2;
17+
18+
@BeforeEach
19+
public void setUp() {
20+
phone1 = new Phone(50, new ArrayList<>(List.of("Hello", "How are you?")), "Samsung");
21+
phone2 = new Phone(2, new ArrayList<>(List.of("Goodbye", "Why?")), "Apple");
22+
}
23+
24+
@Test
25+
public void testIsOn() {
26+
assertThat(phone1.isOn()).isFalse();
27+
}
28+
29+
@Test
30+
public void testPowerOn() {
31+
phone1.powerOn();
32+
assertThat(phone1.isOn()).isTrue();
33+
}
34+
35+
@Test
36+
public void testPowerOff() {
37+
phone2.powerOff();
38+
assertThat(phone2.isOn()).isFalse();
39+
}
40+
41+
@Test
42+
public void testGetBatteryLevel() {
43+
assertThat(phone1.getBatteryLevel()).isEqualTo(50);
44+
}
45+
46+
@Test
47+
public void testLowBattery() {
48+
assertThat(phone2.getBatteryLevel()).isLessThanOrEqualTo(21);
49+
assertThat(phone2.lowBattery()).isTrue();
50+
}
51+
52+
@Test
53+
public void testGetBrand() {
54+
assertThat(phone1.getBrand()).isEqualTo("Samsung");
55+
}
56+
57+
@Test
58+
public void testStartCall() {
59+
phone1.powerOn();
60+
phone1.startCall();
61+
62+
assertThat(phone1.isOn()).isTrue();
63+
assertThat(phone1.getBatteryLevel()).isGreaterThan(5);
64+
assertThat(phone1.isCallInProgress()).isTrue();
65+
}
66+
67+
@Test
68+
public void testStartCallCriticalBatteryThrowsException() {
69+
assertThatThrownBy(() -> phone2.startCall())
70+
.isInstanceOf(CriticalBatteryException.class)
71+
.hasMessageContaining("Battery too low to start a call");
72+
}
73+
74+
@Test
75+
public void testGetCurrentVolume() {
76+
assertThat(phone1.getCurrentVolume()).isEqualTo(Volume.LOW);
77+
}
78+
79+
@Test
80+
public void testSetCurrentVolume() {
81+
phone1.setCurrentVolume(Volume.HIGH);
82+
assertThat(phone1.getCurrentVolume()).isEqualTo(Volume.HIGH);
83+
}
84+
85+
@Test
86+
public void testPrintAllVolumeSettings() {
87+
String allVolumes = phone1.printAllVolumeSettings();
88+
assertThat(allVolumes).isEqualTo("MUTE, LOW, MEDIUM, HIGH");
89+
}
90+
91+
@Test
92+
public void testGetTextReady() {
93+
List<String> texts = phone1.getTextReady();
94+
assertThat(texts.size()).isEqualTo(2);
95+
assertThat(texts).contains("Hello", "How are you?");
96+
}
97+
98+
@Test
99+
public void testAddTextReady() {
100+
phone1.addTextReady("Ok");
101+
102+
List<String> texts = phone1.getTextReady();
103+
assertThat(texts.size()).isEqualTo(3);
104+
assertThat(texts).contains("Ok");
105+
}
106+
}

0 commit comments

Comments
 (0)