From b93a49f8bbfd45819476762f896d48901f7c018d Mon Sep 17 00:00:00 2001 From: Poyam Sharma Date: Tue, 26 Dec 2023 12:25:06 +0530 Subject: [PATCH 1/3] Added SRP Assignment --- SRP/AreaProcessor.cs | 37 +++++++++++++++++++++++++++++++++++++ SRP/Rectangle.cs | 11 +++++++++++ 2 files changed, 48 insertions(+) create mode 100644 SRP/AreaProcessor.cs create mode 100644 SRP/Rectangle.cs diff --git a/SRP/AreaProcessor.cs b/SRP/AreaProcessor.cs new file mode 100644 index 0000000..6e7974b --- /dev/null +++ b/SRP/AreaProcessor.cs @@ -0,0 +1,37 @@ +using System; + +public class AreaProcessor +{ + private static double Area; + + public static void Main() + { + Rectangle rectangle = new Rectangle(5.0, 3.0); + + Area = calculateArea(rectangle); + writeAreaToFile(); + printArea(Area); + } + + private static double calculateArea(Rectangle rectangle) + { + return rectangle.Length * rectangle.Width; + } + + private static void printArea(double area) + { + Console.WriteLine("Area is: " + area); + } + + private static void writeAreaToFile() + { + try + { + File.WriteAllText("AreaDetails.txt", Area); + } + catch (IOException exception) + { + Console.WriteLine("Error writing to file: " + exception.Message); + } + } +} \ No newline at end of file diff --git a/SRP/Rectangle.cs b/SRP/Rectangle.cs new file mode 100644 index 0000000..1a4e410 --- /dev/null +++ b/SRP/Rectangle.cs @@ -0,0 +1,11 @@ +public class Rectangle +{ + public double Length; + public double Width; + + public Rectangle(double length, double width) + { + Length = length; + Width = width; + } +} \ No newline at end of file From 9b4c0d345342d56951c3e501840d3907c7ac576d Mon Sep 17 00:00:00 2001 From: Poyam Sharma Date: Tue, 26 Dec 2023 22:03:09 +0530 Subject: [PATCH 2/3] Added OCP Assignment --- OCP/Device/Laptop.cs | 10 ++++++++++ OCP/Device/SmartPhone.cs | 10 ++++++++++ OCP/DeviceFactoryApp.cs | 17 +++++++++++++++++ OCP/Factory/LaptopFactory.cs | 10 ++++++++++ OCP/Factory/SmartPhoneFactory.cs | 10 ++++++++++ OCP/Model/IDevice.cs | 7 +++++++ OCP/Model/IDeviceFactory.cs | 7 +++++++ 7 files changed, 71 insertions(+) create mode 100644 OCP/Device/Laptop.cs create mode 100644 OCP/Device/SmartPhone.cs create mode 100644 OCP/DeviceFactoryApp.cs create mode 100644 OCP/Factory/LaptopFactory.cs create mode 100644 OCP/Factory/SmartPhoneFactory.cs create mode 100644 OCP/Model/IDevice.cs create mode 100644 OCP/Model/IDeviceFactory.cs diff --git a/OCP/Device/Laptop.cs b/OCP/Device/Laptop.cs new file mode 100644 index 0000000..9e659d2 --- /dev/null +++ b/OCP/Device/Laptop.cs @@ -0,0 +1,10 @@ +namespace Devices +{ + public class Laptop : IDevice + { + public void DisplayDetails() + { + Console.WriteLine("Laptop: Model Y, RAM: 16GB, Storage: 512GB"); + } + } +} \ No newline at end of file diff --git a/OCP/Device/SmartPhone.cs b/OCP/Device/SmartPhone.cs new file mode 100644 index 0000000..e13d0b1 --- /dev/null +++ b/OCP/Device/SmartPhone.cs @@ -0,0 +1,10 @@ +namespace Devices +{ + public class Smartphone : IDevice + { + public void DisplayDetails() + { + Console.WriteLine("Smartphone: Model X, RAM: 8GB, Storage: 128GB"); + } + } +} \ No newline at end of file diff --git a/OCP/DeviceFactoryApp.cs b/OCP/DeviceFactoryApp.cs new file mode 100644 index 0000000..747fd1e --- /dev/null +++ b/OCP/DeviceFactoryApp.cs @@ -0,0 +1,17 @@ +namespace Devices +{ + class DeviceFactoryApp + { + static void Main() + { + IDeviceFactory smartphoneFactory = new SmartphoneFactory(); + IDeviceFactory laptopFactory = new LaptopFactory(); + + IDevice smartphone = smartphoneFactory.CreateDevice(); + IDevice laptop = laptopFactory.CreateDevice(); + + smartphone.DisplayDetails(); + laptop.DisplayDetails(); + } + } +} \ No newline at end of file diff --git a/OCP/Factory/LaptopFactory.cs b/OCP/Factory/LaptopFactory.cs new file mode 100644 index 0000000..3c185c3 --- /dev/null +++ b/OCP/Factory/LaptopFactory.cs @@ -0,0 +1,10 @@ +namespace Devices +{ + public class LaptopFactory : IDeviceFactory + { + public IDevice CreateDevice() + { + return new Laptop(); + } + } +} \ No newline at end of file diff --git a/OCP/Factory/SmartPhoneFactory.cs b/OCP/Factory/SmartPhoneFactory.cs new file mode 100644 index 0000000..08d3192 --- /dev/null +++ b/OCP/Factory/SmartPhoneFactory.cs @@ -0,0 +1,10 @@ +namespace Devices +{ + public class SmartphoneFactory : IDeviceFactory + { + public IDevice CreateDevice() + { + return new Smartphone(); + } + } +} \ No newline at end of file diff --git a/OCP/Model/IDevice.cs b/OCP/Model/IDevice.cs new file mode 100644 index 0000000..42d5cc6 --- /dev/null +++ b/OCP/Model/IDevice.cs @@ -0,0 +1,7 @@ +namespace Devices +{ + public interface IDevice + { + void DisplayDetails(); + } +} \ No newline at end of file diff --git a/OCP/Model/IDeviceFactory.cs b/OCP/Model/IDeviceFactory.cs new file mode 100644 index 0000000..e480e44 --- /dev/null +++ b/OCP/Model/IDeviceFactory.cs @@ -0,0 +1,7 @@ +namespace Devices +{ + public interface IDeviceFactory + { + IDevice CreateDevice(); + } +} \ No newline at end of file From 9108aa0fcb1c12275a2eef141c3a2c2118d398e3 Mon Sep 17 00:00:00 2001 From: Poyam Sharma Date: Tue, 26 Dec 2023 22:05:09 +0530 Subject: [PATCH 3/3] Modified SRP naming conventions --- SRP/AreaProcessor.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/SRP/AreaProcessor.cs b/SRP/AreaProcessor.cs index 6e7974b..e0cb37f 100644 --- a/SRP/AreaProcessor.cs +++ b/SRP/AreaProcessor.cs @@ -2,32 +2,32 @@ public class AreaProcessor { - private static double Area; + private static double _area; public static void Main() { Rectangle rectangle = new Rectangle(5.0, 3.0); - Area = calculateArea(rectangle); - writeAreaToFile(); - printArea(Area); + _area = CalculateArea(rectangle); + WriteAreaToFile(); + PrintArea(_area); } - private static double calculateArea(Rectangle rectangle) + private static double CalculateArea(Rectangle rectangle) { return rectangle.Length * rectangle.Width; } - private static void printArea(double area) + private static void PrintArea(double area) { Console.WriteLine("Area is: " + area); } - private static void writeAreaToFile() + private static void WriteAreaToFile() { try { - File.WriteAllText("AreaDetails.txt", Area); + File.WriteAllText("AreaDetails.txt", _area); } catch (IOException exception) {