Skip to content

Commit 776c078

Browse files
committed
lesson 16 homework mark 1
1 parent dbd5296 commit 776c078

File tree

2 files changed

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

2 files changed

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

0 commit comments

Comments
 (0)