Skip to content

Commit cef7aa2

Browse files
feat: adds Lesson 16- Kimberlee's object-"headphones" with unit tests (#522)
* feat: created lesson 16 object files * fix: fixed created files * feat: changed object/created new files * feat: new object test file * feats: adds object default testing/pass * feat: added tests turn on and turn off/pass * feat: added volume increase and decrease tests/pass * feat: added setcolor test/pass * feat: added function from a collection member variable * feat: added custom exception * chore: edit test/ all passed except testwireless connection tests * chore: edit attempt * chore: edit test files/ only 1 test failed * chore: added a new test making use of the loop function * chore: debugging * chore: down to one failing test * chore: fixed final test * chore: made requested changes, deleted redundant classes * chore: undo rename Signed-off-by: Anthony D. Mays <[email protected]> --------- Signed-off-by: Anthony D. Mays <[email protected]> Co-authored-by: Anthony D. Mays <[email protected]>
1 parent a2a9ebd commit cef7aa2

File tree

3 files changed

+292
-0
lines changed

3 files changed

+292
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.KimberleeObject;
2+
3+
public class ConnectionNotFoundException extends Exception {
4+
public ConnectionNotFoundException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.codedifferently.lesson16.KimberleeObject;
2+
3+
import java.util.Arrays;
4+
5+
public class HeadPhones {
6+
7+
private int volume = 0;
8+
private boolean isPoweredOn = false;
9+
private HeadPhoneColor headPhoneColor = HeadPhoneColor.BLACK;
10+
private boolean isWireless = true;
11+
private String brands = "Beats";
12+
boolean isPreferredBrand = BrandUtils.isPreferredBrand(brands);
13+
private boolean isConnectedToBluetooth = false;
14+
15+
public enum BoostMode {
16+
BASS_BOOST,
17+
VOCAL_BOOST,
18+
TREBLE_BOOST;
19+
}
20+
21+
private BoostMode currentMode;
22+
private BoostMode[] modes = BoostMode.values();
23+
private int currentModeIndex = 0;
24+
25+
public HeadPhones() {
26+
this.currentMode = BoostMode.BASS_BOOST;
27+
System.out.println("Constructor called: currentMode set to " + currentMode);
28+
}
29+
30+
public void nextBoostMode() {
31+
currentModeIndex = (currentModeIndex + 1) % modes.length;
32+
currentMode = modes[currentModeIndex];
33+
}
34+
35+
public BoostMode getCurrentMode() {
36+
System.out.println("getCurrentMode called: currentMode is " + currentMode);
37+
return currentMode;
38+
}
39+
40+
public enum HeadPhoneColor {
41+
RED,
42+
BLUE,
43+
ROSEGOLD,
44+
PINK,
45+
WHITE,
46+
BLACK;
47+
}
48+
49+
public void BrandsArray() {
50+
String[] brands = new String[5];
51+
brands[0] = "Beats";
52+
brands[1] = "Sony";
53+
brands[2] = "Bose";
54+
brands[3] = "SkullCandy";
55+
brands[4] = "Juicy";
56+
}
57+
58+
public int getVolume() {
59+
return volume;
60+
}
61+
62+
public HeadPhoneColor getHeadPhoneColor() {
63+
return headPhoneColor;
64+
}
65+
66+
public boolean isPoweredOn() {
67+
return isPoweredOn;
68+
}
69+
70+
public boolean isWireless() {
71+
return isWireless;
72+
}
73+
74+
public String[] getBrandsArray() {
75+
return BrandUtils.PREFERRED_BRANDS;
76+
}
77+
78+
public void turnOn() {
79+
isPoweredOn = true;
80+
}
81+
82+
public void turnOff() {
83+
isPoweredOn = false;
84+
}
85+
86+
public void increaseVolume() {
87+
if (volume < 100) {
88+
volume++;
89+
}
90+
}
91+
92+
public void setVolume(int volume) {
93+
if (volume >= 0 && volume <= 100) {
94+
this.volume = volume;
95+
}
96+
}
97+
98+
public void decreaseVolume() {
99+
if (volume > 0) {
100+
volume--;
101+
}
102+
}
103+
104+
public void setColor(HeadPhoneColor color) {
105+
this.headPhoneColor = color;
106+
}
107+
108+
public class BrandUtils {
109+
private static final String[] PREFERRED_BRANDS = {
110+
"Beats", "Sony", "Bose", "SkullCandy", "Juicy"
111+
};
112+
113+
public static boolean isPreferredBrand(String brand) {
114+
return Arrays.asList(PREFERRED_BRANDS).contains(brand);
115+
}
116+
}
117+
118+
public boolean connectToBluetooth() {
119+
if (isPoweredOn && isWireless) {
120+
isConnectedToBluetooth = true;
121+
}
122+
return isConnectedToBluetooth;
123+
}
124+
125+
public boolean isConnectedToBluetooth() {
126+
return connectToBluetooth();
127+
}
128+
129+
public void wirelessConnection() throws ConnectionNotFoundException {
130+
if (!isConnectedToBluetooth) {
131+
throw new ConnectionNotFoundException("Headphones Wireless Connection Not Found.");
132+
}
133+
}
134+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package com.codedifferently.lesson16.KimberleeObjectTest;
2+
3+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
import com.codedifferently.lesson16.KimberleeObject.ConnectionNotFoundException;
7+
import com.codedifferently.lesson16.KimberleeObject.HeadPhones;
8+
import com.codedifferently.lesson16.KimberleeObject.HeadPhones.BoostMode;
9+
import com.codedifferently.lesson16.KimberleeObject.HeadPhones.HeadPhoneColor;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class HeadPhonesTest {
14+
15+
private HeadPhones headphones;
16+
17+
@BeforeEach
18+
public void setUp() {
19+
headphones = new HeadPhones();
20+
}
21+
22+
@Test
23+
public void testDefaultState() {
24+
assertEquals(0, headphones.getVolume(), "Volume should be 0 by default.");
25+
assertEquals(
26+
HeadPhoneColor.BLACK, headphones.getHeadPhoneColor(), "Color should be black by default.");
27+
assertFalse(headphones.isPoweredOn(), "HeadPhones should be off by default.");
28+
assertTrue(headphones.isWireless(), "HeadPhones should be wireless by default.");
29+
assertArrayEquals(new String[] {"Beats"}, new String[] {"Beats"});
30+
assertEquals(
31+
BoostMode.BASS_BOOST,
32+
headphones.getCurrentMode(),
33+
"BASS_BOOST should be the default setting.");
34+
}
35+
36+
@Test
37+
public void testTurnOn() {
38+
// Act
39+
headphones.turnOn();
40+
// Assert
41+
assertTrue(headphones.isPoweredOn(), "Headphones should turn on after calling turnOn");
42+
}
43+
44+
@Test
45+
public void testTurnOff() {
46+
// Arrange
47+
headphones.turnOn();
48+
// Act
49+
headphones.turnOff();
50+
// Assert
51+
assertFalse(headphones.isPoweredOn(), "Headphones should turn off after calling");
52+
}
53+
54+
@Test
55+
public void testIncreaseVolume() {
56+
// Arrange
57+
headphones.turnOn();
58+
headphones.increaseVolume();
59+
assertEquals(1, headphones.getVolume(), "Volume should increase by 1");
60+
// Act
61+
headphones.setVolume(99);
62+
headphones.increaseVolume();
63+
// Assert
64+
assertEquals(100, headphones.getVolume(), "Volume should not exceed 100.");
65+
}
66+
67+
@Test
68+
public void testDecreaseVolume() {
69+
// Arrange
70+
headphones.turnOn();
71+
headphones.setVolume(1);
72+
headphones.decreaseVolume();
73+
assertEquals(0, headphones.getVolume(), "Volume should decrease to 0.");
74+
// Act
75+
headphones.decreaseVolume();
76+
// Assert
77+
assertEquals(0, headphones.getVolume(), "Volume should not go lower than 0.");
78+
}
79+
80+
@Test
81+
public void testSetColor() {
82+
// Act
83+
headphones.setColor(HeadPhoneColor.ROSEGOLD);
84+
// Assert
85+
assertEquals(
86+
HeadPhoneColor.ROSEGOLD,
87+
headphones.getHeadPhoneColor(),
88+
"Headphone color should be set to Rose Gold.");
89+
}
90+
91+
@Test
92+
public void testPreferredBrand() {
93+
// Assert
94+
assertTrue(
95+
HeadPhones.BrandUtils.isPreferredBrand("Beats"), "Beats should be a preferred brand.");
96+
assertTrue(HeadPhones.BrandUtils.isPreferredBrand("Sony"), "Sony should be a preferred brand.");
97+
assertFalse(
98+
HeadPhones.BrandUtils.isPreferredBrand("Apple"), "Apple should not be a preferred brand.");
99+
assertTrue(
100+
HeadPhones.BrandUtils.isPreferredBrand("SkullCandy"),
101+
"SkullCandy should be a preferred brand.");
102+
assertTrue(
103+
HeadPhones.BrandUtils.isPreferredBrand("Juicy"), "Juicy should be a preferred brand.");
104+
}
105+
106+
@Test
107+
public void testwirelessConnection() throws Exception {
108+
// Arrange
109+
headphones.turnOn();
110+
// Act
111+
headphones.connectToBluetooth();
112+
// Assert
113+
assertTrue(
114+
headphones.isConnectedToBluetooth(),
115+
"Headphones should connect to bluetooh when wireless and turned on.");
116+
}
117+
118+
@Test
119+
public void testwirelessConnection_connectionNotFound() throws Exception {
120+
// Act
121+
assertThatThrownBy(() -> headphones.wirelessConnection())
122+
.isInstanceOf(ConnectionNotFoundException.class)
123+
.hasMessage("Headphones Wireless Connection Not Found.");
124+
}
125+
126+
@Test
127+
public void testBoostMode() {
128+
assertEquals(
129+
BoostMode.BASS_BOOST,
130+
headphones.getCurrentMode(),
131+
"BASS_BOOST should be the default setting.");
132+
133+
headphones.nextBoostMode();
134+
assertEquals(
135+
BoostMode.VOCAL_BOOST,
136+
headphones.getCurrentMode(),
137+
"The next Boost setting should be VOCAL.");
138+
139+
headphones.nextBoostMode();
140+
assertEquals(
141+
BoostMode.TREBLE_BOOST,
142+
headphones.getCurrentMode(),
143+
"The next Boost setting should be TREBLE.");
144+
145+
headphones.nextBoostMode();
146+
assertEquals(
147+
BoostMode.BASS_BOOST,
148+
headphones.getCurrentMode(),
149+
"BoostMode cycle should return to BASS_BOOST.");
150+
}
151+
}

0 commit comments

Comments
 (0)