Skip to content

Commit 725c15b

Browse files
authored
feat: Adds Dasia's SmartPhone Object and Test to Lesson_16 (#523)
* feat:Adds Dasia's SmartPhone Object and Test to Lesson_16 * fix: deleted methods that I was not using in my test * fix: test that I was not using
1 parent cef7aa2 commit 725c15b

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class BatteryPercentageCustomException extends Exception {
2+
public BatteryPercentageCustomException(String message) {
3+
super(message);
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.SmartPhone;
2+
3+
public enum Name {
4+
APPLE,
5+
SAMSUNG,
6+
ANDROID
7+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.codedifferently.lesson16.SmartPhone;
2+
3+
import java.util.ArrayList;
4+
5+
public class SmartPhone {
6+
7+
private Name brandName;
8+
private String model;
9+
private int batteryPercentage;
10+
private boolean power;
11+
private ArrayList<String> apps;
12+
private int volume;
13+
14+
public SmartPhone(
15+
Name brandName, String model, int batteryPercentage, boolean power, ArrayList<String> apps) {
16+
this.brandName = brandName;
17+
this.batteryPercentage = batteryPercentage;
18+
this.power = power;
19+
this.apps = apps;
20+
this.volume = volume;
21+
}
22+
23+
public void setSmartPhonePowerbattery(int currentBatteryPercentage) {
24+
if (currentBatteryPercentage < 0 || currentBatteryPercentage > 100) {
25+
throw new IllegalArgumentException("Battery percentage must be in between 0 and 100.");
26+
}
27+
this.batteryPercentage = currentBatteryPercentage;
28+
}
29+
30+
public int getBatteryPercentage() {
31+
return batteryPercentage;
32+
}
33+
34+
public String getSmartPhoneApps() {
35+
var smartPhoneApps = "";
36+
for (String s : apps) {
37+
smartPhoneApps += s;
38+
}
39+
return smartPhoneApps;
40+
}
41+
42+
public void removeSmartPhoneApps(String App) {
43+
apps.remove(App);
44+
}
45+
46+
public int getSmartPhoneVolume() {
47+
return 5;
48+
}
49+
50+
public boolean isPowerOn() {
51+
return true;
52+
}
53+
54+
public void addSmartPhoneApps(String instagram) {
55+
apps.add("," + instagram);
56+
}
57+
58+
public void addSmartPhoneVolume() {
59+
volume++;
60+
}
61+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import com.codedifferently.lesson16.SmartPhone.Name;
6+
import com.codedifferently.lesson16.SmartPhone.SmartPhone;
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
12+
public class SmartPhoneTest {
13+
private SmartPhone smartPhone;
14+
15+
@BeforeEach
16+
void BeforeTest() {
17+
smartPhone =
18+
new SmartPhone(
19+
Name.APPLE,
20+
"Iphone14",
21+
50,
22+
true,
23+
new ArrayList<>(
24+
Arrays.asList("Phone,", "Contacts,", "Photos,", "Settings,", "Calender")));
25+
// Turn phone on
26+
}
27+
28+
@Test
29+
void testSmartPhoneCharge() {
30+
// Arrange
31+
32+
// Act
33+
smartPhone.setSmartPhonePowerbattery(
34+
55); // Method with a loop to increase the battery percentage
35+
// Assert
36+
assertEquals(smartPhone.getBatteryPercentage(), 55);
37+
}
38+
39+
@Test
40+
void testSmartPhoneApps() {
41+
// Arrange
42+
var smartPhoneApps = "Instagram";
43+
// Act
44+
smartPhone.addSmartPhoneApps("Instagram"); // Method Add an app to my phone
45+
// Assert
46+
assertEquals(
47+
smartPhone.getSmartPhoneApps(), "Phone,Contacts,Photos,Settings,Calender,Instagram");
48+
}
49+
50+
@Test
51+
void testSmartPhoneRemoveApps() {
52+
// Arrange
53+
smartPhone.addSmartPhoneApps("Instagram");
54+
// Act
55+
smartPhone.removeSmartPhoneApps(",Instagram"); // Method that removes the app from my phone
56+
// Assert
57+
assertEquals(
58+
smartPhone.getSmartPhoneApps(),
59+
"Phone,Contacts,Photos,Settings,Calender"); // Method that gets the apps on my phone
60+
}
61+
62+
@Test
63+
void testSmartPhoneVolume() {
64+
// Arrange
65+
var smartPhoneOldVolume = 2;
66+
smartPhone.getSmartPhoneVolume(); // Method current/Old Volume of phone
67+
68+
// Act
69+
smartPhone.addSmartPhoneVolume(); // Method add Volume to smart phone
70+
71+
// Assert
72+
assertNotEquals(smartPhone.getSmartPhoneVolume(), smartPhoneOldVolume);
73+
}
74+
75+
@Test
76+
void testSmartPhonePowerStatus() {
77+
// Act
78+
boolean isPhoneOn = smartPhone.isPowerOn(); // Method to check if the phone is on
79+
80+
// Assert
81+
assertTrue(isPhoneOn); // Checks that the phone is indeed on
82+
}
83+
}

0 commit comments

Comments
 (0)