Skip to content

Commit d1e4e5c

Browse files
committed
feat: added custom exception
1 parent fa74a57 commit d1e4e5c

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
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+
}

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/KimberleeObject/HeadPhones.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class HeadPhones {
1111
private HeadPhoneColor headPhoneColor = HeadPhoneColor.BLACK;
1212
private boolean isWireless = true;
1313
private String brands = "Beats";
14+
private boolean isConnectedToBluetooth = false;
1415

1516
public enum HeadPhoneColor {
1617
RED,
@@ -81,12 +82,27 @@ public void setColor(HeadPhoneColor color) {
8182
}
8283

8384
public class BrandUtils {
84-
private static final String[] PREFERRED_BRANDS = {
85-
"Beats", "Sony", "Bose", "SkullCandy", "Juicy"
86-
};
85+
private static final String[] PREFERRED_BRANDS = {"Beats", "Sony", "Bose", "SkullCandy", "Juicy"};
8786

88-
public static boolean isPreferredBrand(String brand) {
89-
return Arrays.asList(PREFERRED_BRANDS).contains(brand);
90-
}
87+
public static boolean isPreferredBrand(String brand) {
88+
return Arrays.asList(PREFERRED_BRANDS).contains(brand);
9189
}
90+
}
91+
92+
public void connectToBluetooth() {
93+
if (isPoweredOn && isWireless) {
94+
isConnectedToBluetooth = true;
95+
}
96+
}
97+
98+
public boolean isConnectedToBluetooth() {
99+
return isConnectedToBluetooth;
100+
}
101+
102+
public void wirelessConnection() throws ConnectionNotFoundException {
103+
if (!isConnectedToBluetooth) {
104+
throw new ConnectionNotFoundException("Headphones Wireless Connection Not Found.");
105+
}
106+
107+
}
92108
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.codedifferently.lesson16.KimberleeObject;
2+
3+
public class WirelessConnection {
4+
5+
public WirelessConnection get(boolean isConnectedToBluetooth) {
6+
// TODO Auto-generated method stub
7+
throw new UnsupportedOperationException("Unimplemented method 'get'");
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.codedifferently.lesson16.KimberleeObjectTest;
2+
3+
public class ConnectionNotFoundException {
4+
5+
}

lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/KimberleeObjectTest/HeadPhonesTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
56
import com.codedifferently.lesson16.KimberleeObject.HeadPhones;
67
import com.codedifferently.lesson16.KimberleeObject.HeadPhones.HeadPhoneColor;
78
import org.junit.jupiter.api.BeforeEach;
@@ -91,4 +92,24 @@ public void testPreferredBrand() {
9192
BrandsArray.isPreferredBrand("SkullCandy"), "SkullCandy should be a preferred brand.");
9293
assertTrue(BrandsArray.isPreferredBrand("Juicy"), "Juicy should be a preferred brand.");
9394
}
95+
96+
@Test
97+
public void testwirelessConnection() throws Exception {
98+
// Arrange
99+
headphones.isPoweredOn();
100+
headphones.isWireless();
101+
// Act
102+
headphones.connectToBluetooth();
103+
// Assert
104+
assertTrue(headphones.isConnectedToBluetooth(), "Headphones should connect to bluetooh when wireless and turned on.");
105+
}
106+
107+
@Test
108+
public void testwirelessConnection_connectionNotFound() throws Exception {
109+
// Act
110+
assertThatThrownBy(() -> headphones.wirelessConnection())
111+
.isInstanceOf(ConnectionNotFoundException.class)
112+
.hasMessage("Headphones Wireless Connection Not Found.");
113+
}
94114
}
115+

0 commit comments

Comments
 (0)