diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/KimberleeObject/ConnectionNotFoundException.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/KimberleeObject/ConnectionNotFoundException.java new file mode 100644 index 000000000..4c0fb30e6 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/KimberleeObject/ConnectionNotFoundException.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson16.KimberleeObject; + +public class ConnectionNotFoundException extends Exception { + public ConnectionNotFoundException(String message) { + super(message); + } +} diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/KimberleeObject/HeadPhones.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/KimberleeObject/HeadPhones.java new file mode 100644 index 000000000..d507c363b --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/KimberleeObject/HeadPhones.java @@ -0,0 +1,134 @@ +package com.codedifferently.lesson16.KimberleeObject; + +import java.util.Arrays; + +public class HeadPhones { + + private int volume = 0; + private boolean isPoweredOn = false; + private HeadPhoneColor headPhoneColor = HeadPhoneColor.BLACK; + private boolean isWireless = true; + private String brands = "Beats"; + boolean isPreferredBrand = BrandUtils.isPreferredBrand(brands); + private boolean isConnectedToBluetooth = false; + + public enum BoostMode { + BASS_BOOST, + VOCAL_BOOST, + TREBLE_BOOST; + } + + private BoostMode currentMode; + private BoostMode[] modes = BoostMode.values(); + private int currentModeIndex = 0; + + public HeadPhones() { + this.currentMode = BoostMode.BASS_BOOST; + System.out.println("Constructor called: currentMode set to " + currentMode); + } + + public void nextBoostMode() { + currentModeIndex = (currentModeIndex + 1) % modes.length; + currentMode = modes[currentModeIndex]; + } + + public BoostMode getCurrentMode() { + System.out.println("getCurrentMode called: currentMode is " + currentMode); + return currentMode; + } + + public enum HeadPhoneColor { + RED, + BLUE, + ROSEGOLD, + PINK, + WHITE, + BLACK; + } + + public void BrandsArray() { + String[] brands = new String[5]; + brands[0] = "Beats"; + brands[1] = "Sony"; + brands[2] = "Bose"; + brands[3] = "SkullCandy"; + brands[4] = "Juicy"; + } + + public int getVolume() { + return volume; + } + + public HeadPhoneColor getHeadPhoneColor() { + return headPhoneColor; + } + + public boolean isPoweredOn() { + return isPoweredOn; + } + + public boolean isWireless() { + return isWireless; + } + + public String[] getBrandsArray() { + return BrandUtils.PREFERRED_BRANDS; + } + + public void turnOn() { + isPoweredOn = true; + } + + public void turnOff() { + isPoweredOn = false; + } + + public void increaseVolume() { + if (volume < 100) { + volume++; + } + } + + public void setVolume(int volume) { + if (volume >= 0 && volume <= 100) { + this.volume = volume; + } + } + + public void decreaseVolume() { + if (volume > 0) { + volume--; + } + } + + public void setColor(HeadPhoneColor color) { + this.headPhoneColor = color; + } + + public class BrandUtils { + private static final String[] PREFERRED_BRANDS = { + "Beats", "Sony", "Bose", "SkullCandy", "Juicy" + }; + + public static boolean isPreferredBrand(String brand) { + return Arrays.asList(PREFERRED_BRANDS).contains(brand); + } + } + + public boolean connectToBluetooth() { + if (isPoweredOn && isWireless) { + isConnectedToBluetooth = true; + } + return isConnectedToBluetooth; + } + + public boolean isConnectedToBluetooth() { + return connectToBluetooth(); + } + + public void wirelessConnection() throws ConnectionNotFoundException { + if (!isConnectedToBluetooth) { + throw new ConnectionNotFoundException("Headphones Wireless Connection Not Found."); + } + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/KimberleeObjectTest/HeadPhonesTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/KimberleeObjectTest/HeadPhonesTest.java new file mode 100644 index 000000000..9b1a71ae5 --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/KimberleeObjectTest/HeadPhonesTest.java @@ -0,0 +1,151 @@ +package com.codedifferently.lesson16.KimberleeObjectTest; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.*; + +import com.codedifferently.lesson16.KimberleeObject.ConnectionNotFoundException; +import com.codedifferently.lesson16.KimberleeObject.HeadPhones; +import com.codedifferently.lesson16.KimberleeObject.HeadPhones.BoostMode; +import com.codedifferently.lesson16.KimberleeObject.HeadPhones.HeadPhoneColor; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class HeadPhonesTest { + + private HeadPhones headphones; + + @BeforeEach + public void setUp() { + headphones = new HeadPhones(); + } + + @Test + public void testDefaultState() { + assertEquals(0, headphones.getVolume(), "Volume should be 0 by default."); + assertEquals( + HeadPhoneColor.BLACK, headphones.getHeadPhoneColor(), "Color should be black by default."); + assertFalse(headphones.isPoweredOn(), "HeadPhones should be off by default."); + assertTrue(headphones.isWireless(), "HeadPhones should be wireless by default."); + assertArrayEquals(new String[] {"Beats"}, new String[] {"Beats"}); + assertEquals( + BoostMode.BASS_BOOST, + headphones.getCurrentMode(), + "BASS_BOOST should be the default setting."); + } + + @Test + public void testTurnOn() { + // Act + headphones.turnOn(); + // Assert + assertTrue(headphones.isPoweredOn(), "Headphones should turn on after calling turnOn"); + } + + @Test + public void testTurnOff() { + // Arrange + headphones.turnOn(); + // Act + headphones.turnOff(); + // Assert + assertFalse(headphones.isPoweredOn(), "Headphones should turn off after calling"); + } + + @Test + public void testIncreaseVolume() { + // Arrange + headphones.turnOn(); + headphones.increaseVolume(); + assertEquals(1, headphones.getVolume(), "Volume should increase by 1"); + // Act + headphones.setVolume(99); + headphones.increaseVolume(); + // Assert + assertEquals(100, headphones.getVolume(), "Volume should not exceed 100."); + } + + @Test + public void testDecreaseVolume() { + // Arrange + headphones.turnOn(); + headphones.setVolume(1); + headphones.decreaseVolume(); + assertEquals(0, headphones.getVolume(), "Volume should decrease to 0."); + // Act + headphones.decreaseVolume(); + // Assert + assertEquals(0, headphones.getVolume(), "Volume should not go lower than 0."); + } + + @Test + public void testSetColor() { + // Act + headphones.setColor(HeadPhoneColor.ROSEGOLD); + // Assert + assertEquals( + HeadPhoneColor.ROSEGOLD, + headphones.getHeadPhoneColor(), + "Headphone color should be set to Rose Gold."); + } + + @Test + public void testPreferredBrand() { + // Assert + assertTrue( + HeadPhones.BrandUtils.isPreferredBrand("Beats"), "Beats should be a preferred brand."); + assertTrue(HeadPhones.BrandUtils.isPreferredBrand("Sony"), "Sony should be a preferred brand."); + assertFalse( + HeadPhones.BrandUtils.isPreferredBrand("Apple"), "Apple should not be a preferred brand."); + assertTrue( + HeadPhones.BrandUtils.isPreferredBrand("SkullCandy"), + "SkullCandy should be a preferred brand."); + assertTrue( + HeadPhones.BrandUtils.isPreferredBrand("Juicy"), "Juicy should be a preferred brand."); + } + + @Test + public void testwirelessConnection() throws Exception { + // Arrange + headphones.turnOn(); + // Act + headphones.connectToBluetooth(); + // Assert + assertTrue( + headphones.isConnectedToBluetooth(), + "Headphones should connect to bluetooh when wireless and turned on."); + } + + @Test + public void testwirelessConnection_connectionNotFound() throws Exception { + // Act + assertThatThrownBy(() -> headphones.wirelessConnection()) + .isInstanceOf(ConnectionNotFoundException.class) + .hasMessage("Headphones Wireless Connection Not Found."); + } + + @Test + public void testBoostMode() { + assertEquals( + BoostMode.BASS_BOOST, + headphones.getCurrentMode(), + "BASS_BOOST should be the default setting."); + + headphones.nextBoostMode(); + assertEquals( + BoostMode.VOCAL_BOOST, + headphones.getCurrentMode(), + "The next Boost setting should be VOCAL."); + + headphones.nextBoostMode(); + assertEquals( + BoostMode.TREBLE_BOOST, + headphones.getCurrentMode(), + "The next Boost setting should be TREBLE."); + + headphones.nextBoostMode(); + assertEquals( + BoostMode.BASS_BOOST, + headphones.getCurrentMode(), + "BoostMode cycle should return to BASS_BOOST."); + } +}