Skip to content
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

FEAT: Added Parts, Cpu, and Gpu classes, added custom exception with enum java files #206

Merged
merged 7 commits into from
Mar 19, 2024
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,64 @@
package com.codedifferently.lesson7.mohamedobjects.main;

// CPU is a child class of Part this means that CPU inherits name, releaseYear, and brand
public class CPU extends Part {

// initialization of member variables for CPU
private double clockSpeed;
private int cores;

// A constructor that takes in brand.
public CPU(cpuBrand brand) {
this.brand = brand.toString();
}

// Constructor for GPU that needs name, releaseYear, brand, and clockSpeed
public CPU(String name, int releaseYear, cpuBrand brand, double clockSpeed) {
this.name = name;
checkValidYear(releaseYear);
this.releaseYear = releaseYear;
this.brand = brand.toString();
this.clockSpeed = clockSpeed;
}

// Constructor for GPU that needs name, releaseYear, brand, clockSpeed, and cores
public CPU(String name, int releaseYear, cpuBrand brand, double clockSpeed, int cores) {
this.name = name;
checkValidYear(releaseYear);
this.releaseYear = releaseYear;
this.brand = brand.toString();
this.clockSpeed = clockSpeed;
this.cores = cores;
}

// Fuctions that set the value for all the member variables in CPU
public void setClockSpeed(double clockSpeed) {
this.clockSpeed = clockSpeed;
}

public void setCores(int cores) {
this.cores = cores;
}

// Functions that retreve the member variables.
public double getClockSpeed() {
return clockSpeed;
}

public int getCores() {
return cores;
}

// Calculates the performance for a CPU by clockSpeed to the power of the core count
public double calcPerformance() {
if (cores == 0 || clockSpeed == 0) {
return 0;
} else {
double performance = clockSpeed;
for (int i = 1; i < cores; i++) {
performance *= clockSpeed;
}
return Math.round(performance);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.codedifferently.lesson7.mohamedobjects.main;

import java.util.HashSet;
import java.util.Set;

// GPU is a child class of Part this means that GPU inherits name, releaseYear, and brand
public class GPU extends Part {

// initialization of member variables for GPU
private Set<portType> ports = new HashSet<portType>();
private int vRam;
private boolean hasRayTracing;

// empty constructor for GPU
public GPU() {}

// Constructor for GPU that needs name, releaseYear, brand, and vRam
public GPU(String name, int releaseYear, String brand, int vRam) {
this.vRam = vRam;
this.name = name;
checkValidYear(releaseYear);
this.releaseYear = releaseYear;
this.brand = brand;
}

// Constructor for GPU that needs name, releaseYear, brand, vRam, and hasRayTracing
public GPU(String name, int releaseYear, String brand, int vRam, boolean hasRayTracing) {
this.vRam = vRam;
this.name = name;
checkValidYear(releaseYear);
this.releaseYear = releaseYear;
this.brand = brand;
this.hasRayTracing = hasRayTracing;
}

// Fuctions that set the value for all the member variables in GPU
public void setvRam(int vRam) {
this.vRam = vRam;
}

public void setHasRayTracing(boolean hasRayTracing) {
this.hasRayTracing = hasRayTracing;
}

// Functions that retreve the member variables.
public int getvRam() {
return vRam;
}

public boolean getHasRayTracing() {
return hasRayTracing;
}

public Set<portType> getPorts() {
return ports;
}

// Adds ports to a set that contains ports
public void addPorts(portType port) {
this.ports.add(port);
}

// A function that returns number of ports
public int calculateNumPorts() {
return ports.size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codedifferently.lesson7.mohamedobjects.main;

public class IllegalYearExeption extends RuntimeException {
IllegalYearExeption(String error) {
super(error);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.codedifferently.lesson7.mohamedobjects.main;

public class Part {
// initialization of member variables for part
String name;
int releaseYear;
String brand;

// empty constructor for Part
public Part() {}

// constructor for Part that requires name, realeaseYear, and brand
public Part(String name, int releaseYear, String brand) {
this.name = name;
checkValidYear(releaseYear);
this.releaseYear = releaseYear;
this.brand = brand;
}

// Fuctions that set the value for all the member variables in Part
public void setBrand(String brand) {
this.brand = brand;
}

public void setName(String name) {
this.name = name;
}

public void setReleaseYear(int releaseYear) {
checkValidYear(releaseYear);
this.releaseYear = releaseYear;
}

// Functions that retreve the member variables.
public String getBrand() {
return brand;
}

public String getName() {
return name;
}

public int getReleaseYear() {
return releaseYear;
}

// A function that checks if the releaseYear is valid
public boolean checkValidYear() {
if (releaseYear <= 2024 && releaseYear >= 2000) {
return true;
} else {
throw new IllegalYearExeption("Year is out of expected range. 2000 - 2024");
}
}

// a function to check if a year was to be entered, is it valid
public boolean checkValidYear(int year) {
if (year <= 2024 && year >= 2000) {
return true;
} else {
throw new IllegalYearExeption("Year is out of expected range. 2000 - 2024");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codedifferently.lesson7.mohamedobjects.main;

public enum cpuBrand {
AMD,
INTEL,
APPLE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.codedifferently.lesson7.mohamedobjects.main;

public enum portType {
VGA,
DVI,
HDMI,
DP
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.codedifferently.lesson7.mohamedobjects.main;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Set;
import org.junit.jupiter.api.Test;

class ComputerPartsTest {

@Test
public void testCpuConstruction() {
CPU cpu = new CPU("M1", 2020, cpuBrand.INTEL, 23.32);

assertEquals(23.32, cpu.getClockSpeed());
assertNotEquals(8, cpu.getCores());

cpu.setCores(8);

assertEquals(8, cpu.getCores());
assertEquals(2020, cpu.getReleaseYear());
assertNotEquals("M2", cpu.getName());
}

@Test
void testGpuConstruction() {
GPU bGpu = new GPU("Radion 560 xt", 2015, "AMD", 8);
Set<portType> porty = Set.of(portType.DP, portType.HDMI, portType.VGA);

bGpu.addPorts(portType.DP);
bGpu.addPorts(portType.HDMI);
bGpu.addPorts(portType.VGA);

assertEquals(porty, bGpu.getPorts());
assertEquals(3, bGpu.calculateNumPorts());
}

@Test
void testYearValadation() {
GPU bGpu = new GPU("Radion 560 xt", 2015, "AMD", 8);

assertThrows(
IllegalYearExeption.class,
() -> {
bGpu.checkValidYear(1999);
});
assertThrows(
IllegalYearExeption.class,
() -> {
bGpu.checkValidYear(3000);
});
}

@Test
void testPartConstruction() {
Part apart = new Part();

apart.setBrand("A Brand");
apart.setName("A Part");
apart.setReleaseYear(2011);

assertEquals("A Brand", apart.getBrand());
assertEquals("A Part", apart.getName());
assertEquals(2011, apart.getReleaseYear());
}

@Test
void testCpuPerformance() {
CPU acpu = new CPU("M1", 2020, cpuBrand.INTEL, 23.32);

assertEquals(0, acpu.calcPerformance());
acpu.setCores(8);

assertEquals(Math.round(87463805373.55), acpu.calcPerformance());
}
}