diff --git a/src/main/java/object/jaydenrandrews/Phone.java b/src/main/java/object/jaydenrandrews/Phone.java new file mode 100644 index 0000000..95d85ca --- /dev/null +++ b/src/main/java/object/jaydenrandrews/Phone.java @@ -0,0 +1,74 @@ +package object.jaydenrandrews; + +public class Phone { + private String brand; + private String model; + private int storageGB; + private boolean isOn; + private int batteryLife; + + public Phone(String brand, String model, int storageGB, int batteryLife) { + if (storageGB <= 0) { + throw new IllegalArgumentException("Storage must be positive"); + } + if (batteryLife < 0 || batteryLife > 100) { + throw new IllegalArgumentException("Battery must be between 0 and 100"); + } + + this.brand = brand; + this.model = model; + this.storageGB = storageGB; + this.isOn = false; + this.batteryLife = batteryLife; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public boolean isOn() { + return isOn; + } + + public void powerOn() { + this.isOn = true; + } + + public void powerOff() { + this.isOn = false; + } + + public int getStorageGB() { + return storageGB; + } + + public void setStorageGB(int storageGB) { + this.storageGB = storageGB; + } + + public void charge(int amount) { + int newCharge = amount + this.getBatteryLife(); + this.batteryLife = Math.min(newCharge, 100); + } + + public void useBattery(int amount) { + int newCharge = this.getBatteryLife() - amount; + this.batteryLife = Math.max(0, newCharge); + } + + public int getBatteryLife() { + return batteryLife; + } +} diff --git a/src/test/java/object/jaydenandrews/PhoneTest.java b/src/test/java/object/jaydenandrews/PhoneTest.java new file mode 100644 index 0000000..6f530cf --- /dev/null +++ b/src/test/java/object/jaydenandrews/PhoneTest.java @@ -0,0 +1,148 @@ +package object.jaydenandrews; + +import object.jaydenrandrews.Phone; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class PhoneTest { + @Test + public void constructor_HappyPath_ShouldInitializeFieldsCorrectly() { + // Arrange + String brand = "Apple"; + String model = "iPhone 14"; + int storageGB = 128; + int batteryLife = 75; + + // Act + Phone phone = new Phone(brand, model, storageGB, batteryLife); + + // Assert + assertEquals(brand, phone.getBrand()); + assertEquals(model, phone.getModel()); + assertEquals(storageGB, phone.getStorageGB()); + assertEquals(batteryLife, phone.getBatteryLife()); + } + + @Test + public void assertInvalidStorage() { + // Act & Assert + assertThrows(IllegalArgumentException.class, () -> { new Phone("Samsung", "Galaxy S21", -1, 100); + }); + } + + @Test + public void assertInvalidBatteryLifeLessThan() { + // Arrange + String brand = "Apple"; + String model = "iPhone 14"; + int storageGB = -1; + int batteryLife = 75; + boolean isOn = false; + + // Act & Assert + assertThrows(IllegalArgumentException.class, () -> { new Phone("Samsung", "Galaxy S21", 128, -1); + }); + } + + @Test + public void assertInvalidBatteryGreaterThan() { + // Arrange + String brand = "Apple"; + String model = "iPhone 14"; + int storageGB = -1; + int batteryLife = 75; + boolean isOn = false; + + // Act & Assert + assertThrows(IllegalArgumentException.class, () -> { new Phone("Samsung", "Galaxy S21", 128, 101); + }); + } + + @Test + public void assertSettersWorkCorrectly() { + // Arrange + String brand = "Apple"; + String model = "iPhone 14"; + int storageGB = 128; + int batteryLife = 75; + + // Act + Phone phone = new Phone(brand, model, storageGB, batteryLife); + phone.setBrand("Samsung"); + phone.setStorageGB(256); + phone.setModel("Galaxy S20"); + + // Assert + assertEquals("Samsung", phone.getBrand()); + assertEquals("Galaxy S20", phone.getModel()); + assertEquals(256, phone.getStorageGB()); + } + + @Test + public void assertPowerOnWorksCorrectly() { + // Arrange + String brand = "Apple"; + String model = "iPhone 14"; + int storageGB = 128; + int batteryLife = 75; + + // Act + Phone phone = new Phone(brand, model, storageGB, batteryLife); + phone.powerOn(); + + // Assert + assertTrue(phone.isOn()); + } + + @Test + public void assertPowerOffWorksCorrectly() { + // Arrange + String brand = "Apple"; + String model = "iPhone 14"; + int storageGB = 128; + int batteryLife = 75; + + // Act + Phone phone = new Phone(brand, model, storageGB, batteryLife); + phone.powerOn(); + phone.powerOff(); + + // Assert + assertFalse(phone.isOn()); + } + + @Test + public void assertChargeWorksCorrectly() { + // Arrange + String brand = "Apple"; + String model = "iPhone 14"; + int storageGB = 128; + int batteryLife = 75; + + // Act & Assert + Phone phone = new Phone(brand, model, storageGB, batteryLife); + phone.charge(24); + + assertEquals(99, phone.getBatteryLife()); + phone.charge(24); + assertEquals(100, phone.getBatteryLife()); + } + + @Test + public void assertUseBatteryLifeWorksCorrectly() { + // Arrange + String brand = "Apple"; + String model = "iPhone 14"; + int storageGB = 128; + int batteryLife = 75; + + // Act & Assert + Phone phone = new Phone(brand, model, storageGB, batteryLife); + phone.useBattery(25); + + assertEquals(50, phone.getBatteryLife()); + phone.useBattery(100); + assertEquals(0, phone.getBatteryLife()); + } +}