Skip to content

feat: adds computer class with unit test for each class #507

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 3 commits into from
Apr 11, 2025
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,94 @@
package com.codedifferently.lesson16.personcomputer;

import java.util.ArrayList;
import java.util.List;

public class Computer {
public enum ComputerStatus {
OFFLINE,
ONLINE,
SLEEP,
SHUTDOWN
}

private String brand;
private String model;
private String cpu;
private int ramGB;
private int storageGB;
private boolean isPoweredOn;
private List<String> installedPrograms;
private ComputerStatus status; // use the enum here

public Computer(
String brand, String model, String cpu, int ramGB, int storageGB, String installedPrograms) {
this.brand = brand;
this.model = model;
this.cpu = cpu;
this.ramGB = ramGB;
this.storageGB = storageGB;
this.isPoweredOn = false;
this.installedPrograms = new ArrayList<>();
this.status = ComputerStatus.OFFLINE;
}

public class ComputerAlreadyOnException extends Exception {
public ComputerAlreadyOnException(String message) {
super(message);
}
}

public void powerOn() throws ComputerAlreadyOnException {
if (!isPoweredOn) {
isPoweredOn = true;
status = ComputerStatus.ONLINE;
} else {
throw new ComputerAlreadyOnException("Computer is already powered on.");
}
}

public String getBrand() {
return brand;
}

public String getModel() {
return model;
}

public String getCPU() {
return cpu;
}

public int getRAMGB() {
return ramGB;
}

public int getSTORAGEGB() {
return storageGB;
}

public boolean isPoweredOn() {
return isPoweredOn;
}

public ComputerStatus getStatus() {
return status;
}

public void listPrograms() {
System.out.println("Installed Programs:");
for (String program : installedPrograms) {
System.out.println("- " + program);
}
}

public void installProgram(String programName) {
if (!installedPrograms.contains(programName)) {
installedPrograms.add(programName);
}
}

public List<String> getInstalledPrograms() {
return installedPrograms;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.codedifferently.lesson16.personcomputer;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class ComputerAlreadyOnExceptionTest {

@Test
void testExceptionMessage() {
String message = "Computer is already powered on.";
Computer.ComputerAlreadyOnException exception =
new Computer(message, message, message, 0, 0, message)
.new ComputerAlreadyOnException(message);
assertEquals(message, exception.getMessage());
}

@Test
void testPowerOnThrowsException() throws Exception {
Computer computer = new Computer("Dell", "XPS", "Intel", 8, 256, "");
computer.powerOn();

Exception exception =
assertThrows(Computer.ComputerAlreadyOnException.class, computer::powerOn);

assertEquals("Computer is already powered on.", exception.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.codedifferently.lesson16.personcomputer;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class ComputerStatusTest {

@Test
void testEnumValues() {
Computer.ComputerStatus[] statuses = Computer.ComputerStatus.values();
assertEquals(4, statuses.length);
assertArrayEquals(
new Computer.ComputerStatus[] {
Computer.ComputerStatus.OFFLINE,
Computer.ComputerStatus.ONLINE,
Computer.ComputerStatus.SLEEP,
Computer.ComputerStatus.SHUTDOWN
},
statuses);
}

@Test
void testValueOf() {
assertEquals(Computer.ComputerStatus.ONLINE, Computer.ComputerStatus.valueOf("ONLINE"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.codedifferently.lesson16.personcomputer;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ComputerTest {

private Computer computer;

@BeforeEach
public void setUp() {
computer = new Computer("Dell", "Inspiron 15", "Intel Core i7", 16, 512, "Windows 11");
}

@Test
public void testPowerOn() throws Computer.ComputerAlreadyOnException {
assertFalse(computer.isPoweredOn());

computer.powerOn();

assertTrue(computer.isPoweredOn());
assertEquals(Computer.ComputerStatus.ONLINE, computer.getStatus());
}

@Test
public void testInstallProgram() {
// Install a new program
computer.installProgram("Windows 11");

assertTrue(computer.getInstalledPrograms().contains("Windows 11"));
}

@Test
public void testDuplicateProgramNotInstalledTwice() {
// Install a program
computer.installProgram("Google Chrome");

// Try installing the same program again
computer.installProgram("Google Chrome");

// Verify the program is only installed once
assertEquals(1, computer.getInstalledPrograms().size());
}

@Test
public void testGetters() {
// Verify that the getters return correct values
assertEquals("Dell", computer.getBrand());
assertEquals("Inspiron 15", computer.getModel());
assertEquals("Intel Core i7", computer.getCPU());
assertEquals(16, computer.getRAMGB());
assertEquals(512, computer.getSTORAGEGB());
}
}