Skip to content

Commit 6c5d278

Browse files
authored
feat: adds computer class with unit test for each class (#507)
* lesson 16 homework mark0 * lesson 16 homework mark1
1 parent 9762cfd commit 6c5d278

File tree

4 files changed

+208
-0
lines changed

4 files changed

+208
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.codedifferently.lesson16.personcomputer;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Computer {
7+
public enum ComputerStatus {
8+
OFFLINE,
9+
ONLINE,
10+
SLEEP,
11+
SHUTDOWN
12+
}
13+
14+
private String brand;
15+
private String model;
16+
private String cpu;
17+
private int ramGB;
18+
private int storageGB;
19+
private boolean isPoweredOn;
20+
private List<String> installedPrograms;
21+
private ComputerStatus status; // use the enum here
22+
23+
public Computer(
24+
String brand, String model, String cpu, int ramGB, int storageGB, String installedPrograms) {
25+
this.brand = brand;
26+
this.model = model;
27+
this.cpu = cpu;
28+
this.ramGB = ramGB;
29+
this.storageGB = storageGB;
30+
this.isPoweredOn = false;
31+
this.installedPrograms = new ArrayList<>();
32+
this.status = ComputerStatus.OFFLINE;
33+
}
34+
35+
public class ComputerAlreadyOnException extends Exception {
36+
public ComputerAlreadyOnException(String message) {
37+
super(message);
38+
}
39+
}
40+
41+
public void powerOn() throws ComputerAlreadyOnException {
42+
if (!isPoweredOn) {
43+
isPoweredOn = true;
44+
status = ComputerStatus.ONLINE;
45+
} else {
46+
throw new ComputerAlreadyOnException("Computer is already powered on.");
47+
}
48+
}
49+
50+
public String getBrand() {
51+
return brand;
52+
}
53+
54+
public String getModel() {
55+
return model;
56+
}
57+
58+
public String getCPU() {
59+
return cpu;
60+
}
61+
62+
public int getRAMGB() {
63+
return ramGB;
64+
}
65+
66+
public int getSTORAGEGB() {
67+
return storageGB;
68+
}
69+
70+
public boolean isPoweredOn() {
71+
return isPoweredOn;
72+
}
73+
74+
public ComputerStatus getStatus() {
75+
return status;
76+
}
77+
78+
public void listPrograms() {
79+
System.out.println("Installed Programs:");
80+
for (String program : installedPrograms) {
81+
System.out.println("- " + program);
82+
}
83+
}
84+
85+
public void installProgram(String programName) {
86+
if (!installedPrograms.contains(programName)) {
87+
installedPrograms.add(programName);
88+
}
89+
}
90+
91+
public List<String> getInstalledPrograms() {
92+
return installedPrograms;
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.codedifferently.lesson16.personcomputer;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class ComputerAlreadyOnExceptionTest {
8+
9+
@Test
10+
void testExceptionMessage() {
11+
String message = "Computer is already powered on.";
12+
Computer.ComputerAlreadyOnException exception =
13+
new Computer(message, message, message, 0, 0, message)
14+
.new ComputerAlreadyOnException(message);
15+
assertEquals(message, exception.getMessage());
16+
}
17+
18+
@Test
19+
void testPowerOnThrowsException() throws Exception {
20+
Computer computer = new Computer("Dell", "XPS", "Intel", 8, 256, "");
21+
computer.powerOn();
22+
23+
Exception exception =
24+
assertThrows(Computer.ComputerAlreadyOnException.class, computer::powerOn);
25+
26+
assertEquals("Computer is already powered on.", exception.getMessage());
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.codedifferently.lesson16.personcomputer;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class ComputerStatusTest {
9+
10+
@Test
11+
void testEnumValues() {
12+
Computer.ComputerStatus[] statuses = Computer.ComputerStatus.values();
13+
assertEquals(4, statuses.length);
14+
assertArrayEquals(
15+
new Computer.ComputerStatus[] {
16+
Computer.ComputerStatus.OFFLINE,
17+
Computer.ComputerStatus.ONLINE,
18+
Computer.ComputerStatus.SLEEP,
19+
Computer.ComputerStatus.SHUTDOWN
20+
},
21+
statuses);
22+
}
23+
24+
@Test
25+
void testValueOf() {
26+
assertEquals(Computer.ComputerStatus.ONLINE, Computer.ComputerStatus.valueOf("ONLINE"));
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.codedifferently.lesson16.personcomputer;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class ComputerTest {
11+
12+
private Computer computer;
13+
14+
@BeforeEach
15+
public void setUp() {
16+
computer = new Computer("Dell", "Inspiron 15", "Intel Core i7", 16, 512, "Windows 11");
17+
}
18+
19+
@Test
20+
public void testPowerOn() throws Computer.ComputerAlreadyOnException {
21+
assertFalse(computer.isPoweredOn());
22+
23+
computer.powerOn();
24+
25+
assertTrue(computer.isPoweredOn());
26+
assertEquals(Computer.ComputerStatus.ONLINE, computer.getStatus());
27+
}
28+
29+
@Test
30+
public void testInstallProgram() {
31+
// Install a new program
32+
computer.installProgram("Windows 11");
33+
34+
assertTrue(computer.getInstalledPrograms().contains("Windows 11"));
35+
}
36+
37+
@Test
38+
public void testDuplicateProgramNotInstalledTwice() {
39+
// Install a program
40+
computer.installProgram("Google Chrome");
41+
42+
// Try installing the same program again
43+
computer.installProgram("Google Chrome");
44+
45+
// Verify the program is only installed once
46+
assertEquals(1, computer.getInstalledPrograms().size());
47+
}
48+
49+
@Test
50+
public void testGetters() {
51+
// Verify that the getters return correct values
52+
assertEquals("Dell", computer.getBrand());
53+
assertEquals("Inspiron 15", computer.getModel());
54+
assertEquals("Intel Core i7", computer.getCPU());
55+
assertEquals(16, computer.getRAMGB());
56+
assertEquals(512, computer.getSTORAGEGB());
57+
}
58+
}

0 commit comments

Comments
 (0)