diff --git a/Assignment-2/Problem-1.java b/Assignment-2/Problem-1.java new file mode 100644 index 0000000..f95e4d8 --- /dev/null +++ b/Assignment-2/Problem-1.java @@ -0,0 +1,27 @@ +public class Calculate { + int firstOperand; + double secondOperand; + String convertedUppercaseText; + + public void printCalculatedSum(int firstOperand, int secondOperand) { + int additionResult = firstOperand + secondOperand; + System.out.println("Result: " + additionResult); + } + + public void printUpperCaseString(String inputText) { + convertedUppercaseText = inputText.toUpperCase(); + System.out.println("Updated String: " + convertedUppercaseText); + } +} + +public class Computation { + public static void main(String[] args) { + Calculate calculate = new Calculator(); + calculate.firstOperand = 10; + calculate.secondOperand = 20.5; + calculate.convertedUppercaseText = "hello"; + + calculate.printCalculatedSum(calculate.firstOperand, 5); + calculate.printUpperCaseString("world"); + } +} diff --git a/Assignment-2/Problem-2.java b/Assignment-2/Problem-2.java new file mode 100644 index 0000000..fd9375a --- /dev/null +++ b/Assignment-2/Problem-2.java @@ -0,0 +1,97 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +class Product { + private String name; + private double price; + private int quantity; + + public setProductDetails(String name, double price, int quantity) { + this.name = name; + this.price = price; + this.quantity = quantity; + } + + public String getName() { + return name; + } + + public double getPrice() { + return price; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int newQuantity) { + this.quantity = newQuantity; + } +} + +class InventoryManager { + private List inventory; + + public InventoryManager() { + this.inventory = new ArrayList<>(); + } + + public void addProduct(Product product) { + inventory.add(product); + } + + public List getInventory() { + return inventory; + } +} + +public class Main { + public static void main(String[] args) { + InventoryManager inventoryManager = new InventoryManager(); + Scanner scanner = new Scanner(System.in); + + while (true) { + System.out.println("\nInventory Management System"); + System.out.println("1. Add Product"); + System.out.println("2. Display Inventory"); + System.out.println("3. Exit"); + System.out.print("Enter your choice: "); + + int userChoice = scanner.nextInt(); + + switch (userChoice) { + case 1: + System.out.print("Enter product name: "); + String productName = scanner.next(); + System.out.print("Enter product price: "); + double productPrice = scanner.nextDouble(); + System.out.print("Enter product quantity: "); + int productQuantity = scanner.nextInt(); + + Product newProduct = new Product(productName, productPrice, productQuantity); + inventoryManager.addProduct(newProduct); + + System.out.println("Product added successfully!"); + break; + + case 2: + System.out.println("\nCurrent Inventory:"); + List products = inventoryManager.getInventory(); + for (Product product : products) { + System.out.println("Name: " + product.getName() + + ", Price: $" + product.getPrice() + + ", Quantity: " + product.getQuantity()); + } + break; + + case 3: + System.out.println("Exiting program. Goodbye!"); + System.exit(0); + + default: + System.out.println("Invalid choice. Please enter a valid option."); + } + } + } +} diff --git a/Assignment-3/OCP/Device.java b/Assignment-3/OCP/Device.java new file mode 100644 index 0000000..7efd0c3 --- /dev/null +++ b/Assignment-3/OCP/Device.java @@ -0,0 +1,3 @@ +interface Device { + void displayDetails(); +} diff --git a/Assignment-3/OCP/DeviceFactory.java b/Assignment-3/OCP/DeviceFactory.java new file mode 100644 index 0000000..ccc7289 --- /dev/null +++ b/Assignment-3/OCP/DeviceFactory.java @@ -0,0 +1,3 @@ +interface DeviceFactory { + Device createDevice(); +} diff --git a/Assignment-3/OCP/Laptop.java b/Assignment-3/OCP/Laptop.java new file mode 100644 index 0000000..3232c05 --- /dev/null +++ b/Assignment-3/OCP/Laptop.java @@ -0,0 +1,6 @@ +class Laptop implements Device { + @Override + public void displayDetails() { + System.out.println("Laptop: Model Y, RAM: 16GB, Storage: 512GB"); + } +} diff --git a/Assignment-3/OCP/LaptopFactory.java b/Assignment-3/OCP/LaptopFactory.java new file mode 100644 index 0000000..03c16ce --- /dev/null +++ b/Assignment-3/OCP/LaptopFactory.java @@ -0,0 +1,6 @@ +class LaptopFactory implements DeviceFactory { + @Override + public Device createDevice() { + return new Laptop(); + } +} diff --git a/Assignment-3/OCP/Main.java b/Assignment-3/OCP/Main.java new file mode 100644 index 0000000..53cc17c --- /dev/null +++ b/Assignment-3/OCP/Main.java @@ -0,0 +1,12 @@ +public class Main { + 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/Assignment-3/OCP/Smartphone.java b/Assignment-3/OCP/Smartphone.java new file mode 100644 index 0000000..7d6651a --- /dev/null +++ b/Assignment-3/OCP/Smartphone.java @@ -0,0 +1,6 @@ +class Smartphone implements Device { + @Override + public void displayDetails() { + System.out.println("Smartphone: Model X, RAM: 8GB, Storage: 128GB"); + } +} diff --git a/Assignment-3/OCP/SmartphoneFactory.java b/Assignment-3/OCP/SmartphoneFactory.java new file mode 100644 index 0000000..533e575 --- /dev/null +++ b/Assignment-3/OCP/SmartphoneFactory.java @@ -0,0 +1,6 @@ +class SmartphoneFactory implements DeviceFactory { + @Override + public Device createDevice() { + return new Smartphone(); + } +} diff --git a/Assignment-3/SRP/problem-1/AreaCalculator.java b/Assignment-3/SRP/problem-1/AreaCalculator.java new file mode 100644 index 0000000..2080307 --- /dev/null +++ b/Assignment-3/SRP/problem-1/AreaCalculator.java @@ -0,0 +1,5 @@ +public class AreaCalculator { + public double calculateArea(Rectangle rectangle) { + return rectangle.getLength() * rectangle.getWidth(); + } +} diff --git a/Assignment-3/SRP/problem-1/Main.java b/Assignment-3/SRP/problem-1/Main.java new file mode 100644 index 0000000..2672d63 --- /dev/null +++ b/Assignment-3/SRP/problem-1/Main.java @@ -0,0 +1,12 @@ +public class Main { + public static void main(String[] args) { + Rectangle rectangle = new Rectangle(5.0, 3.0); + AreaCalculator calculator = new AreaCalculator(); + double area = calculator.calculateArea(rectangle); + printArea(area); + } + + public static void printArea(double area) { + System.out.println("Area: " + area); + } +} diff --git a/Assignment-3/SRP/problem-1/Rectangle.java b/Assignment-3/SRP/problem-1/Rectangle.java new file mode 100644 index 0000000..b574d89 --- /dev/null +++ b/Assignment-3/SRP/problem-1/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/Assignment-3/SRP/problem-2/AreaFileCalculator.java b/Assignment-3/SRP/problem-2/AreaFileCalculator.java new file mode 100644 index 0000000..d29f171 --- /dev/null +++ b/Assignment-3/SRP/problem-2/AreaFileCalculator.java @@ -0,0 +1,12 @@ +import java.io.FileWriter; +import java.io.IOException; + +public class AreaFileWriter { + public void writeAreaToFile(double area) { + try (FileWriter writer = new FileWriter("area.txt")) { + writer.write("Area: " + area); + } catch (IOException e) { + System.err.println("Error writing to file: " + e.getMessage()); + } + } +} diff --git a/Assignment-3/SRP/problem-2/Main.java b/Assignment-3/SRP/problem-2/Main.java new file mode 100644 index 0000000..70076ee --- /dev/null +++ b/Assignment-3/SRP/problem-2/Main.java @@ -0,0 +1,11 @@ +public class Main { + public static void main(String[] args) { + Rectangle rectangle = new Rectangle(5.0, 3.0); + double area = rectangle.calculateArea(); + + AreaFileWriter fileWriter = new AreaFileWriter(); + fileWriter.writeAreaToFile(area); + + System.out.println("Calculated Area: " + area); + } +} diff --git a/Assignment-3/SRP/problem-2/Rectangle.java b/Assignment-3/SRP/problem-2/Rectangle.java new file mode 100644 index 0000000..452006e --- /dev/null +++ b/Assignment-3/SRP/problem-2/Rectangle.java @@ -0,0 +1,13 @@ +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; + } +}