diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/CPU.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/CPU.java new file mode 100644 index 00000000..7378951b --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/CPU.java @@ -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); + } + } +} diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/GPU.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/GPU.java new file mode 100644 index 00000000..b113e97e --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/GPU.java @@ -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 ports = new HashSet(); + 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 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(); + } +} diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/IllegalYearExeption.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/IllegalYearExeption.java new file mode 100644 index 00000000..250212ad --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/IllegalYearExeption.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson7.mohamedobjects.main; + +public class IllegalYearExeption extends RuntimeException { + IllegalYearExeption(String error) { + super(error); + } +} diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Part.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Part.java new file mode 100644 index 00000000..202aec2b --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Part.java @@ -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"); + } + } +} diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrand.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrand.java new file mode 100644 index 00000000..79233ab0 --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrand.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson7.mohamedobjects.main; + +public enum cpuBrand { + AMD, + INTEL, + APPLE +} diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portType.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portType.java new file mode 100644 index 00000000..07af06d2 --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portType.java @@ -0,0 +1,8 @@ +package com.codedifferently.lesson7.mohamedobjects.main; + +public enum portType { + VGA, + DVI, + HDMI, + DP +} diff --git a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/mohamedobjects/main/ComputerPartsTest.java b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/mohamedobjects/main/ComputerPartsTest.java new file mode 100644 index 00000000..23e83464 --- /dev/null +++ b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/mohamedobjects/main/ComputerPartsTest.java @@ -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 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()); + } +}