Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/main/java/object/jaydenrandrews/Phone.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
148 changes: 148 additions & 0 deletions src/test/java/object/jaydenandrews/PhoneTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}