From 284d1817c7086ab89d260af551d89adba246117a Mon Sep 17 00:00:00 2001 From: Pradeep Dhupar Date: Tue, 26 Dec 2023 14:39:35 +0530 Subject: [PATCH] Assignment 3 --- OCP/ClientWithFactory.java | 12 ++++++++++++ OCP/Device.java | 3 +++ OCP/DeviceFactory.java | 3 +++ OCP/DeviceType.java | 4 ++++ OCP/Laptop.java | 6 ++++++ OCP/LaptopFactory.java | 6 ++++++ OCP/Smartphone.java | 6 ++++++ OCP/SmartphoneFactory.java | 6 ++++++ SRP_Example1/AreaCalculator.java | 5 +++++ SRP_Example1/AreaPrinter.java | 6 ++++++ SRP_Example1/Main.java | 6 ++++++ SRP_Example1/Rectangle.java | 17 +++++++++++++++++ SRP_Example2/AreaCalculator.java | 5 +++++ SRP_Example2/FileWriterUtil.java | 12 ++++++++++++ SRP_Example2/Main.java | 12 ++++++++++++ SRP_Example2/Rectangle.java | 17 +++++++++++++++++ 16 files changed, 126 insertions(+) create mode 100644 OCP/ClientWithFactory.java create mode 100644 OCP/Device.java create mode 100644 OCP/DeviceFactory.java create mode 100644 OCP/DeviceType.java create mode 100644 OCP/Laptop.java create mode 100644 OCP/LaptopFactory.java create mode 100644 OCP/Smartphone.java create mode 100644 OCP/SmartphoneFactory.java create mode 100644 SRP_Example1/AreaCalculator.java create mode 100644 SRP_Example1/AreaPrinter.java create mode 100644 SRP_Example1/Main.java create mode 100644 SRP_Example1/Rectangle.java create mode 100644 SRP_Example2/AreaCalculator.java create mode 100644 SRP_Example2/FileWriterUtil.java create mode 100644 SRP_Example2/Main.java create mode 100644 SRP_Example2/Rectangle.java diff --git a/OCP/ClientWithFactory.java b/OCP/ClientWithFactory.java new file mode 100644 index 0000000..a130503 --- /dev/null +++ b/OCP/ClientWithFactory.java @@ -0,0 +1,12 @@ +public class ClientWithFactory { + public static void main(String[] args) { + DeviceFactory smartphoneFactory = new SmartphoneFactory(); + DeviceFactory laptopFactory = new LaptopFactory(); + + Device smartphone = smartphoneFactory.createDevice(); + Device laptop = laptopFactory.createDevice(); + + smartphone.displayDetails(); + laptop.displayDetails(); + } +} diff --git a/OCP/Device.java b/OCP/Device.java new file mode 100644 index 0000000..f8d9356 --- /dev/null +++ b/OCP/Device.java @@ -0,0 +1,3 @@ +public interface Device { + void displayDetails(); +} \ No newline at end of file diff --git a/OCP/DeviceFactory.java b/OCP/DeviceFactory.java new file mode 100644 index 0000000..8761b16 --- /dev/null +++ b/OCP/DeviceFactory.java @@ -0,0 +1,3 @@ +public interface DeviceFactory { + Device createDevice(); +} diff --git a/OCP/DeviceType.java b/OCP/DeviceType.java new file mode 100644 index 0000000..0492744 --- /dev/null +++ b/OCP/DeviceType.java @@ -0,0 +1,4 @@ +public enum DeviceType { + SMARTPHONE, + LAPTOP +} diff --git a/OCP/Laptop.java b/OCP/Laptop.java new file mode 100644 index 0000000..bc631b3 --- /dev/null +++ b/OCP/Laptop.java @@ -0,0 +1,6 @@ +public class Laptop implements Device { + @Override + public void displayDetails() { + System.out.println("Laptop: Model Y, RAM: 16GB, Storage: 512GB"); + } +} diff --git a/OCP/LaptopFactory.java b/OCP/LaptopFactory.java new file mode 100644 index 0000000..e08aa27 --- /dev/null +++ b/OCP/LaptopFactory.java @@ -0,0 +1,6 @@ +public class LaptopFactory implements DeviceFactory { + @Override + public Device createDevice() { + return new Laptop(); + } +} diff --git a/OCP/Smartphone.java b/OCP/Smartphone.java new file mode 100644 index 0000000..c263e55 --- /dev/null +++ b/OCP/Smartphone.java @@ -0,0 +1,6 @@ +public class Smartphone implements Device { + @Override + public void displayDetails() { + System.out.println("Smartphone: Model X, RAM: 8GB, Storage: 128GB"); + } +} diff --git a/OCP/SmartphoneFactory.java b/OCP/SmartphoneFactory.java new file mode 100644 index 0000000..707f34d --- /dev/null +++ b/OCP/SmartphoneFactory.java @@ -0,0 +1,6 @@ +public class SmartphoneFactory implements DeviceFactory { + @Override + public Device createDevice() { + return new Smartphone(); + } +} diff --git a/SRP_Example1/AreaCalculator.java b/SRP_Example1/AreaCalculator.java new file mode 100644 index 0000000..224c711 --- /dev/null +++ b/SRP_Example1/AreaCalculator.java @@ -0,0 +1,5 @@ +public class AreaCalculator { + public static double calculateArea(Rectangle rectangle) { + return rectangle.getLength() * rectangle.getWidth(); + } +} \ No newline at end of file diff --git a/SRP_Example1/AreaPrinter.java b/SRP_Example1/AreaPrinter.java new file mode 100644 index 0000000..afe6d20 --- /dev/null +++ b/SRP_Example1/AreaPrinter.java @@ -0,0 +1,6 @@ +public class AreaPrinter { + public static void printArea(Rectangle rectangle) { + double area = AreaCalculator.calculateArea(rectangle); + System.out.println("Area: " + area); + } +} \ No newline at end of file diff --git a/SRP_Example1/Main.java b/SRP_Example1/Main.java new file mode 100644 index 0000000..b778363 --- /dev/null +++ b/SRP_Example1/Main.java @@ -0,0 +1,6 @@ +public class Main { + public static void main(String[] args) { + Rectangle rectangle = new Rectangle(5.0, 3.0); + AreaPrinter.printArea(rectangle); + } +} \ No newline at end of file diff --git a/SRP_Example1/Rectangle.java b/SRP_Example1/Rectangle.java new file mode 100644 index 0000000..b574d89 --- /dev/null +++ b/SRP_Example1/Rectangle.java @@ -0,0 +1,17 @@ +public class Rectangle { + private double length; + private double width; + + public Rectangle(double length, double width) { + this.length = length; + this.width = width; + } + + public double getLength() { + return length; + } + + public double getWidth() { + return width; + } +} diff --git a/SRP_Example2/AreaCalculator.java b/SRP_Example2/AreaCalculator.java new file mode 100644 index 0000000..224c711 --- /dev/null +++ b/SRP_Example2/AreaCalculator.java @@ -0,0 +1,5 @@ +public class AreaCalculator { + public static double calculateArea(Rectangle rectangle) { + return rectangle.getLength() * rectangle.getWidth(); + } +} \ No newline at end of file diff --git a/SRP_Example2/FileWriterUtil.java b/SRP_Example2/FileWriterUtil.java new file mode 100644 index 0000000..eb21c33 --- /dev/null +++ b/SRP_Example2/FileWriterUtil.java @@ -0,0 +1,12 @@ +import java.io.FileWriter; +import java.io.IOException; + +public class FileWriterUtil { + public static void writeToFile(String fileName, String content) { + try (FileWriter writer = new FileWriter(fileName)) { + writer.write(content); + } catch (IOException e) { + System.err.println("Error writing to file: " + e.getMessage()); + } + } +} diff --git a/SRP_Example2/Main.java b/SRP_Example2/Main.java new file mode 100644 index 0000000..05d7e5c --- /dev/null +++ b/SRP_Example2/Main.java @@ -0,0 +1,12 @@ +public class Main { + public static void main(String[] args) { + Rectangle rectangle = new Rectangle(5.0, 3.0); + + // Calculate area + double area = AreaCalculator.calculateArea(rectangle); + System.out.println("Calculated Area: " + area); + + // Save area to a file + FileWriterUtil.writeToFile("area.txt", "Area: " + area); + } +} diff --git a/SRP_Example2/Rectangle.java b/SRP_Example2/Rectangle.java new file mode 100644 index 0000000..b574d89 --- /dev/null +++ b/SRP_Example2/Rectangle.java @@ -0,0 +1,17 @@ +public class Rectangle { + private double length; + private double width; + + public Rectangle(double length, double width) { + this.length = length; + this.width = width; + } + + public double getLength() { + return length; + } + + public double getWidth() { + return width; + } +}