diff --git a/week-1/day-2/exercise-1/Circle/Circle.cs b/week-1/day-2/exercise-1/Circle/Circle.cs index 065ffa5d..9d9e2fd1 100644 --- a/week-1/day-2/exercise-1/Circle/Circle.cs +++ b/week-1/day-2/exercise-1/Circle/Circle.cs @@ -9,5 +9,22 @@ namespace Circle internal class Circle { // Implement the Circle class here + public double Radius { get; private set; } + + public Circle(double radius) + { + Radius = radius; + } + + public double GetArea() + { + return Math.PI * Math.Pow(Radius, 2); + } + + public double GetCircumference() + { + return 2 * Math.PI * Radius; + } + } } diff --git a/week-1/day-2/exercise-1/Circle/Program.cs b/week-1/day-2/exercise-1/Circle/Program.cs index 0df91b0d..d7a5f941 100644 --- a/week-1/day-2/exercise-1/Circle/Program.cs +++ b/week-1/day-2/exercise-1/Circle/Program.cs @@ -4,7 +4,17 @@ internal class Program { static void Main(string[] args) { - // Create a Circle object and display its area and circumference + // Create a Circle object with a given radius + double radius = 5.0; + Circle circle = new Circle(radius); + + // Calculate and display the area + double area = circle.GetArea(); + Console.WriteLine($"Area of the circle with radius {radius}: {area}"); + + // Calculate and display the circumference + double circumference = circle.GetCircumference(); + Console.WriteLine($"Circumference of the circle with radius {radius}: {circumference}"); } } } \ No newline at end of file diff --git a/week-1/day-2/exercise-2/BankAccount/Program.cs b/week-1/day-2/exercise-2/BankAccount/Program.cs index c58c30e2..84f485ad 100644 --- a/week-1/day-2/exercise-2/BankAccount/Program.cs +++ b/week-1/day-2/exercise-2/BankAccount/Program.cs @@ -1,10 +1,90 @@ -namespace BankAccount +using System; + +public abstract class BankAccount { - internal class Program + public string AccountNumber { get; protected set; } + public double Balance { get; protected set; } + + public BankAccount(string accountNumber, double initialBalance) { - static void Main(string[] args) + AccountNumber = accountNumber; + Balance = initialBalance; + } + + public abstract void Deposit(double amount); + public abstract void Withdraw(double amount); +} + +public class SavingsAccount : BankAccount +{ + public double InterestRate { get; private set; } + + public SavingsAccount(string accountNumber, double initialBalance, double interestRate) + : base(accountNumber, initialBalance) + { + InterestRate = interestRate; + } + + public override void Deposit(double amount) + { + Balance += amount; + } + + public override void Withdraw(double amount) + { + if (Balance >= amount) + { + Balance -= amount; + } + else { - // Create SavingsAccount and CheckingAccount objects and perform operations + Console.WriteLine("Insufficient balance."); } } -} \ No newline at end of file +} + +public class CheckingAccount : BankAccount +{ + public double OverdraftLimit { get; private set; } + + public CheckingAccount(string accountNumber, double initialBalance, double overdraftLimit) + : base(accountNumber, initialBalance) + { + OverdraftLimit = overdraftLimit; + } + + public override void Deposit(double amount) + { + Balance += amount; + } + + public override void Withdraw(double amount) + { + if (Balance + OverdraftLimit >= amount) + { + Balance -= amount; + } + else + { + Console.WriteLine("Insufficient balance."); + } + } +} + +class Program +{ + static void Main(string[] args) + { + SavingsAccount savingsAccount = new SavingsAccount("SA123", 1000, 0.05); + CheckingAccount checkingAccount = new CheckingAccount("CA456", 500, 200); + + savingsAccount.Deposit(500); + savingsAccount.Withdraw(200); + + checkingAccount.Deposit(300); + checkingAccount.Withdraw(800); + + Console.WriteLine($"Savings Account Balance: {savingsAccount.Balance}"); + Console.WriteLine($"Checking Account Balance: {checkingAccount.Balance}"); + } +} diff --git a/week-1/day-2/exercise-3/AnimalExercise/Program.cs b/week-1/day-2/exercise-3/AnimalExercise/Program.cs index 5928b7cb..756e8b2c 100644 --- a/week-1/day-2/exercise-3/AnimalExercise/Program.cs +++ b/week-1/day-2/exercise-3/AnimalExercise/Program.cs @@ -1,10 +1,66 @@ -namespace AnimalExercise +using System; +using System.Collections.Generic; + +public abstract class Animal { - internal class Program + public string Name { get; set; } + public int Age { get; set; } + + public abstract void MakeSound(); +} + +public class Dog : Animal, IMovable +{ + public override void MakeSound() + { + Console.WriteLine("Woof"); + } + + public void Move() { - static void Main(string[] args) + Console.WriteLine("Dog is running."); + } +} + +public class Cat : Animal, IMovable +{ + public override void MakeSound() + { + Console.WriteLine("Meow"); + } + + public void Move() + { + Console.WriteLine("Cat is sneaking."); + } +} + +public interface IMovable +{ + void Move(); +} + +class Program +{ + static void Main(string[] args) + { + List animals = new List + { + new Dog { Name = "Buddy", Age = 3 }, + new Cat { Name = "Whiskers", Age = 2 } + }; + + foreach (var animal in animals) { - // Create a list of Animal objects, add Dog and Cat instances, and call their methods + Console.WriteLine($"Name: {animal.Name}, Age: {animal.Age}"); + animal.MakeSound(); + + if (animal is IMovable movableAnimal) + { + movableAnimal.Move(); + } + + Console.WriteLine(); } } -} \ No newline at end of file +} diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs index 77395b3a..add6361c 100644 --- a/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs @@ -1,10 +1,198 @@ -namespace VehicleManagementSystem +using System; +using System.Collections.Generic; + +// 1. IVehicle interface +public interface IVehicle { - internal class Program + void Drive(); +} + +// 2. Car and Truck classes +public class Car : IVehicle +{ + public void Drive() + { + Console.WriteLine("Car is driving."); + } +} + +public class Truck : IVehicle +{ + public void Drive() + { + Console.WriteLine("Truck is driving."); + } +} + +// 3. Singleton VehicleLogger class +public class VehicleLogger +{ + private static VehicleLogger instance; + private VehicleLogger() { } + + public static VehicleLogger Instance + { + get + { + if (instance == null) + { + instance = new VehicleLogger(); + } + return instance; + } + } + + public void Log(string message) + { + Console.WriteLine($"[Log] {message}"); + } +} + +// 4. Abstract VehicleFactory class +public abstract class VehicleFactory +{ + public abstract IVehicle CreateVehicle(); + + public void DoSomethingWithVehicle() + { + IVehicle vehicle = CreateVehicle(); + vehicle.Drive(); + } +} + +// 5. Concrete factory classes +public class CarFactory : VehicleFactory +{ + public override IVehicle CreateVehicle() + { + return new Car(); + } +} + +public class TruckFactory : VehicleFactory +{ + public override IVehicle CreateVehicle() + { + return new Truck(); + } +} + +// 6. IRepository interface +public interface IRepository +{ + T GetById(int id); + IEnumerable GetAll(); + void Add(T entity); + void Update(T entity); + void Delete(T entity); +} + +// 7. VehicleRepository class +public class VehicleRepository : IRepository +{ + private List vehicles = new List(); + + public IVehicle GetById(int id) + { + return vehicles.FirstOrDefault(v => v.GetHashCode() == id); + } + + public IEnumerable GetAll() + { + return vehicles; + } + + public void Add(IVehicle entity) + { + vehicles.Add(entity); + } + + public void Update(IVehicle entity) + { + // Implement update logic + } + + public void Delete(IVehicle entity) + { + vehicles.Remove(entity); + } +} + +// 8. VehicleService class +public class VehicleService +{ + private IRepository repository; + private VehicleLogger logger; + + public VehicleService(IRepository repository) + { + this.repository = repository; + logger = VehicleLogger.Instance; + } + + public void AddVehicle(VehicleFactory factory) { - static void Main(string[] args) + IVehicle vehicle = factory.CreateVehicle(); + repository.Add(vehicle); + logger.Log($"Added a new {vehicle.GetType().Name}"); + } + + public void RemoveVehicle(int id) + { + IVehicle vehicle = repository.GetById(id); + if (vehicle != null) + { + repository.Delete(vehicle); + logger.Log($"Removed {vehicle.GetType().Name}"); + } + else { - Console.WriteLine("Hello, World!"); + logger.Log("Vehicle not found."); } } -} \ No newline at end of file + + public void ListVehicles() + { + foreach (var vehicle in repository.GetAll()) + { + Console.WriteLine($"Vehicle: {vehicle.GetType().Name}"); + } + } + + public void DoSomethingWithVehicle(int id) + { + IVehicle vehicle = repository.GetById(id); + if (vehicle != null) + { + vehicle.Drive(); + logger.Log($"Performed action on {vehicle.GetType().Name}"); + } + else + { + logger.Log("Vehicle not found."); + } + } +} + +class Program +{ + static void Main(string[] args) + { + IRepository repository = new VehicleRepository(); + VehicleService service = new VehicleService(repository); + + VehicleFactory carFactory = new CarFactory(); + VehicleFactory truckFactory = new TruckFactory(); + + service.AddVehicle(carFactory); + service.AddVehicle(truckFactory); + + service.ListVehicles(); + + service.DoSomethingWithVehicle(1); + service.DoSomethingWithVehicle(2); + + service.RemoveVehicle(1); + service.ListVehicles(); + } +} diff --git a/week-1/day-3/exercise-1/FactorialApp/Program.cs b/week-1/day-3/exercise-1/FactorialApp/Program.cs index f116eeca..aced81db 100644 --- a/week-1/day-3/exercise-1/FactorialApp/Program.cs +++ b/week-1/day-3/exercise-1/FactorialApp/Program.cs @@ -12,9 +12,20 @@ public static void Main() Console.WriteLine($"The factorial of {number} is: {factorial}"); } - public static long CalculateFactorial(int number) + public static long CalculateFactorial(int n ) { - throw new NotImplementedException(); + if (n == 0 || n == 1) + { + return 1; + } + + long result = 1; + for (int i = 2; i <= n; i++) + { + result *= i; + } + + return result; } } } \ No newline at end of file diff --git a/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs b/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs index a4ec62d9..8c02d7d6 100644 --- a/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs +++ b/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs @@ -223,7 +223,7 @@ public int[] DijkstraShortestPath(int source) visited[i] = false; } - distances[source] = int.MinValue; + distances[source] = 0; for (int count = 0; count < vertices - 1; count++) {