From e473bf266d3422aab3f6746840b0fc8214306b51 Mon Sep 17 00:00:00 2001 From: Mohamed <“moibrahi7@gmail.com”> Date: Thu, 14 Mar 2024 15:59:30 +0000 Subject: [PATCH 1/7] Feat: Added Parts, Cpu, and Gpu classes, added custom exeption with enum files. --- .../lesson7/MohamedObjects/Main/CPU.java | 41 ++++++++++++ .../lesson7/MohamedObjects/Main/GPU.java | 65 +++++++++++++++++++ .../Main/IllegalYearExeption.java | 7 ++ .../lesson7/MohamedObjects/Main/Parts.java | 49 ++++++++++++++ .../MohamedObjects/Main/cpuBrands.java | 5 ++ .../MohamedObjects/Main/portTypes.java | 8 +++ .../codedifferently/lesson7/MohamedTest.java | 65 +++++++++++++++++++ 7 files changed, 240 insertions(+) create mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/CPU.java create mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/GPU.java create mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/IllegalYearExeption.java create mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Parts.java create mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrands.java create mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portTypes.java create mode 100644 lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MohamedTest.java 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..3b344297 --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/CPU.java @@ -0,0 +1,41 @@ +package com.codedifferently.lesson7.MohamedObjects.Main; + + +public class CPU extends Parts { + double clockSpeed; + int cores; + + public CPU() {} + + public CPU(String name, int releaseYear, cpuBrands brand, double clockSpeed) { + this.name = name; + checkValidYear(releaseYear); + this.releaseYear = releaseYear; + this.brand = brand.toString(); + this.clockSpeed = clockSpeed; + } + public CPU(String name, int releaseYear, cpuBrands brand, double clockSpeed, int cores) { + this.name = name; + checkValidYear(releaseYear); + this.releaseYear = releaseYear; + this.brand = brand.toString(); + this.clockSpeed = clockSpeed; + this.cores = cores; + } + + public void setClockSpeed(double clockSpeed) { + this.clockSpeed = clockSpeed; + } + + public double getClockSpeed() { + return clockSpeed; + } + + public void setCores(int cores) { + this.cores = cores; + } + + public int getCores() { + return cores; + } +} 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..4c479a6d --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/GPU.java @@ -0,0 +1,65 @@ +package com.codedifferently.lesson7.MohamedObjects.Main; + +import java.util.EnumMap; +import java.util.EnumSet; +import java.util.HashSet; +import java.util.Set; + +enum empty{ + +} + + +public class GPU extends Parts { + + Set ports = new HashSet(); + + int vRam; + + boolean hasRayTracing; + + public GPU() {} + + public GPU(String name, int releaseYear, String brand, int vRam) { + this.vRam = vRam; + this.name = name; + checkValidYear(releaseYear); + this.releaseYear = releaseYear; + this.brand = brand; + } + + + 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; + } + + public void setvRam(int vRam) { + this.vRam = vRam; + } + + public void setHasRayTracing(boolean hasRayTracing) { + this.hasRayTracing = hasRayTracing; + } + public int getvRam() { + return vRam; + } + public Set getPorts() { + return ports; + } + + public void addPorts(portTypes port) { + this.ports.add(port); + } + public int printNumPorts(){ + int i = 0; + for (portTypes a : ports){ + i++; + } + return i; + } + +} 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..e5f876a4 --- /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/Parts.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Parts.java new file mode 100644 index 00000000..5a651f7a --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Parts.java @@ -0,0 +1,49 @@ +package com.codedifferently.lesson7.MohamedObjects.Main; + +public class Parts { + String name; + int releaseYear; + String brand; + + public Parts() {} + + public Parts(String name, int releaseYear, String brand) { + this.name = name; + checkValidYear(releaseYear); + this.releaseYear = releaseYear; + this.brand = brand; + } + + 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; + } + + public String getBrand() { + return brand; + } + + public String getName() { + return name; + } + + public int getReleaseYear() { + return releaseYear; + } + + 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/cpuBrands.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrands.java new file mode 100644 index 00000000..cdcc7c36 --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrands.java @@ -0,0 +1,5 @@ +package com.codedifferently.lesson7.MohamedObjects.Main; + +public enum cpuBrands{ + AMD, INTEL, APPLE +} \ No newline at end of file diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portTypes.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portTypes.java new file mode 100644 index 00000000..37f9d5e5 --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portTypes.java @@ -0,0 +1,8 @@ +package com.codedifferently.lesson7.MohamedObjects.Main; + +public enum portTypes { + VGA, + DVI, + HDMI, + DP + } \ No newline at end of file diff --git a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MohamedTest.java b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MohamedTest.java new file mode 100644 index 00000000..98785793 --- /dev/null +++ b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MohamedTest.java @@ -0,0 +1,65 @@ +package com.codedifferently.lesson7; + + + + +import static org.junit.jupiter.api.Assertions.assertTrue; + +// import java.lang.reflect.Method; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +import com.codedifferently.lesson7.MohamedObjects.Main.CPU; +import com.codedifferently.lesson7.MohamedObjects.Main.GPU; +import com.codedifferently.lesson7.MohamedObjects.Main.IllegalYearExeption; +import com.codedifferently.lesson7.MohamedObjects.Main.cpuBrands; +import com.codedifferently.lesson7.MohamedObjects.Main.portTypes; + +import java.util.*; + +class MohamedTest { + + @Test + public void setClockSpeed(){ + CPU cpu = new CPU("cpu1", 2000, cpuBrands.INTEL ,23.32); + assertEquals(23.32,cpu.getClockSpeed()); + } + + @Test + void gpuReleaseYear() throws IllegalYearExeption{ + GPU aGpu = new GPU(); + try { + aGpu.setReleaseYear(1999); + fail("Expected IllegalYearExeption"); + } catch (IllegalYearExeption e) { + assertEquals("Year is out of expected range. 2000 - 2024", e.getMessage()); + } + } + + @Test + void testGpuPorts(){ + + Set porty = Set.of(portTypes.DP, portTypes.HDMI, portTypes.VGA ); + GPU bGpu = new GPU("Radion 560 xt", 2015, "AMD", 8); + bGpu.addPorts(portTypes.DP); + bGpu.addPorts(portTypes.HDMI); + bGpu.addPorts(portTypes.VGA); + assertEquals(bGpu.getPorts(), porty); + } + + @Test + void testSize(){ + GPU bGpu = new GPU("GTX 1050", 2015, "Navidia", 8); + bGpu.addPorts(portTypes.DP); + bGpu.addPorts(portTypes.HDMI); + bGpu.addPorts(portTypes.VGA); + assertEquals(bGpu.printNumPorts(), 3 ); + } + + @Test + void testCores(){ + CPU m1 = new CPU("M1", 2020, cpuBrands.APPLE, 4.0, 8); + assertEquals(m1.getCores(), 8);; + } +} From 856435688e23de5be816ee515d78ba8051c597b3 Mon Sep 17 00:00:00 2001 From: Mohamed <“moibrahi7@gmail.com”> Date: Thu, 14 Mar 2024 16:07:13 +0000 Subject: [PATCH 2/7] Chore: ran spotless apply to ensure code was up to formating standard --- .../lesson7/MohamedObjects/Main/CPU.java | 2 +- .../lesson7/MohamedObjects/Main/GPU.java | 20 +++++------ .../MohamedObjects/Main/cpuBrands.java | 8 +++-- .../MohamedObjects/Main/portTypes.java | 10 +++--- .../codedifferently/lesson7/MohamedTest.java | 33 ++++++++----------- 5 files changed, 33 insertions(+), 40 deletions(-) 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 index 3b344297..94d13d60 100644 --- 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 @@ -1,6 +1,5 @@ package com.codedifferently.lesson7.MohamedObjects.Main; - public class CPU extends Parts { double clockSpeed; int cores; @@ -14,6 +13,7 @@ public CPU(String name, int releaseYear, cpuBrands brand, double clockSpeed) { this.brand = brand.toString(); this.clockSpeed = clockSpeed; } + public CPU(String name, int releaseYear, cpuBrands brand, double clockSpeed, int cores) { this.name = name; checkValidYear(releaseYear); 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 index 4c479a6d..66b49612 100644 --- 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 @@ -1,14 +1,9 @@ package com.codedifferently.lesson7.MohamedObjects.Main; -import java.util.EnumMap; -import java.util.EnumSet; import java.util.HashSet; import java.util.Set; -enum empty{ - -} - +enum empty {} public class GPU extends Parts { @@ -28,7 +23,6 @@ public GPU(String name, int releaseYear, String brand, int vRam) { this.brand = brand; } - public GPU(String name, int releaseYear, String brand, int vRam, boolean hasRayTracing) { this.vRam = vRam; this.name = name; @@ -40,13 +34,15 @@ public GPU(String name, int releaseYear, String brand, int vRam, boolean hasRayT public void setvRam(int vRam) { this.vRam = vRam; } - + public void setHasRayTracing(boolean hasRayTracing) { - this.hasRayTracing = hasRayTracing; + this.hasRayTracing = hasRayTracing; } + public int getvRam() { return vRam; } + public Set getPorts() { return ports; } @@ -54,12 +50,12 @@ public Set getPorts() { public void addPorts(portTypes port) { this.ports.add(port); } - public int printNumPorts(){ + + public int printNumPorts() { int i = 0; - for (portTypes a : ports){ + for (portTypes a : ports) { i++; } return i; } - } diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrands.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrands.java index cdcc7c36..4a00f15f 100644 --- a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrands.java +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrands.java @@ -1,5 +1,7 @@ package com.codedifferently.lesson7.MohamedObjects.Main; -public enum cpuBrands{ - AMD, INTEL, APPLE -} \ No newline at end of file +public enum cpuBrands { + AMD, + INTEL, + APPLE +} diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portTypes.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portTypes.java index 37f9d5e5..e2acc402 100644 --- a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portTypes.java +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portTypes.java @@ -1,8 +1,8 @@ package com.codedifferently.lesson7.MohamedObjects.Main; public enum portTypes { - VGA, - DVI, - HDMI, - DP - } \ No newline at end of file + VGA, + DVI, + HDMI, + DP +} diff --git a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MohamedTest.java b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MohamedTest.java index 98785793..644eb16d 100644 --- a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MohamedTest.java +++ b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MohamedTest.java @@ -1,13 +1,7 @@ package com.codedifferently.lesson7; - - - -import static org.junit.jupiter.api.Assertions.assertTrue; - // import java.lang.reflect.Method; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; import com.codedifferently.lesson7.MohamedObjects.Main.CPU; @@ -15,19 +9,19 @@ import com.codedifferently.lesson7.MohamedObjects.Main.IllegalYearExeption; import com.codedifferently.lesson7.MohamedObjects.Main.cpuBrands; import com.codedifferently.lesson7.MohamedObjects.Main.portTypes; - import java.util.*; +import org.junit.jupiter.api.Test; class MohamedTest { - + @Test - public void setClockSpeed(){ - CPU cpu = new CPU("cpu1", 2000, cpuBrands.INTEL ,23.32); - assertEquals(23.32,cpu.getClockSpeed()); + public void setClockSpeed() { + CPU cpu = new CPU("cpu1", 2000, cpuBrands.INTEL, 23.32); + assertEquals(23.32, cpu.getClockSpeed()); } @Test - void gpuReleaseYear() throws IllegalYearExeption{ + void gpuReleaseYear() throws IllegalYearExeption { GPU aGpu = new GPU(); try { aGpu.setReleaseYear(1999); @@ -38,9 +32,9 @@ void gpuReleaseYear() throws IllegalYearExeption{ } @Test - void testGpuPorts(){ - - Set porty = Set.of(portTypes.DP, portTypes.HDMI, portTypes.VGA ); + void testGpuPorts() { + + Set porty = Set.of(portTypes.DP, portTypes.HDMI, portTypes.VGA); GPU bGpu = new GPU("Radion 560 xt", 2015, "AMD", 8); bGpu.addPorts(portTypes.DP); bGpu.addPorts(portTypes.HDMI); @@ -49,17 +43,18 @@ void testGpuPorts(){ } @Test - void testSize(){ + void testSize() { GPU bGpu = new GPU("GTX 1050", 2015, "Navidia", 8); bGpu.addPorts(portTypes.DP); bGpu.addPorts(portTypes.HDMI); bGpu.addPorts(portTypes.VGA); - assertEquals(bGpu.printNumPorts(), 3 ); + assertEquals(bGpu.printNumPorts(), 3); } @Test - void testCores(){ + void testCores() { CPU m1 = new CPU("M1", 2020, cpuBrands.APPLE, 4.0, 8); - assertEquals(m1.getCores(), 8);; + assertEquals(m1.getCores(), 8); + ; } } From b48d11e615f8091752df651ed6e998c2813da276 Mon Sep 17 00:00:00 2001 From: Mohamed <“moibrahi7@gmail.com”> Date: Thu, 14 Mar 2024 19:00:17 +0000 Subject: [PATCH 3/7] Fix: Changed nameing to be clear and fit conventions and provided mor consise tests --- .../lesson7/MohamedObjects/Main/CPU.java | 12 ++-- .../lesson7/MohamedObjects/Main/GPU.java | 25 +++---- .../Main/IllegalYearExeption.java | 2 +- .../lesson7/MohamedObjects/Main/Parts.java | 49 -------------- .../MohamedObjects/Main/cpuBrands.java | 7 -- .../MohamedObjects/Main/portTypes.java | 8 --- .../codedifferently/lesson7/MohamedTest.java | 60 ----------------- .../main/ComputerPartsTest.java | 65 +++++++++++++++++++ 8 files changed, 83 insertions(+), 145 deletions(-) delete mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Parts.java delete mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrands.java delete mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portTypes.java delete mode 100644 lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MohamedTest.java create mode 100644 lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/mohamedobjects/main/ComputerPartsTest.java 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 index 94d13d60..33c7ba80 100644 --- 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 @@ -1,12 +1,14 @@ -package com.codedifferently.lesson7.MohamedObjects.Main; +package com.codedifferently.lesson7.mohamedobjects.main; -public class CPU extends Parts { +public class CPU extends Part { double clockSpeed; int cores; - public CPU() {} + public CPU(cpuBrand brand) { + this.brand = brand.toString(); + } - public CPU(String name, int releaseYear, cpuBrands brand, double clockSpeed) { + public CPU(String name, int releaseYear, cpuBrand brand, double clockSpeed) { this.name = name; checkValidYear(releaseYear); this.releaseYear = releaseYear; @@ -14,7 +16,7 @@ public CPU(String name, int releaseYear, cpuBrands brand, double clockSpeed) { this.clockSpeed = clockSpeed; } - public CPU(String name, int releaseYear, cpuBrands brand, double clockSpeed, int cores) { + public CPU(String name, int releaseYear, cpuBrand brand, double clockSpeed, int cores) { this.name = name; checkValidYear(releaseYear); this.releaseYear = releaseYear; 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 index 66b49612..9288c2f5 100644 --- 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 @@ -1,17 +1,15 @@ -package com.codedifferently.lesson7.MohamedObjects.Main; +package com.codedifferently.lesson7.mohamedobjects.main; import java.util.HashSet; import java.util.Set; -enum empty {} +public class GPU extends Part { -public class GPU extends Parts { + private Set ports = new HashSet(); - Set ports = new HashSet(); + private int vRam; - int vRam; - - boolean hasRayTracing; + private boolean hasRayTracing; public GPU() {} @@ -43,19 +41,16 @@ public int getvRam() { return vRam; } - public Set getPorts() { + public Set getPorts() { return ports; } - public void addPorts(portTypes port) { + public void addPorts(portType port) { this.ports.add(port); } - public int printNumPorts() { - int i = 0; - for (portTypes a : ports) { - i++; - } - return i; + public int calculateNumPorts() { + // this was set to a loop to meet project reqirements + 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 index e5f876a4..250212ad 100644 --- 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 @@ -1,4 +1,4 @@ -package com.codedifferently.lesson7.MohamedObjects.Main; +package com.codedifferently.lesson7.mohamedobjects.main; public class IllegalYearExeption extends RuntimeException { IllegalYearExeption(String error) { diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Parts.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Parts.java deleted file mode 100644 index 5a651f7a..00000000 --- a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Parts.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.codedifferently.lesson7.MohamedObjects.Main; - -public class Parts { - String name; - int releaseYear; - String brand; - - public Parts() {} - - public Parts(String name, int releaseYear, String brand) { - this.name = name; - checkValidYear(releaseYear); - this.releaseYear = releaseYear; - this.brand = brand; - } - - 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; - } - - public String getBrand() { - return brand; - } - - public String getName() { - return name; - } - - public int getReleaseYear() { - return releaseYear; - } - - 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/cpuBrands.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrands.java deleted file mode 100644 index 4a00f15f..00000000 --- a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrands.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.codedifferently.lesson7.MohamedObjects.Main; - -public enum cpuBrands { - AMD, - INTEL, - APPLE -} diff --git a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portTypes.java b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portTypes.java deleted file mode 100644 index e2acc402..00000000 --- a/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portTypes.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.codedifferently.lesson7.MohamedObjects.Main; - -public enum portTypes { - VGA, - DVI, - HDMI, - DP -} diff --git a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MohamedTest.java b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MohamedTest.java deleted file mode 100644 index 644eb16d..00000000 --- a/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/MohamedTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.codedifferently.lesson7; - -// import java.lang.reflect.Method; - -import static org.junit.jupiter.api.Assertions.*; - -import com.codedifferently.lesson7.MohamedObjects.Main.CPU; -import com.codedifferently.lesson7.MohamedObjects.Main.GPU; -import com.codedifferently.lesson7.MohamedObjects.Main.IllegalYearExeption; -import com.codedifferently.lesson7.MohamedObjects.Main.cpuBrands; -import com.codedifferently.lesson7.MohamedObjects.Main.portTypes; -import java.util.*; -import org.junit.jupiter.api.Test; - -class MohamedTest { - - @Test - public void setClockSpeed() { - CPU cpu = new CPU("cpu1", 2000, cpuBrands.INTEL, 23.32); - assertEquals(23.32, cpu.getClockSpeed()); - } - - @Test - void gpuReleaseYear() throws IllegalYearExeption { - GPU aGpu = new GPU(); - try { - aGpu.setReleaseYear(1999); - fail("Expected IllegalYearExeption"); - } catch (IllegalYearExeption e) { - assertEquals("Year is out of expected range. 2000 - 2024", e.getMessage()); - } - } - - @Test - void testGpuPorts() { - - Set porty = Set.of(portTypes.DP, portTypes.HDMI, portTypes.VGA); - GPU bGpu = new GPU("Radion 560 xt", 2015, "AMD", 8); - bGpu.addPorts(portTypes.DP); - bGpu.addPorts(portTypes.HDMI); - bGpu.addPorts(portTypes.VGA); - assertEquals(bGpu.getPorts(), porty); - } - - @Test - void testSize() { - GPU bGpu = new GPU("GTX 1050", 2015, "Navidia", 8); - bGpu.addPorts(portTypes.DP); - bGpu.addPorts(portTypes.HDMI); - bGpu.addPorts(portTypes.VGA); - assertEquals(bGpu.printNumPorts(), 3); - } - - @Test - void testCores() { - CPU m1 = new CPU("M1", 2020, cpuBrands.APPLE, 4.0, 8); - assertEquals(m1.getCores(), 8); - ; - } -} 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..0dfc92f7 --- /dev/null +++ b/lesson_07/objects/objects_app/src/test/java/com/codedifferently/lesson7/mohamedobjects/main/ComputerPartsTest.java @@ -0,0 +1,65 @@ +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 { + CPU cpu = new CPU("M1", 2020, cpuBrand.INTEL, 23.32); + GPU bGpu = new GPU("Radion 560 xt", 2015, "AMD", 8); + Part apart = new Part(); + + @Test + public void testCpuConstruction() { + 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() { + + 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() { + assertThrows( + IllegalYearExeption.class, + () -> { + bGpu.checkValidYear(1999); + }); + assertThrows( + IllegalYearExeption.class, + () -> { + bGpu.checkValidYear(3000); + }); + } + + @Test + void testPartConstruction() { + 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()); + } +} From 14e49dbf3a60c4a75039ee4ba62e1fca6afc28ca Mon Sep 17 00:00:00 2001 From: Mohamed <“moibrahi7@gmail.com”> Date: Thu, 14 Mar 2024 19:03:22 +0000 Subject: [PATCH 4/7] Fic: added 3 missing files --- .../lesson7/MohamedObjects/Main/Part.java | 49 +++++++++++++++++++ .../lesson7/MohamedObjects/Main/cpuBrand.java | 7 +++ .../lesson7/MohamedObjects/Main/portType.java | 8 +++ 3 files changed, 64 insertions(+) create mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Part.java create mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/cpuBrand.java create mode 100644 lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/portType.java 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..52df260f --- /dev/null +++ b/lesson_07/objects/objects_app/src/main/java/com/codedifferently/lesson7/MohamedObjects/Main/Part.java @@ -0,0 +1,49 @@ +package com.codedifferently.lesson7.mohamedobjects.main; + +public class Part { + String name; + int releaseYear; + String brand; + + public Part() {} + + public Part(String name, int releaseYear, String brand) { + this.name = name; + checkValidYear(releaseYear); + this.releaseYear = releaseYear; + this.brand = brand; + } + + 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; + } + + public String getBrand() { + return brand; + } + + public String getName() { + return name; + } + + public int getReleaseYear() { + return releaseYear; + } + + 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 +} From 256d6928c668fce2ccb2eced26a281929058ce1e Mon Sep 17 00:00:00 2001 From: Mohamed <“moibrahi7@gmail.com”> Date: Mon, 18 Mar 2024 17:59:42 +0000 Subject: [PATCH 5/7] Feat: added CPU performance calc and comments to help the code be more readable --- .../lesson7/MohamedObjects/Main/CPU.java | 33 +++++++++++++++---- .../lesson7/MohamedObjects/Main/GPU.java | 17 ++++++++-- .../lesson7/MohamedObjects/Main/Part.java | 15 +++++++++ .../main/ComputerPartsTest.java | 21 ++++++++---- 4 files changed, 71 insertions(+), 15 deletions(-) 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 index 33c7ba80..7378951b 100644 --- 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 @@ -1,13 +1,18 @@ 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 { - double clockSpeed; - int cores; + // 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); @@ -16,6 +21,7 @@ public CPU(String name, int releaseYear, cpuBrand brand, double clockSpeed) { 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); @@ -25,19 +31,34 @@ public CPU(String name, int releaseYear, cpuBrand brand, double clockSpeed, int this.cores = cores; } + // Fuctions that set the value for all the member variables in CPU public void setClockSpeed(double clockSpeed) { this.clockSpeed = clockSpeed; } - public double getClockSpeed() { - return 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 index 9288c2f5..b113e97e 100644 --- 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 @@ -3,16 +3,18 @@ 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; @@ -21,14 +23,17 @@ public GPU(String name, int releaseYear, String brand, int vRam) { 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; } @@ -37,20 +42,26 @@ 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() { - // this was set to a loop to meet project reqirements return ports.size(); } } 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 index 52df260f..202aec2b 100644 --- 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 @@ -1,12 +1,15 @@ 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); @@ -14,6 +17,7 @@ public Part(String name, int releaseYear, String brand) { this.brand = brand; } + // Fuctions that set the value for all the member variables in Part public void setBrand(String brand) { this.brand = brand; } @@ -27,6 +31,7 @@ public void setReleaseYear(int releaseYear) { this.releaseYear = releaseYear; } + // Functions that retreve the member variables. public String getBrand() { return brand; } @@ -39,6 +44,16 @@ 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; 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 index 0dfc92f7..5dc70c9c 100644 --- 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 @@ -8,28 +8,24 @@ import org.junit.jupiter.api.Test; class ComputerPartsTest { - CPU cpu = new CPU("M1", 2020, cpuBrand.INTEL, 23.32); - GPU bGpu = new GPU("Radion 560 xt", 2015, "AMD", 8); Part apart = new Part(); @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); @@ -41,6 +37,8 @@ void testGpuConstruction() { @Test void testYearValadation() { + GPU bGpu = new GPU("Radion 560 xt", 2015, "AMD", 8); + assertThrows( IllegalYearExeption.class, () -> { @@ -55,11 +53,22 @@ void testYearValadation() { @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()); + } } From 08a3947dce9c5cf5fde57c629b874733bab1680f Mon Sep 17 00:00:00 2001 From: Mohamed <“moibrahi7@gmail.com”> Date: Mon, 18 Mar 2024 18:00:36 +0000 Subject: [PATCH 6/7] Chore: Removed un-need new Part --- .../lesson7/mohamedobjects/main/ComputerPartsTest.java | 1 - 1 file changed, 1 deletion(-) 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 index 5dc70c9c..c77bfc87 100644 --- 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 @@ -8,7 +8,6 @@ import org.junit.jupiter.api.Test; class ComputerPartsTest { - Part apart = new Part(); @Test public void testCpuConstruction() { From cd91e08b1131791e3432b6e89f98de12a2edead4 Mon Sep 17 00:00:00 2001 From: Mohamed <“moibrahi7@gmail.com”> Date: Mon, 18 Mar 2024 20:19:31 +0000 Subject: [PATCH 7/7] Chore: formatting was changed --- .../lesson7/mohamedobjects/main/ComputerPartsTest.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 index c77bfc87..23e83464 100644 --- 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 @@ -12,6 +12,7 @@ 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()); @@ -26,11 +27,12 @@ public void testCpuConstruction() { 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(porty, bGpu.getPorts()); assertEquals(3, bGpu.calculateNumPorts()); } @@ -66,8 +68,10 @@ void testPartConstruction() { @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()); } }