Skip to content

feat: add computer class with test #501

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

Closed
wants to merge 4 commits into from
Closed
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.PersonalComputer;

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,89 @@
package com.codedifferently.lesson16.PersonalComputer;

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

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
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", "XPS 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 testPowerOnAlreadyOn() {
try {
computer.powerOn();
} catch (Computer.ComputerAlreadyOnException e) {
fail("Exception should not be thrown");
}

assertThrows(Computer.ComputerAlreadyOnException.class, () -> computer.powerOn());
}

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

assertTrue(computer.getInstalledPrograms().contains("Microsoft Word"));
}

@Test
public void testInstallProgramAlreadyInstalled() {
// 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 testListPrograms() {
// Capture the printed output
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);
System.setOut(printStream);

// Install some programs
computer.installProgram("Adobe Photoshop");
computer.installProgram("Visual Studio Code");

// List the installed programs
computer.listPrograms();

// Check if the correct program names are printed
String output = outputStream.toString();
assertTrue(output.contains("Adobe Photoshop"));
assertTrue(output.contains("Visual Studio Code"));
}

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