diff --git a/week-1/day-2/exercise-1/Circle/Circle.cs b/week-1/day-2/exercise-1/Circle/Circle.cs index 065ffa5d..22083426 100644 --- a/week-1/day-2/exercise-1/Circle/Circle.cs +++ b/week-1/day-2/exercise-1/Circle/Circle.cs @@ -6,8 +6,26 @@ namespace Circle { - internal class Circle + public class Circle { - // Implement the Circle class here + private double Radius; + // Constructor for the Circle class, initializes the radius + public Circle(double radius) + { + Radius = radius; + } + + // Method to calculate and return the area of the circle + public double GetArea() + { + return Math.Round(Math.PI * Radius * Radius); // Calculate and round the area + // Alternative: return Math.PI * Math.Pow(Radius, 2); + } + + // Method to calculate and return the circumference of the circle + public double GetCircumference() + { + return Math.Round(2 * Math.PI * Radius); // Calculate and round the circumference + } } } diff --git a/week-1/day-2/exercise-1/Circle/Program.cs b/week-1/day-2/exercise-1/Circle/Program.cs index 0df91b0d..980c4a80 100644 --- a/week-1/day-2/exercise-1/Circle/Program.cs +++ b/week-1/day-2/exercise-1/Circle/Program.cs @@ -1,10 +1,17 @@ namespace Circle { - internal class Program + public class Program { + // Entry point of the program static void Main(string[] args) { - // Create a Circle object and display its area and circumference + double radius = 5.5; // Define the radius of the circle + + Circle areaObj = new Circle(radius); // Create a Circle object with the given radius + + // Print the calculated area and circumference of the circle + Console.WriteLine("The Area of Circle is " + areaObj.GetArea()); + Console.WriteLine("The Circumference of Circle is " + areaObj.GetCircumference()); } } } \ No newline at end of file diff --git a/week-1/day-2/exercise-2/BankAccount/BankAccount.cs b/week-1/day-2/exercise-2/BankAccount/BankAccount.cs new file mode 100644 index 00000000..4d969f2f --- /dev/null +++ b/week-1/day-2/exercise-2/BankAccount/BankAccount.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankAccount +{ + // Abstract class + public abstract class BankAccount + { + // Properties + public long AccountNumber; // The account number associated with the bank account. + public double Balance; // The current balance in the bank account. + + // Methods + public abstract void Deposit(double amount); // Abstract method to deposit money into the account. + public abstract void Withdraw(double amount); // Abstract method to withdraw money from the account. + } + + // First Derived class + class SavingsAccount : BankAccount + { + private double InterestRate; // The interest rate for the savings account. + + // Constructor to initialize the savings account. + public SavingsAccount(long accountNumber, double balance, double interestRate) + { + AccountNumber = accountNumber; + Balance = balance; + InterestRate = interestRate; + } + + // Deposit method for savings account. + public override void Deposit(double amount) + { + Balance += amount; + } + + // Withdraw method for savings account. + public override void Withdraw(double amount) + { + if (amount <= Balance) + { + Balance -= amount; + } + else + { + Console.WriteLine($"Insufficient Balance. Your Main Balance is {Balance}"); + } + } + } + + // Second Derived class + class CheckingAccount : BankAccount + { + private double OverdraftLimit; // The overdraft limit for the checking account. + + // Constructor to initialize the checking account. + public CheckingAccount(long accountNumber, double balance, double overdraftLimit) + { + AccountNumber = accountNumber; + Balance = balance; + OverdraftLimit = overdraftLimit; + } + + // Deposit method for checking account. + public override void Deposit(double amount) + { + Balance += amount; + } + + // Withdraw method for checking account. + public override void Withdraw(double amount) + { + if (amount <= OverdraftLimit + Balance) + { + Balance -= amount; + } + else + { + Console.WriteLine("Exceed the limit of withdrawn for existing balance and overdraft limit."); + } + } + } +} diff --git a/week-1/day-2/exercise-2/BankAccount/Program.cs b/week-1/day-2/exercise-2/BankAccount/Program.cs index c58c30e2..a7ba13a0 100644 --- a/week-1/day-2/exercise-2/BankAccount/Program.cs +++ b/week-1/day-2/exercise-2/BankAccount/Program.cs @@ -1,10 +1,22 @@ namespace BankAccount { - internal class Program + public class Program { static void Main(string[] args) { - // Create SavingsAccount and CheckingAccount objects and perform operations + // Creating instances of SavingsAccount and CheckingAccount + SavingsAccount savingsAccount = new SavingsAccount(34572637483, 1000.50, 0.4); + CheckingAccount checkingAccount = new CheckingAccount(34572637483, 200, 400); + + // Performing operations on the savings account + savingsAccount.Deposit(1000); + savingsAccount.Withdraw(750); + Console.WriteLine($"SavingsAccount Balance is: {savingsAccount.Balance}"); + + // Performing operations on the checking account + checkingAccount.Deposit(1000); + checkingAccount.Withdraw(3000); + Console.WriteLine($"CheckingAccount Balance is: {checkingAccount.Balance}"); } } } \ No newline at end of file diff --git a/week-1/day-2/exercise-3/AnimalExercise/Animal.cs b/week-1/day-2/exercise-3/AnimalExercise/Animal.cs new file mode 100644 index 00000000..fb2ff9ba --- /dev/null +++ b/week-1/day-2/exercise-3/AnimalExercise/Animal.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AnimalExercise +{ + // Abstract class for animals + public abstract class Animal + { + // Properties of an animal + public string? Name; // Name of the animal (can be null) + public int Age; // Age of the animal + + // Abstract method for making a sound + // The body of this method must be provided by derived classes + public abstract void MakeSound(); + } + + // Interface for movable objects + interface IMovable + { + void Move(); // Method to define movement behavior + } + + // First derived class: Dog + class Dog : Animal, IMovable + { + public override void MakeSound() + { + // Implementation of the MakeSound method for a dog + Console.WriteLine("Dog Sounds - Woof Woof..."); + } + + public void Move() + { + // Implementation of the Move method for a dog + Console.WriteLine("This is the Move() method."); + } + } + + // Second derived class: Cat + class Cat : Animal, IMovable + { + public override void MakeSound() + { + // Implementation of the MakeSound method for a cat + Console.WriteLine("Cat Sounds - Meow Meow...\n"); + } + + public void Move() + { + // Implementation of the Move method for a cat + Console.WriteLine("This is the Move() method."); + } + } +} diff --git a/week-1/day-2/exercise-3/AnimalExercise/Program.cs b/week-1/day-2/exercise-3/AnimalExercise/Program.cs index 5928b7cb..5ddb7434 100644 --- a/week-1/day-2/exercise-3/AnimalExercise/Program.cs +++ b/week-1/day-2/exercise-3/AnimalExercise/Program.cs @@ -1,10 +1,20 @@ namespace AnimalExercise { - internal class Program + public class Program { static void Main(string[] args) { - // Create a list of Animal objects, add Dog and Cat instances, and call their methods + // Create instances of Dog and Cat + Dog dogSound = new Dog(); + Cat catSound = new Cat(); + + // Call the MakeSound method for both objects + dogSound.MakeSound(); + catSound.MakeSound(); + + // Call the Move method for both objects + dogSound.Move(); + catSound.Move(); } } } \ No newline at end of file diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/IRepository.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/IRepository.cs new file mode 100644 index 00000000..d227067d --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/IRepository.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem +{ + public interface IRepository + { + T GetById(int id); + + IEnumerable GetAll(); + + void Add(T entity); + + void Update(T entity); + + void Delete(T entity); + } +} diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/IVehicle.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/IVehicle.cs new file mode 100644 index 00000000..bc32cdb7 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/IVehicle.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem +{ + // Define an interface for vehicles. + public interface IVehicle + { + void Drive(); + } + + // Define a class representing a Car, which implements the IVehicle interface. + public class Car : IVehicle + { + // Implement the Drive method for the Car class. + public void Drive() + { + Console.WriteLine("Driving Car."); + } + } + + // Define a class representing a Truck, which implements the IVehicle interface. + public class Truck : IVehicle + { + // Implement the Drive method for the Truck class. + public void Drive() + { + Console.WriteLine("Driving Truck."); + } + } +} diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs index 77395b3a..1ffd9b80 100644 --- a/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs @@ -1,10 +1,68 @@ namespace VehicleManagementSystem { - internal class Program + public class Program { static void Main(string[] args) { - Console.WriteLine("Hello, World!"); + // Prompt the user to input a vehicle type + Console.WriteLine("1. Factory Design Method."); + Console.Write("Enter a vehicle type car or truck: "); + string? vehicleType = Console.ReadLine(); + // Retrieve the appropriate vehicle factory based on user input + VehicleFactory vehicleFactory = GetVehicleFactory(vehicleType); + + if (vehicleFactory != null) + { + // Use the factory to create a vehicle and perform a drive action + IVehicle vehicle = vehicleFactory.CreateVehicle(); + Console.WriteLine("\nVehicle created:"); + vehicle.Drive(); + } + else + { + Console.WriteLine("Invalid vehicle type."); + } + + Console.WriteLine("\n2.Repository CRUD operation.\n"); + + // Create instances of VehicleRepository and VehicleLogger + VehicleRepository vehicleRepository = new VehicleRepository(); + VehicleLogger vehicleLogger = VehicleLogger.Instance; + + // Create a VehicleService and associate it with the repository and logger + VehicleService vehicleService = new VehicleService(vehicleRepository, vehicleLogger); + + // Create instances of CarFactory and TruckFactory + CarFactory carFactory = new CarFactory(); + TruckFactory truckFactory = new TruckFactory(); + + // List vehicles and demonstrate adding/removing vehicles + vehicleService.AddVehicle(carFactory); + vehicleService.AddVehicle(carFactory); + vehicleService.AddVehicle(truckFactory); + + vehicleService.ListVehicles(); + vehicleService.RemoveVehicle(1); + vehicleService.AddVehicle(truckFactory); + vehicleService.ListVehicles(); + + // Demonstrate performing actions on vehicles + vehicleService.DoSomethingWithVehicle(1); + vehicleService.DoSomethingWithVehicle(2); + } + + // Helper function to get the appropriate vehicle factory based on input + static VehicleFactory GetVehicleFactory(string vehicleType) + { + switch (vehicleType.ToLower()) + { + case "car": + return new CarFactory(); + case "truck": + return new TruckFactory(); + default: + return null; + } } } } \ No newline at end of file diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleFactory.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleFactory.cs new file mode 100644 index 00000000..2da1b359 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleFactory.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem +{ + // Abstract factory class that defines a method for creating vehicles + public abstract class VehicleFactory + { + // Abstract method for creating a vehicle + public abstract IVehicle CreateVehicle(); + + // Method that demonstrates using the created vehicle + public void DoSomethingWithVehicle() + { + // Create a vehicle using the factory method + IVehicle vehicle = CreateVehicle(); + + // Drive the created vehicle + vehicle.Drive(); + } + } + + // Concrete factory class that creates cars + public class CarFactory : VehicleFactory + { + // Override the CreateVehicle method to create a car + public override IVehicle CreateVehicle() + { + return new Car(); + } + } + + // Concrete factory class that creates trucks + public class TruckFactory : VehicleFactory + { + // Override the CreateVehicle method to create a truck + public override IVehicle CreateVehicle() + { + return new Truck(); + } + } +} diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleLogger.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleLogger.cs new file mode 100644 index 00000000..42b13400 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleLogger.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem +{ + // Define a class for logging vehicle-related messages + public class VehicleLogger + { + // Private constructor to prevent external instantiation + private VehicleLogger() { } + + // Create an object for Thread Safety to use double check locking + private static readonly object locker = new object(); + + // Create a private instance variable for the singleton pattern + private static VehicleLogger? instance; + + // Define a public property to access the singleton instance + public static VehicleLogger Instance + { + get + { + // Thread Safety Singleton using Double-Check Locking + if (instance == null) + { + lock (locker) + { + // If instance is still null, create a new instance + if (instance == null) + { + instance = new VehicleLogger(); + } + } + } + // Return the singleton instance + return instance; + } + } + + // Method for logging vehicle-related messages + public void Log(string message) + { + Console.WriteLine($"Vehicle Logging: {message}"); + } + } +} diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleRepository.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleRepository.cs new file mode 100644 index 00000000..b3cc1c99 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleRepository.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem +{ + // This class implements the IRepository interface for IVehicle objects. + public class VehicleRepository : IRepository + { + // Private dictionary to store vehicles with their unique IDs. + private Dictionary vehicles = new Dictionary(); + + // Used to generate the next available ID for new vehicles. + private int nextId = 1; + + // Adds a new vehicle to the repository. + public void Add(IVehicle entity) + { + int id = nextId++; + vehicles[id] = entity; + } + + // Deletes a vehicle from the repository. + public void Delete(IVehicle entity) + { + // Find the key-value pair associated with the specified entity. + var item = vehicles.FirstOrDefault(v => v.Value == entity); + + // If the entity is found, remove it from the dictionary. + if (item.Value != null) + { + vehicles.Remove(item.Key); + } + } + + // Retrieves all vehicles from the repository. + public IEnumerable GetAll() + { + return vehicles.Values; + } + + // Retrieves a vehicle by its ID from the repository. + public IVehicle GetById(int id) + { + // Try to get the vehicle associated with the given ID. + vehicles.TryGetValue(id, out IVehicle vehicle); + return vehicle; + } + + // Updates a vehicle in the repository (not implemented in this version). + public void Update(IVehicle entity) + { + throw new NotImplementedException(); + } + } +} diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleService.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleService.cs new file mode 100644 index 00000000..f78b59d1 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleService.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem +{ + // The VehicleService class handles operations related to vehicles using a repository and logger. + public class VehicleService + { + private readonly IRepository _vehicleRepository; // Repository to store vehicles. + private VehicleLogger _vehicleLogger; // Logger to record vehicle-related actions. + + // Constructor for VehicleService. + public VehicleService(IRepository vehicleRepository, VehicleLogger vehicleLogger) + { + _vehicleRepository = vehicleRepository; + _vehicleLogger = vehicleLogger; + } + + // Adds a vehicle to the repository using a factory and logs the action. + public void AddVehicle(VehicleFactory factory) + { + IVehicle vehicle = factory.CreateVehicle(); + _vehicleRepository.Add(vehicle); + _vehicleLogger.Log("Vehicle added successfully"); + } + + // Removes a vehicle from the repository by ID and logs the action. + public void RemoveVehicle(int id) + { + IVehicle vehicle = _vehicleRepository.GetById(id); + if (vehicle != null) + { + _vehicleRepository.Delete(vehicle); + _vehicleLogger.Log("Vehicle Removed Successfully."); + } + else + { + _vehicleLogger.Log("Vehicle Not Found."); + } + } + + // Lists all vehicles in the repository and invokes the Drive method for each vehicle. + public void ListVehicles() + { + foreach (IVehicle vehicle in _vehicleRepository.GetAll()) + { + vehicle.Drive(); + } + } + + // Performs an action on a vehicle by ID, logs the action, and invokes the Drive method. + public void DoSomethingWithVehicle(int id) + { + IVehicle vehicle = _vehicleRepository.GetById(id); + if (vehicle != null) + { + vehicle.Drive(); + _vehicleLogger.Log("Something happened with the vehicle. Drive Slowly"); + } + } + } +}