Skip to content

Commit 724b0ec

Browse files
committed
lesson 16 homework mark0
1 parent dbd5296 commit 724b0ec

File tree

2 files changed

+188
-0
lines changed
  • lesson_16/objects/objects_app/src

2 files changed

+188
-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.PersonalComputer;
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,94 @@
1+
package com.codedifferently.lesson16.PersonalComputerTest;
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.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
import static org.junit.jupiter.api.Assertions.fail;
8+
9+
import com.codedifferently.lesson16.PersonalComputer.Computer;
10+
import java.io.ByteArrayOutputStream;
11+
import java.io.PrintStream;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
public class ComputerTest {
16+
17+
private Computer computer;
18+
19+
@BeforeEach
20+
public void setUp() {
21+
computer = new Computer("Dell", "XPS 15", "Intel Core i7", 16, 512, "Windows 11");
22+
}
23+
24+
@Test
25+
public void testPowerOn() throws Computer.ComputerAlreadyOnException {
26+
assertFalse(computer.isPoweredOn());
27+
28+
computer.powerOn();
29+
30+
assertTrue(computer.isPoweredOn());
31+
assertEquals(Computer.ComputerStatus.ONLINE, computer.getStatus());
32+
}
33+
34+
@Test
35+
public void testPowerOnAlreadyOn() {
36+
try {
37+
computer.powerOn();
38+
} catch (Computer.ComputerAlreadyOnException e) {
39+
fail("Exception should not be thrown");
40+
}
41+
42+
assertThrows(Computer.ComputerAlreadyOnException.class, () -> computer.powerOn());
43+
}
44+
45+
@Test
46+
public void testInstallProgram() {
47+
// Install a new program
48+
computer.installProgram("Microsoft Word");
49+
50+
assertTrue(computer.getInstalledPrograms().contains("Microsoft Word"));
51+
}
52+
53+
@Test
54+
public void testInstallProgramAlreadyInstalled() {
55+
// Install a program
56+
computer.installProgram("Google Chrome");
57+
58+
// Try installing the same program again
59+
computer.installProgram("Google Chrome");
60+
61+
// Verify the program is only installed once
62+
assertEquals(1, computer.getInstalledPrograms().size());
63+
}
64+
65+
@Test
66+
public void testListPrograms() {
67+
// Capture the printed output
68+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
69+
PrintStream printStream = new PrintStream(outputStream);
70+
System.setOut(printStream);
71+
72+
// Install some programs
73+
computer.installProgram("Adobe Photoshop");
74+
computer.installProgram("Visual Studio Code");
75+
76+
// List the installed programs
77+
computer.listPrograms();
78+
79+
// Check if the correct program names are printed
80+
String output = outputStream.toString();
81+
assertTrue(output.contains("Adobe Photoshop"));
82+
assertTrue(output.contains("Visual Studio Code"));
83+
}
84+
85+
@Test
86+
public void testGetters() {
87+
// Verify that the getters return correct values
88+
assertEquals("Dell", computer.getBrand());
89+
assertEquals("XPS 15", computer.getModel());
90+
assertEquals("Intel Core i7", computer.getCPU());
91+
assertEquals(16, computer.getRAMGB());
92+
assertEquals(512, computer.getSTORAGEGB());
93+
}
94+
}

0 commit comments

Comments
 (0)