From 02c4ded074e6bfdfa39ac595501369371d13de5f Mon Sep 17 00:00:00 2001 From: Mukul Palol Date: Tue, 26 Dec 2023 09:42:01 +0530 Subject: [PATCH 1/2] Added SRP assignment codes --- SRP/Example1/Printer.cs | 10 ++++++++++ SRP/Example1/Program.cs | 15 +++++++++++++++ SRP/Example1/Rectangle.cs | 19 +++++++++++++++++++ SRP/Example2/FileWriter.cs | 17 +++++++++++++++++ SRP/Example2/Program.cs | 15 +++++++++++++++ SRP/Example2/Rectangle.cs | 19 +++++++++++++++++++ 6 files changed, 95 insertions(+) create mode 100644 SRP/Example1/Printer.cs create mode 100644 SRP/Example1/Program.cs create mode 100644 SRP/Example1/Rectangle.cs create mode 100644 SRP/Example2/FileWriter.cs create mode 100644 SRP/Example2/Program.cs create mode 100644 SRP/Example2/Rectangle.cs diff --git a/SRP/Example1/Printer.cs b/SRP/Example1/Printer.cs new file mode 100644 index 0000000..ee70e30 --- /dev/null +++ b/SRP/Example1/Printer.cs @@ -0,0 +1,10 @@ +namespace RectangleAreaCalculator +{ + public class Printer + { + public void PrintArea(double area) + { + Console.WriteLine($"Area: {area}"); + } + } +} diff --git a/SRP/Example1/Program.cs b/SRP/Example1/Program.cs new file mode 100644 index 0000000..5e9696a --- /dev/null +++ b/SRP/Example1/Program.cs @@ -0,0 +1,15 @@ +namespace RectangleAreaCalculator +{ + public class Program + { + static void Main() + { + Rectangle rectangle = new Rectangle(5.0, 3.0); + + double area = rectangle.CalculateArea(); + + Printer printer = new Printer(); + printer.PrintArea(area); + } + } +} diff --git a/SRP/Example1/Rectangle.cs b/SRP/Example1/Rectangle.cs new file mode 100644 index 0000000..52e0cd9 --- /dev/null +++ b/SRP/Example1/Rectangle.cs @@ -0,0 +1,19 @@ +namespace RectangleAreaCalculator +{ + public class Rectangle + { + public double Length { get; } + public double Width { get; } + + public Rectangle(double length, double width) + { + Length = length; + Width = width; + } + + public double CalculateArea() + { + return Length * Width; + } + } +} diff --git a/SRP/Example2/FileWriter.cs b/SRP/Example2/FileWriter.cs new file mode 100644 index 0000000..a48150f --- /dev/null +++ b/SRP/Example2/FileWriter.cs @@ -0,0 +1,17 @@ +namespace RectangleAreaApplication +{ + public class FileWriter + { + public static void WriteToFile(string fileName, string content) + { + try + { + File.WriteAllText(fileName, content); + } + catch (IOException exception) + { + Console.WriteLine("Error writing to file: " + exception.Message); + } + } + } +} diff --git a/SRP/Example2/Program.cs b/SRP/Example2/Program.cs new file mode 100644 index 0000000..353b76c --- /dev/null +++ b/SRP/Example2/Program.cs @@ -0,0 +1,15 @@ +namespace RectangleAreaApplication +{ + public class Program + { + static void Main(string[] args) + { + Rectangle rectangle = new Rectangle(5.0, 3.0); + double area = rectangle.CalculateArea(); + + FileWriter.WriteToFile("area.txt", $"Area: {area}"); + + Console.WriteLine($"Calculated Area: {area}"); + } + } +} diff --git a/SRP/Example2/Rectangle.cs b/SRP/Example2/Rectangle.cs new file mode 100644 index 0000000..62d1d05 --- /dev/null +++ b/SRP/Example2/Rectangle.cs @@ -0,0 +1,19 @@ +namespace RectangleAreaApplication +{ + public class Rectangle + { + private double length; + private double width; + + public Rectangle(double length, double width) + { + this.length = length; + this.width = width; + } + + public double CalculateArea() + { + return length * width; + } + } +} From 1cea7bcb9d7c2e0b7a87211e1f247d18a2082b6b Mon Sep 17 00:00:00 2001 From: Mukul Palol Date: Tue, 26 Dec 2023 10:41:07 +0530 Subject: [PATCH 2/2] Added OCP assignment code --- OCP/IDevice.cs | 7 +++++++ OCP/IDeviceFactory.cs | 7 +++++++ OCP/Laptop.cs | 10 ++++++++++ OCP/LaptopFactory.cs | 10 ++++++++++ OCP/Program.cs | 17 +++++++++++++++++ OCP/Smartphone.cs | 10 ++++++++++ OCP/SmartphoneFactory.cs | 10 ++++++++++ 7 files changed, 71 insertions(+) create mode 100644 OCP/IDevice.cs create mode 100644 OCP/IDeviceFactory.cs create mode 100644 OCP/Laptop.cs create mode 100644 OCP/LaptopFactory.cs create mode 100644 OCP/Program.cs create mode 100644 OCP/Smartphone.cs create mode 100644 OCP/SmartphoneFactory.cs diff --git a/OCP/IDevice.cs b/OCP/IDevice.cs new file mode 100644 index 0000000..45944b6 --- /dev/null +++ b/OCP/IDevice.cs @@ -0,0 +1,7 @@ +namespace ElectronicDevices +{ + public interface IDevice + { + void DisplayDetails(); + } +} diff --git a/OCP/IDeviceFactory.cs b/OCP/IDeviceFactory.cs new file mode 100644 index 0000000..866afd7 --- /dev/null +++ b/OCP/IDeviceFactory.cs @@ -0,0 +1,7 @@ +namespace ElectronicDevices +{ + public interface IDeviceFactory + { + IDevice CreateDevice(); + } +} diff --git a/OCP/Laptop.cs b/OCP/Laptop.cs new file mode 100644 index 0000000..9068e01 --- /dev/null +++ b/OCP/Laptop.cs @@ -0,0 +1,10 @@ +namespace ElectronicDevices +{ + public class Laptop : IDevice + { + public void DisplayDetails() + { + Console.WriteLine("Laptop: Model Y, RAM: 16GB, Storage: 512GB"); + } + } +} diff --git a/OCP/LaptopFactory.cs b/OCP/LaptopFactory.cs new file mode 100644 index 0000000..24bcee6 --- /dev/null +++ b/OCP/LaptopFactory.cs @@ -0,0 +1,10 @@ +namespace ElectronicDevices +{ + public class LaptopFactory : IDeviceFactory + { + public IDevice CreateDevice() + { + return new Laptop(); + } + } +} diff --git a/OCP/Program.cs b/OCP/Program.cs new file mode 100644 index 0000000..e506171 --- /dev/null +++ b/OCP/Program.cs @@ -0,0 +1,17 @@ +namespace ElectronicDevices +{ + internal class Program + { + static void Main(string[] args) + { + IDeviceFactory smartphoneFactory = new SmartphoneFactory(); + IDeviceFactory laptopFactory = new LaptopFactory(); + + IDevice smartphone = smartphoneFactory.CreateDevice(); + IDevice laptop = laptopFactory.CreateDevice(); + + smartphone.DisplayDetails(); + laptop.DisplayDetails(); + } + } +} diff --git a/OCP/Smartphone.cs b/OCP/Smartphone.cs new file mode 100644 index 0000000..8fe83e1 --- /dev/null +++ b/OCP/Smartphone.cs @@ -0,0 +1,10 @@ +namespace ElectronicDevices +{ + public class Smartphone : IDevice + { + public void DisplayDetails() + { + Console.WriteLine("Smartphone: Model X, RAM: 8GB, Storage: 128GB"); + } + } +} diff --git a/OCP/SmartphoneFactory.cs b/OCP/SmartphoneFactory.cs new file mode 100644 index 0000000..e875907 --- /dev/null +++ b/OCP/SmartphoneFactory.cs @@ -0,0 +1,10 @@ +namespace ElectronicDevices +{ + public class SmartphoneFactory : IDeviceFactory + { + public IDevice CreateDevice() + { + return new Smartphone(); + } + } +}