Skip to content

feat:Adds Dasia's SmartPhone Object and Test to Lesson_16 #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 19, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class BatteryPercentageCustomException extends Exception {
public BatteryPercentageCustomException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codedifferently.lesson16.SmartPhone;

public enum Name {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BrandName would be better.

APPLE,
SAMSUNG,
ANDROID
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.codedifferently.lesson16.SmartPhone;

import java.util.ArrayList;

public class SmartPhone {

private Name brandName;
private String model;
private int batteryPercentage;
private boolean power;
private ArrayList<String> apps;
private int volume;

public SmartPhone(
Name brandName, String model, int batteryPercentage, boolean power, ArrayList<String> apps) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be List<String> apps so that clients can pass in any implementation of a List that works for them. This meets the spirit of the dependency inversion principle.

this.brandName = brandName;
this.batteryPercentage = batteryPercentage;
this.power = power;
this.apps = apps;
this.volume = volume;
}

public void setSmartPhonePowerbattery(int currentBatteryPercentage) {
if (currentBatteryPercentage < 0 || currentBatteryPercentage > 100) {
throw new IllegalArgumentException("Battery percentage must be in between 0 and 100.");
}
this.batteryPercentage = currentBatteryPercentage;
}

public int getBatteryPercentage() {
return batteryPercentage;
}

public String getSmartPhoneApps() {
var smartPhoneApps = "";
for (String s : apps) {
smartPhoneApps += s;
}
return smartPhoneApps;
}

public void removeSmartPhoneApps(String App) {
apps.remove(App);
}

public int getSmartPhoneVolume() {
return 5;
}

public boolean isPowerOn() {
return true;
}

public void addSmartPhoneApps(String instagram) {
apps.add("," + instagram);
}

public void addSmartPhoneVolume() {
volume++;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.codedifferently.lesson16.SmartPhone.Name;
import com.codedifferently.lesson16.SmartPhone.SmartPhone;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class SmartPhoneTest {
private SmartPhone smartPhone;

@BeforeEach
void BeforeTest() {
smartPhone =
new SmartPhone(
Name.APPLE,
"Iphone14",
50,
true,
new ArrayList<>(
Arrays.asList("Phone,", "Contacts,", "Photos,", "Settings,", "Calender")));
// Turn phone on
}

@Test
void testSmartPhoneCharge() {
// Arrange

// Act
smartPhone.setSmartPhonePowerbattery(
55); // Method with a loop to increase the battery percentage
// Assert
assertEquals(smartPhone.getBatteryPercentage(), 55);
}

@Test
void testSmartPhoneApps() {
// Arrange
var smartPhoneApps = "Instagram";
// Act
smartPhone.addSmartPhoneApps("Instagram"); // Method Add an app to my phone
// Assert
assertEquals(
smartPhone.getSmartPhoneApps(), "Phone,Contacts,Photos,Settings,Calender,Instagram");
}

@Test
void testSmartPhoneRemoveApps() {
// Arrange
smartPhone.addSmartPhoneApps("Instagram");
// Act
smartPhone.removeSmartPhoneApps(",Instagram"); // Method that removes the app from my phone
// Assert
assertEquals(
smartPhone.getSmartPhoneApps(),
"Phone,Contacts,Photos,Settings,Calender"); // Method that gets the apps on my phone
}

@Test
void testSmartPhoneVolume() {
// Arrange
var smartPhoneOldVolume = 2;
smartPhone.getSmartPhoneVolume(); // Method current/Old Volume of phone

// Act
smartPhone.addSmartPhoneVolume(); // Method add Volume to smart phone

// Assert
assertNotEquals(smartPhone.getSmartPhoneVolume(), smartPhoneOldVolume);
}

@Test
void testSmartPhonePowerStatus() {
// Act
boolean isPhoneOn = smartPhone.isPowerOn(); // Method to check if the phone is on

// Assert
assertTrue(isPhoneOn); // Checks that the phone is indeed on
}
}