From 38d7d545530773977874aa8bb586ae89ee16a0ba Mon Sep 17 00:00:00 2001 From: Vipul Date: Wed, 9 Aug 2023 13:20:38 +0530 Subject: [PATCH 01/14] Add Cirle class --- week-1/day-2/exercise-1/Circle/Circle.cs | 15 +++++++++++- week-1/day-2/exercise-1/Circle/Program.cs | 29 ++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/week-1/day-2/exercise-1/Circle/Circle.cs b/week-1/day-2/exercise-1/Circle/Circle.cs index 065ffa5d..449d0d00 100644 --- a/week-1/day-2/exercise-1/Circle/Circle.cs +++ b/week-1/day-2/exercise-1/Circle/Circle.cs @@ -6,8 +6,21 @@ namespace Circle { - internal class Circle + public class Circle { // Implement the Circle class here + private readonly double _radius; + public Circle(double Radius) + { + _radius = Radius; + } + public double GetArea() + { + return Math.Round(Math.PI * Math.Pow(_radius, 2), 4); + } + public double GetCircumference() + { + return Math.Round(2 * Math.PI + _radius, 4); + } } } diff --git a/week-1/day-2/exercise-1/Circle/Program.cs b/week-1/day-2/exercise-1/Circle/Program.cs index 0df91b0d..535ad748 100644 --- a/week-1/day-2/exercise-1/Circle/Program.cs +++ b/week-1/day-2/exercise-1/Circle/Program.cs @@ -1,10 +1,37 @@ namespace Circle { - internal class Program + public class Program { static void Main(string[] args) { // Create a Circle object and display its area and circumference + double Radius; + bool FirstIteration = true; + do + { + if(!FirstIteration) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Please enter valid number !!!\n"); + Console.ForegroundColor = ConsoleColor.White; + } + Console.Write("Please Enter radius : "); + FirstIteration = false; + + } while (!double.TryParse(Console.ReadLine(), out Radius)); + + // Create instance of Circle class + Circle circleObj = new(Radius); + + // call GetArea() method that calculate area + double Area = circleObj.GetArea(); + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"Area of Circle is : {Area}"); + + // call GetArea() method that calculate circumference + double Circumference = circleObj.GetCircumference(); + Console.WriteLine($"Circumference of Cirxle is : {Circumference}"); + Console.ForegroundColor = ConsoleColor.White; } } } \ No newline at end of file From 5b1a3ff717a76b612163680ea0f1e6501306e13e Mon Sep 17 00:00:00 2001 From: Vipul Date: Wed, 9 Aug 2023 13:28:19 +0530 Subject: [PATCH 02/14] Add exercise 2 --- .../exercise-2/BankAccount/BankAccount.cs | 98 +++++++++++++++ .../day-2/exercise-2/BankAccount/Program.cs | 114 +++++++++++++++++- 2 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 week-1/day-2/exercise-2/BankAccount/BankAccount.cs 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..3a538c25 --- /dev/null +++ b/week-1/day-2/exercise-2/BankAccount/BankAccount.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankAccount +{ + public abstract class BankAccount + { + protected Int64 _accountNumber { get; set; } + protected double _balance { get; set; } + public abstract void Deposit(double amount); + public abstract void Withdraw(double amount); + } + public class SavingsAccount : BankAccount + { + public double InterestRate { get; private set; } + public SavingsAccount(Int64 accountNumber, double initialBalance, double interestRate) + { + _accountNumber = accountNumber; + _balance = initialBalance; + InterestRate = interestRate; + } + public override void Deposit(double amount) + { + _balance += amount; + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"Successfully Deposited {amount} into Savings Account."); + Console.WriteLine($"Current balance : {_balance}"); + Console.ForegroundColor = ConsoleColor.White; + } + + + + public override void Withdraw(double amount) + { + if (amount > _balance) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Insufficient balance in Saving Account."); + Console.ForegroundColor = ConsoleColor.White; + } + else + { + Console.ForegroundColor = ConsoleColor.Green; + _balance -= amount; + Console.WriteLine($"Your Account number is : {_accountNumber}"); + Console.WriteLine($"{amount} is withdrawn from {_accountNumber}"); + Console.WriteLine($"Your current balance is : {_balance}"); + Console.ForegroundColor = ConsoleColor.White; + } + } + } + public class CheckingAccount : BankAccount + { + public double OverdraftLimit { get; private set; } + + public CheckingAccount(Int64 accountNumber, double initialBalance, double overdraftLimit) + { + _accountNumber = accountNumber; + _balance = initialBalance; + OverdraftLimit = overdraftLimit; + } + public override void Deposit(double amount) + { + _balance += amount; + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"Successfully Deposited {amount} into Checking Account."); + Console.WriteLine($"Current balance: {_balance}"); + Console.ForegroundColor = ConsoleColor.White; + } + + public override void Withdraw(double amount) + { + if (amount > _balance) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Insufficient balance in Checking Account."); + Console.ForegroundColor = ConsoleColor.White; + } + else if (amount > OverdraftLimit) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine($"You can't withdraw more than {OverdraftLimit} from Checking Account."); + Console.ForegroundColor = ConsoleColor.White; + } + else + { + _balance -= amount; + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine($"Withdrew {amount} from Checking Account."); + Console.WriteLine($"Current balance: {_balance}"); + Console.ForegroundColor = ConsoleColor.White; + } + } + } +} diff --git a/week-1/day-2/exercise-2/BankAccount/Program.cs b/week-1/day-2/exercise-2/BankAccount/Program.cs index c58c30e2..df07192a 100644 --- a/week-1/day-2/exercise-2/BankAccount/Program.cs +++ b/week-1/day-2/exercise-2/BankAccount/Program.cs @@ -1,10 +1,122 @@ namespace BankAccount { - internal class Program + + public class Program { static void Main(string[] args) { // Create SavingsAccount and CheckingAccount objects and perform operations + Console.WriteLine("Enter 1 => Saving Account"); + Console.WriteLine("Enter 2 => Checking Account"); + bool isFirstIteration = true; + int YourChoice = 0; + do + { + if (!isFirstIteration && YourChoice != 1 && YourChoice != 2) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.Write("Please Enter Valid Account Choice !!!\n"); + Console.ForegroundColor = ConsoleColor.White; + } + Console.ForegroundColor = ConsoleColor.Green; + Console.Write("Please Your Account Choice : "); + Console.ForegroundColor = ConsoleColor.White; + + isFirstIteration = false; + + } while (!int.TryParse(Console.ReadLine(), out YourChoice) || (YourChoice != 1 && YourChoice != 2)); + + if (YourChoice == 1) + { + SavingsAccount savingsAccount = new(782378, 5000, 0.05); + Console.WriteLine("Enter 1 => Deposit"); + Console.WriteLine("Enter 2 => Withdraw"); + + int Choice = TansactionChoiceInput(); + double Amount = TakeInputAmount(); + if (Choice == 1) + { + Console.ForegroundColor = ConsoleColor.DarkCyan; + Console.WriteLine("Please wait ......"); + Thread.Sleep(2000); + Console.ForegroundColor = ConsoleColor.White; + savingsAccount.Deposit(Amount); + } + else + { + Console.ForegroundColor = ConsoleColor.DarkCyan; + Console.WriteLine("Please wait ......"); + Thread.Sleep(2000); + Console.ForegroundColor = ConsoleColor.White; + savingsAccount.Withdraw(Amount); + } + } + else + { + CheckingAccount checkingAccount = new(782378, 5000, 3000); + Console.WriteLine("Enter 1 => Deposit"); + Console.WriteLine("Enter 2 => Withdraw"); + + int Choice = TansactionChoiceInput(); + double Amount = TakeInputAmount(); + if (Choice == 1) + { + Console.ForegroundColor = ConsoleColor.DarkCyan; + Console.WriteLine("Please wait ......"); + Thread.Sleep(2000); + Console.ForegroundColor = ConsoleColor.White; + checkingAccount.Deposit(Amount); + } + else + { + Console.ForegroundColor = ConsoleColor.DarkCyan; + Console.WriteLine("Please wait ......"); + Thread.Sleep(2000); + Console.ForegroundColor = ConsoleColor.White; + checkingAccount.Withdraw(Amount); + } + } + } + private static double TakeInputAmount() + { + double amount = 0; + bool isFirstIteration = true; + do + { + if (!isFirstIteration && amount == 0) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.Write("Please Enter Correct amount !!!\n"); + Console.ForegroundColor = ConsoleColor.White; + } + Console.ForegroundColor = ConsoleColor.Green; + Console.Write("Please Enter Amount : "); + Console.ForegroundColor = ConsoleColor.White; + isFirstIteration = false; + + } while (!double.TryParse(Console.ReadLine(), out amount)); + return amount; + } + + private static int TansactionChoiceInput() + { + int TransactionChoice = 0; + bool isFirstIteration = true; + do + { + if (!isFirstIteration && TransactionChoice != 1 && TransactionChoice != 2) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.Write("Please Enter Valid Transaction Choice !!!\n"); + Console.ForegroundColor = ConsoleColor.White; + } + Console.ForegroundColor = ConsoleColor.Green; + Console.Write("Please Enter Transaction Choice : "); + Console.ForegroundColor = ConsoleColor.White; + isFirstIteration = false; + + } while (!int.TryParse(Console.ReadLine(), out TransactionChoice) || (TransactionChoice != 1 && TransactionChoice != 2)); + return TransactionChoice; } } } \ No newline at end of file From 59be23df96845122ed7c18207ecc273f99537ac3 Mon Sep 17 00:00:00 2001 From: Vipul Date: Wed, 9 Aug 2023 13:32:37 +0530 Subject: [PATCH 03/14] Add exercise-3 --- .../day-2/exercise-3/AnimalExercise/Animal.cs | 42 +++++++++++++++++++ .../AnimalExercise/Interfaces/IMovable.cs | 13 ++++++ .../exercise-3/AnimalExercise/Program.cs | 12 +++++- 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 week-1/day-2/exercise-3/AnimalExercise/Animal.cs create mode 100644 week-1/day-2/exercise-3/AnimalExercise/Interfaces/IMovable.cs 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..514ec9bf --- /dev/null +++ b/week-1/day-2/exercise-3/AnimalExercise/Animal.cs @@ -0,0 +1,42 @@ +using AnimalExercise.Interfaces; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AnimalExercise +{ + public abstract class Animal + { + public string? Name { get; set; } + public int Age { get; set; } + + public abstract void MakeSound(); + } + class Cat : Animal, IMovable + { + public override void MakeSound() + { + Console.WriteLine("Meow"); + } + + public void Move() + { + Console.WriteLine("Cat is climbing."); + } + } + + public class Dog : Animal, IMovable + { + public override void MakeSound() + { + Console.WriteLine("Woof"); + } + + public void Move() + { + Console.WriteLine("Dog is moving."); + } + } +} diff --git a/week-1/day-2/exercise-3/AnimalExercise/Interfaces/IMovable.cs b/week-1/day-2/exercise-3/AnimalExercise/Interfaces/IMovable.cs new file mode 100644 index 00000000..498635ba --- /dev/null +++ b/week-1/day-2/exercise-3/AnimalExercise/Interfaces/IMovable.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AnimalExercise.Interfaces +{ + public interface IMovable + { + void Move(); + } +} diff --git a/week-1/day-2/exercise-3/AnimalExercise/Program.cs b/week-1/day-2/exercise-3/AnimalExercise/Program.cs index 5928b7cb..0ec22a18 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 + List animals = new List + { + new Dog { Name = "Tommy", Age = 5 }, + new Cat { Name = "Jelly", Age = 2 } + }; + foreach (var animal in animals) + { + Console.WriteLine($"Name: {animal.Name}, Age: {animal.Age}"); + animal.MakeSound(); + } } } } \ No newline at end of file From 474a5435d0e6b2c1847cd4ec9e1c2181a38b2127 Mon Sep 17 00:00:00 2001 From: Vipul Date: Wed, 9 Aug 2023 13:42:22 +0530 Subject: [PATCH 04/14] Add exercise-4 --- .../exercise-4/VehicleManagementSystem/Car.cs | 18 ++++ .../Interfaces/IVehicle.cs | 14 ++++ .../Interfaces/IVehicleRepository.cs | 38 +++++++++ .../VehicleManagementSystem/Models/Vehicle.cs | 14 ++++ .../VehicleManagementSystem/Program.cs | 41 +++++++++- .../VehicleManagementSystem/Truck.cs | 18 ++++ .../VehicleManagementSystem/VehicleFactory.cs | 41 ++++++++++ .../VehicleManagementSystem/VehicleLogger.cs | 42 ++++++++++ .../VehicleRepository.cs | 69 ++++++++++++++++ .../VehicleManagementSystem/VehicleService.cs | 82 +++++++++++++++++++ 10 files changed, 374 insertions(+), 3 deletions(-) create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/Car.cs create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/Interfaces/IVehicle.cs create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/Interfaces/IVehicleRepository.cs create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/Models/Vehicle.cs create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/Truck.cs create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/VehicleFactory.cs create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/VehicleLogger.cs create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/VehicleRepository.cs create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/VehicleService.cs diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/Car.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/Car.cs new file mode 100644 index 00000000..87024417 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/Car.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VehicleManagementSystem.Interfaces; +using VehicleManagementSystem.Models; + +namespace VehicleManagementSystem +{ + public class Car : Vehicle, IVehicle + { + public void Drive() + { + Console.WriteLine("Car is Moving"); + } + } +} diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/Interfaces/IVehicle.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/Interfaces/IVehicle.cs new file mode 100644 index 00000000..eaf19dd4 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/Interfaces/IVehicle.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem.Interfaces +{ + public interface IVehicle + { + void Drive(); + + } +} diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/Interfaces/IVehicleRepository.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/Interfaces/IVehicleRepository.cs new file mode 100644 index 00000000..4e97589f --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/Interfaces/IVehicleRepository.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem.Interfaces +{ + public interface IVehicleRepository + { + /// + /// It return a vehicle + /// + /// + /// + T GetById(int id); + /// + /// It return all the vehicle + /// + /// + IEnumerable GetAll(); + /// + /// It add vehicle into List + /// + /// + void Add(T vehicle); + /// + /// It Update the vehicle + /// + /// + void Update(T vehicle); + /// + /// It delete the vehicle from list + /// + /// + void Delete(T vehicle); + } +} diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/Models/Vehicle.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/Models/Vehicle.cs new file mode 100644 index 00000000..102ef75b --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/Models/Vehicle.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem.Models +{ + public class Vehicle + { + public int Id { get; set; } + public string? Name { get; set; } + } +} diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs index 77395b3a..0021205a 100644 --- a/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs @@ -1,10 +1,45 @@ -namespace VehicleManagementSystem +using System.Diagnostics; +using VehicleManagementSystem.Interfaces; +using VehicleManagementSystem.Models; + +namespace VehicleManagementSystem { - internal class Program + public class Program { static void Main(string[] args) { - Console.WriteLine("Hello, World!"); + // Factory method call using Factory Design pattern + VehicleFactory carFactory = new CarFactory(); + carFactory.DoSomethingWithVehicle(); + + VehicleFactory truckFactory = new TruckFactory(); + truckFactory.DoSomethingWithVehicle(); + + // Create an instance of the VehicleRepository implementing IRepository + IVehicleRepository repository = new VehicleRepository(); + + // Create a VehicleService instance using the VehicleRepository and VehicleLogger + VehicleService service = new VehicleService(repository, VehicleLogger.Instance); + + // Add vehicles + service.AddVehicle(new Car { Name = "Honda" }); + service.AddVehicle(new Truck { Name = "TATA" }); + + // List vehicles + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine("\n------------- List of vehicles --------------\n"); + Console.ForegroundColor = ConsoleColor.White; + service.ListVehicles(); + service.DoSomethingWithVehicle(1); + + //Remove Vehicle by Id + service.RemoveVehicle(2); + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine("\n------------- List of vehicles --------------\n"); + Console.ForegroundColor = ConsoleColor.White; + + // List Vehicle + service.ListVehicles(); } } } \ No newline at end of file diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/Truck.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/Truck.cs new file mode 100644 index 00000000..a3745247 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/Truck.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VehicleManagementSystem.Interfaces; +using VehicleManagementSystem.Models; + +namespace VehicleManagementSystem +{ + public class Truck : Vehicle, IVehicle + { + public void Drive() + { + Console.WriteLine("Truck is moving"); + } + } +} 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..5c157956 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleFactory.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VehicleManagementSystem.Interfaces; + +namespace VehicleManagementSystem +{ + public abstract class VehicleFactory + { + // abstract method for creating vehicle + public abstract IVehicle CreateVehicle(); + + public void DoSomethingWithVehicle() + { + IVehicle vehicle = CreateVehicle(); + Console.WriteLine("Doing something with the vehicle"); + // call the drive method on the basis of created vehicle + vehicle.Drive(); + } + } + + public class CarFactory : VehicleFactory + { + // override the Vehiclefactory Method and create a Car instance + public override IVehicle CreateVehicle() + { + return new Car(); + } + } + + public class TruckFactory : VehicleFactory + { + // override the Vehiclefactory Method and create a Truck instance + 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..4cdb6049 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleLogger.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem +{ + public sealed class VehicleLogger + { + private static VehicleLogger? _instance; + private static readonly object lockObject = new object(); + + // Private constructor to prevent instantiation + private VehicleLogger() { } + + public static VehicleLogger Instance + { + get + { + // Double-check locking to ensure thread safety + if (_instance == null) + { + // It handle the singleton instance in a multithread environment + lock (lockObject) + { + if (_instance == null) + { + _instance = new VehicleLogger(); + } + } + } + return _instance; + } + } + + public void Log(string message) + { + Console.WriteLine($"Log: {DateTime.Now} : {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..55222dd1 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleRepository.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VehicleManagementSystem.Interfaces; +using VehicleManagementSystem.Models; + +namespace VehicleManagementSystem +{ + class VehicleRepository : IVehicleRepository + { + private List vehicles = new List(); + // counter for generating unique Id + private int nextId = 1; + + /// + /// retrive a vehicle by it's id + /// + /// + /// + public Vehicle GetById(int id) + { + return vehicles.Find(v => v.Id == id)!; + } + + /// + /// It Retrieve all vehicles from list + /// + /// + public IEnumerable GetAll() + { + return vehicles; + } + + /// + /// It Add Vehicle to list + /// + /// + public void Add(Vehicle vehicle) + { + vehicle.Id = nextId++; + vehicles.Add(vehicle); + } + + /// + /// It Update the Vehicle + /// + /// + public void Update(Vehicle vehicle) + { + int index = vehicles.FindIndex(v => v.Id == vehicle.Id); + if (index >= 0) + { + // It replace the existing vehicle with the updated vehicle + vehicles[index] = vehicle; + } + } + + /// + /// It delete a vehicle from the repository + /// + /// + public void Delete(Vehicle vehicle) + { + vehicles.Remove(vehicle); + } + } +} 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..15240213 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleService.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VehicleManagementSystem.Interfaces; +using VehicleManagementSystem.Models; + +namespace VehicleManagementSystem +{ + public class VehicleService + { + private readonly IVehicleRepository vehicleRepository; + private readonly VehicleLogger _logger; + + // it initialize the VehicleService with a repository and logger + public VehicleService(IVehicleRepository repository, VehicleLogger logger) + { + vehicleRepository = repository; + _logger = logger; + } + + /// + /// It add a vehicle to the repository + /// + /// + public void AddVehicle(Vehicle vehicle) + { + vehicleRepository.Add(vehicle); + Console.ForegroundColor = ConsoleColor.Green; + _logger.Log($"Added a {vehicle.Name} with Id {vehicle.Id}"); + Console.ForegroundColor = ConsoleColor.White; + } + + /// + /// It remove a vehicle from the repository + /// + /// + public void RemoveVehicle(int id) + { + var vehicle = vehicleRepository.GetById(id); + if (vehicle != null) + { + vehicleRepository.Delete(vehicle); + Console.ForegroundColor = ConsoleColor.Green; + _logger.Log($"Removed vehicle with Id {id}"); + Console.ForegroundColor = ConsoleColor.White; + } + } + + /// + /// It log the list of vehicles + /// + public void ListVehicles() + { + _logger.Log("List of Vehicles:"); + foreach (var vehicle in vehicleRepository.GetAll()) + { + + _logger.Log($"ID: {vehicle.Id}, Name: {vehicle.Name}"); + + } + } + + /// + /// It perform an action on a specific vehicle (car/truck) + /// + /// + public void DoSomethingWithVehicle(int id) + { + var vehicle = vehicleRepository.GetById(id); + if (vehicle != null) + { + _logger.Log($"Performing some action on vehicle with Id {id}"); + } + else + { + _logger.Log($"Vehicle with Id {id} not found."); + } + } + } +} From c65f13664aee5cc1195318249732c7d4fea5a398 Mon Sep 17 00:00:00 2001 From: Vipul Date: Wed, 9 Aug 2023 15:19:39 +0530 Subject: [PATCH 05/14] Add exercise-1 --- .../day-3/exercise-1/FactorialApp/Program.cs | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/week-1/day-3/exercise-1/FactorialApp/Program.cs b/week-1/day-3/exercise-1/FactorialApp/Program.cs index f116eeca..a5f1fbac 100644 --- a/week-1/day-3/exercise-1/FactorialApp/Program.cs +++ b/week-1/day-3/exercise-1/FactorialApp/Program.cs @@ -4,17 +4,49 @@ public class Program { public static void Main() { - Console.Write("Enter a number: "); - int number = int.Parse(Console.ReadLine()); + bool FirstIterate = true; + int number; + do + { + // condition fro skip the warning message to print + if (!FirstIterate) + { + // change the red color for warning that value is not allowed + Console.ForegroundColor = ConsoleColor.Red; + Console.Write("Please Enter valid number !!!\n"); + Console.ForegroundColor = ConsoleColor.White; + } + Console.Write("Please Enter number : "); + FirstIterate = false; + // check the condition for input it;s take only positive integer number + } while (!int.TryParse(Console.ReadLine(), out number) || (number < 0)); + // call the calculatefactorial() method long factorial = CalculateFactorial(number); + // print that message with green colour + Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"The factorial of {number} is: {factorial}"); + Console.ForegroundColor = ConsoleColor.White; } - - public static long CalculateFactorial(int number) + private static long CalculateFactorial(int number) { - throw new NotImplementedException(); + //throw new NotImplementedException(); + int factorial = 1; + // it check the condition if number is 0 and 1 than retrun 1 + if (number == 0 || number == 1) + { + return factorial; + } + else + { + // calculate the factorial using for loop + for (int i = 2; i <= number; i++) + { + factorial *= i; + } + } + return factorial; } } } \ No newline at end of file From 7be63eb57a48bb22502fe62e85886b97ebb203c9 Mon Sep 17 00:00:00 2001 From: Vipul Date: Wed, 9 Aug 2023 15:24:55 +0530 Subject: [PATCH 06/14] Add exercise-2 --- week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs b/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs index a4ec62d9..338fb261 100644 --- a/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs +++ b/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs @@ -223,7 +223,8 @@ public int[] DijkstraShortestPath(int source) visited[i] = false; } - distances[source] = int.MinValue; + // source should be 0 because distance from source to source is 0 + distances[source] = 0; for (int count = 0; count < vertices - 1; count++) { From 6e508fac9da077d537dacea7ef8e7186937cc778 Mon Sep 17 00:00:00 2001 From: Vipul Date: Thu, 10 Aug 2023 13:11:01 +0530 Subject: [PATCH 07/14] Add day-4 exercise-1 --- .../day-4/exercise-1/StackApp/CustomStack.cs | 39 +++++++++++++++++++ .../day-4/exercise-1/StackApp/ICustomStack.cs | 2 +- week-1/day-4/exercise-1/StackApp/Program.cs | 16 ++++---- 3 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 week-1/day-4/exercise-1/StackApp/CustomStack.cs diff --git a/week-1/day-4/exercise-1/StackApp/CustomStack.cs b/week-1/day-4/exercise-1/StackApp/CustomStack.cs new file mode 100644 index 00000000..9dcc8af0 --- /dev/null +++ b/week-1/day-4/exercise-1/StackApp/CustomStack.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StackApp +{ + public class CustomStack : ICustomStack + { + private List _items; + public CustomStack() + { + _items = new List(); + } + public bool IsEmpty() + { + return _items.Count == 0; + } + + public T Pop() + { + if (IsEmpty()) + { + throw new InvalidOperationException("Stack is empty!!!"); + } + + int lastIndex = _items.Count - 1; + T PoppedItem = _items[lastIndex]; + _items.RemoveAt(lastIndex); + return PoppedItem; + } + + public void Push(T item) + { + _items.Add(item); + } + } +} diff --git a/week-1/day-4/exercise-1/StackApp/ICustomStack.cs b/week-1/day-4/exercise-1/StackApp/ICustomStack.cs index 55f3936d..19e3c509 100644 --- a/week-1/day-4/exercise-1/StackApp/ICustomStack.cs +++ b/week-1/day-4/exercise-1/StackApp/ICustomStack.cs @@ -1,6 +1,6 @@ namespace StackApp { - internal interface ICustomStack + public interface ICustomStack { void Push(T item); T Pop(); diff --git a/week-1/day-4/exercise-1/StackApp/Program.cs b/week-1/day-4/exercise-1/StackApp/Program.cs index e089f665..76f0950a 100644 --- a/week-1/day-4/exercise-1/StackApp/Program.cs +++ b/week-1/day-4/exercise-1/StackApp/Program.cs @@ -1,16 +1,16 @@ namespace StackApp { - internal class Program + public class Program { static void Main(string[] args) { - //ICustomStack intStack = new CustomStack(); - //intStack.Push(1); - //intStack.Push(2); - //intStack.Push(3); - //Console.WriteLine(intStack.Pop()); // Output: 3 - //Console.WriteLine(intStack.Pop()); // Output: 2 - //Console.WriteLine(intStack.IsEmpty()); // Output: False + ICustomStack intStack = new CustomStack(); + intStack.Push(1); + intStack.Push(2); + intStack.Push(3); + Console.WriteLine(intStack.Pop()); // Output: 3 + Console.WriteLine(intStack.Pop()); // Output: 2 + Console.WriteLine(intStack.IsEmpty()); // Output: False } } } \ No newline at end of file From a44ae246ca4c2b01e7cca9b2b9dab2e6c488f660 Mon Sep 17 00:00:00 2001 From: Vipul Date: Thu, 10 Aug 2023 13:13:15 +0530 Subject: [PATCH 08/14] Add day-4 exercise-2 --- week-1/day-4/exercise-2/IteratorsApp/Program.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/week-1/day-4/exercise-2/IteratorsApp/Program.cs b/week-1/day-4/exercise-2/IteratorsApp/Program.cs index f3cb1ae8..1a4c16ba 100644 --- a/week-1/day-4/exercise-2/IteratorsApp/Program.cs +++ b/week-1/day-4/exercise-2/IteratorsApp/Program.cs @@ -1,6 +1,6 @@ namespace IteratorsApp { - internal class Program + public class Program { static void Main(string[] args) { @@ -14,7 +14,20 @@ static void Main(string[] args) // https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/yield public static IEnumerable FibonacciSequence() { - throw new NotImplementedException(); + int first = 0; + int second = 1; + + yield return first; + yield return second; + + // Generating the first 10 Fibonacci numbers + for (int i = 2; i < 10; i++) + { + int next = first + second; + yield return next; + first = second; + second = next; + } } } } \ No newline at end of file From d9613dc171e8a952c01a86f7fe9ab8852d72dd6e Mon Sep 17 00:00:00 2001 From: Vipul Date: Thu, 10 Aug 2023 13:17:05 +0530 Subject: [PATCH 09/14] Add day-4 exercise-3 --- .../exercise-3/LinqToObjectsApp/Program.cs | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/week-1/day-4/exercise-3/LinqToObjectsApp/Program.cs b/week-1/day-4/exercise-3/LinqToObjectsApp/Program.cs index 95e8ac84..e8faa334 100644 --- a/week-1/day-4/exercise-3/LinqToObjectsApp/Program.cs +++ b/week-1/day-4/exercise-3/LinqToObjectsApp/Program.cs @@ -19,13 +19,47 @@ static void Main(string[] args) //3. Sort people by name //4. Project/Select only Name and Country of all people + //1. Get all people from USA + List PeopleFromUSA = people.Where(person => person.Country == "USA").ToList(); + Console.WriteLine("People from USA :"); + foreach (var person in PeopleFromUSA) + { + Console.WriteLine($"{person.Name}, {person.Age}, {person.Country}"); + } + + // Get all people above 30 + List PeopleAbove30 = people.Where(person => person.Age > 30).ToList(); + Console.WriteLine("People from above 30 :"); + foreach (var person in PeopleAbove30) + { + Console.WriteLine($"{person.Name}, {person.Age}, {person.Country}"); + } + + // Sort people by name + List SortedByName = people.OrderBy(person => person.Name).ToList(); + Console.WriteLine("People sorted by name:"); + foreach (var person in SortedByName) + { + Console.WriteLine($"{person.Name}, {person.Age}, {person.Country}"); + } + + // Project/Select only Name and Country of all people + var NameAndCountryList = people.Select(person => new { person.Name, person.Country }).ToList(); + + Console.WriteLine("\nName and Country list:"); + Console.WriteLine("Name => Country"); + foreach (var item in NameAndCountryList) + { + Console.WriteLine($"{item.Name} => {item.Country}"); + } + } } public class Person { - public string Name { get; set; } + public string? Name { get; set; } public int Age { get; set; } - public string Country { get; set; } + public string? Country { get; set; } } } \ No newline at end of file From 27686def026fd01203ab32669a9ffadd5785b553 Mon Sep 17 00:00:00 2001 From: Vipul Date: Thu, 10 Aug 2023 13:19:11 +0530 Subject: [PATCH 10/14] Add day-4 exercise-4 --- .../day-4/exercise-4/LinqToXmlApp/Program.cs | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/week-1/day-4/exercise-4/LinqToXmlApp/Program.cs b/week-1/day-4/exercise-4/LinqToXmlApp/Program.cs index 315df143..be8fe604 100644 --- a/week-1/day-4/exercise-4/LinqToXmlApp/Program.cs +++ b/week-1/day-4/exercise-4/LinqToXmlApp/Program.cs @@ -1,4 +1,6 @@ -namespace LinqToXmlApp +using System.Xml.Linq; + +namespace LinqToXmlApp { internal class Program { @@ -33,10 +35,35 @@ static void Main(string[] args) "; // Create an XDocument object from the XML string + // Write the title of all books to the console + // Write the title of all books with genre "Genre 1" to the console + + // Create an XDocument object from the XML string + XDocument xdoc = XDocument.Parse(xmlString); // Write the title of all books to the console + Console.WriteLine("Titles of all books:"); + foreach (var titleElement in xdoc.Descendants("Title")) + { + Console.WriteLine(titleElement.Value); + } + + Console.WriteLine(); // Write the title of all books with genre "Genre 1" to the console + Console.WriteLine("Titles of books with genre 'Genre 1':"); + foreach (var bookElement in xdoc.Descendants("Book")) + { + XElement? genreElement = bookElement.Element("Genre"); + if (genreElement != null && genreElement.Value == "Genre 1") + { + XElement? titleElement = bookElement.Element("Title"); + if (titleElement != null) + { + Console.WriteLine(titleElement.Value); + } + } + } } } From 7ee9c03f234694a129b089f26e4bd478cc9b85ec Mon Sep 17 00:00:00 2001 From: Vipul Date: Thu, 10 Aug 2023 13:20:39 +0530 Subject: [PATCH 11/14] Add day-4 exercise-5 --- week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs b/week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs index b18cea78..41dcc6ce 100644 --- a/week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs +++ b/week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs @@ -15,7 +15,11 @@ static void Main(string[] args) // Optimized implementation sw.Restart(); - + // Here for optimizing the linq query lazy loading is using + // AsParallel() Method execute LINQ queries in parallel + // We can also use IQueriable and AsQueryable() Method when we are working with database + IEnumerable optimizedQuery = data.AsParallel().Where(x => x > 100).OrderByDescending(x => x).Take(10); + sw.Stop(); Console.WriteLine("Optimized Query: {0} ms", sw.ElapsedMilliseconds); } From 3214e0983af04ad534accb9ba2da02018ffc97e6 Mon Sep 17 00:00:00 2001 From: Vipul Date: Thu, 10 Aug 2023 13:24:23 +0530 Subject: [PATCH 12/14] Add day-4 exercise-6 --- .../exercise-6/LinqFilterAndSort/Person.cs | 15 ++++++++++ .../exercise-6/LinqFilterAndSort/Program.cs | 30 +++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 week-1/day-4/exercise-6/LinqFilterAndSort/Person.cs diff --git a/week-1/day-4/exercise-6/LinqFilterAndSort/Person.cs b/week-1/day-4/exercise-6/LinqFilterAndSort/Person.cs new file mode 100644 index 00000000..25fb8771 --- /dev/null +++ b/week-1/day-4/exercise-6/LinqFilterAndSort/Person.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LinqFilterAndSort +{ + public class Person + { + public string? FirstName { get; set; } + public string? LastName { get; set; } + public int Age { get; set; } + } +} diff --git a/week-1/day-4/exercise-6/LinqFilterAndSort/Program.cs b/week-1/day-4/exercise-6/LinqFilterAndSort/Program.cs index 5c6d6240..fafef954 100644 --- a/week-1/day-4/exercise-6/LinqFilterAndSort/Program.cs +++ b/week-1/day-4/exercise-6/LinqFilterAndSort/Program.cs @@ -1,12 +1,38 @@ -namespace LinqFilterAndSort +using System; + +namespace LinqFilterAndSort { - internal class Program + public class Program { static void Main(string[] args) { // Create a list of Person objects // Use LINQ to filter and sort the list // Print the filtered and sorted list of people to the console + + // Create a list of Person objects + List people = new List + { + new Person { FirstName = "Vipul", LastName = "Kumar", Age = 24 }, + new Person { FirstName = "Divyanshu", LastName = "Kumar", Age = 25 }, + new Person { FirstName = "Rahul", LastName = "Yadav", Age = 22 }, + new Person { FirstName = "Ashish", LastName = "Kumar", Age = 21 }, + new Person { FirstName = "Atul", LastName = "Tiwari", Age = 17 }, + new Person { FirstName = "Abhi", LastName = "Upadhyay", Age = 12 } + }; + + // Use LINQ to filter people who are 18 years old or older + List FilteredPeople = people.Where(person => person.Age >= 18).ToList(); + + // Use LINQ to sort the filtered list by last name, then by first name + List SortedPeople = FilteredPeople.OrderBy(person => person.LastName).ThenBy(person => person.FirstName).ToList(); + + // Print the filtered and sorted list of people to the console + Console.WriteLine("Filtered and Sorted People : "); + foreach (var person in SortedPeople) + { + Console.WriteLine($"{person.FirstName} {person.LastName}, Age : {person.Age}"); + } } } } \ No newline at end of file From da08afc5354a935aa420e31a471359c2891d76c2 Mon Sep 17 00:00:00 2001 From: Vipul Date: Thu, 10 Aug 2023 13:26:40 +0530 Subject: [PATCH 13/14] Add day-4 exercise-7 --- .../exercise-7/LinqListNumbers/Program.cs | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/week-1/day-4/exercise-7/LinqListNumbers/Program.cs b/week-1/day-4/exercise-7/LinqListNumbers/Program.cs index 6caadd07..db9b81b7 100644 --- a/week-1/day-4/exercise-7/LinqListNumbers/Program.cs +++ b/week-1/day-4/exercise-7/LinqListNumbers/Program.cs @@ -13,6 +13,64 @@ static void Main(string[] args) // 3. Calculate the sum of all numbers // 4. Calculate the average of all numbers // 5. Find the minimum and maximum values in the list + + // Find all even numbers + List EvenNumbers = numbers.Where(num => num % 2 == 0).ToList(); + Console.WriteLine("Even Numbers: " + string.Join(", ", EvenNumbers)); + + // Find all numbers greater than a specific value (e.g., 20) + int specificValue = UserInput(); + List greaterThanSpecificValue = numbers.Where(num => num > specificValue).ToList(); + Console.WriteLine("Numbers Greater Than " + specificValue + ": " + string.Join(", ", greaterThanSpecificValue)); + + // Calculate the sum of all numbers + int sum = numbers.Sum(); + Console.WriteLine("Sum of Numbers: " + sum); + + // Calculate the average of all numbers + double average = numbers.Average(); + Console.WriteLine("Average of Numbers: " + average); + + // Find the minimum and maximum values in the list + int minimum = numbers.Min(); + int maximum = numbers.Max(); + Console.WriteLine("Minimum Value: " + minimum); + Console.WriteLine("Maximum Value: " + maximum); + } + + // Take user input integer only uaer can be only able to type integer number + // Here negative number is also allowed + private static int UserInput() + { + ConsoleKeyInfo key; + string input = ""; + + Console.Write("Enter Specific number : "); + + do + { + key = Console.ReadKey(true); + if (char.IsDigit(key.KeyChar)) + { + input += key.KeyChar; + + // Display the digit + Console.Write(key.KeyChar); + } + else if (key.Key == ConsoleKey.Backspace && input.Length > 0) + { + input = input.Substring(0, input.Length - 1); + + // Erase the last character from display + Console.Write("\b \b"); + } + } while (key.Key != ConsoleKey.Enter); + + // Move to the next line + Console.WriteLine(); + + int number = Convert.ToInt32(input); + return number; } } } \ No newline at end of file From 69c45127e846bdb7648b1d63b3b65fc51ed559ef Mon Sep 17 00:00:00 2001 From: Vipul Date: Thu, 10 Aug 2023 13:29:33 +0530 Subject: [PATCH 14/14] Add day-4 exercise-8 --- .../exercise-8/LinqGroupAggregate/Product.cs | 6 +-- .../exercise-8/LinqGroupAggregate/Program.cs | 47 +++++++++++++++---- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/week-1/day-4/exercise-8/LinqGroupAggregate/Product.cs b/week-1/day-4/exercise-8/LinqGroupAggregate/Product.cs index df8c445d..dc95e1d4 100644 --- a/week-1/day-4/exercise-8/LinqGroupAggregate/Product.cs +++ b/week-1/day-4/exercise-8/LinqGroupAggregate/Product.cs @@ -6,10 +6,10 @@ namespace LinqGroupAggregate { - internal class Product + public class Product { - public string Name { get; set; } - public string Category { get; set; } + public string? Name { get; set; } + public string? Category { get; set; } public decimal Price { get; set; } } } diff --git a/week-1/day-4/exercise-8/LinqGroupAggregate/Program.cs b/week-1/day-4/exercise-8/LinqGroupAggregate/Program.cs index 04dbd615..aeb921e6 100644 --- a/week-1/day-4/exercise-8/LinqGroupAggregate/Program.cs +++ b/week-1/day-4/exercise-8/LinqGroupAggregate/Program.cs @@ -1,25 +1,54 @@ namespace LinqGroupAggregate { - internal class Program + public class Program { static void Main(string[] args) { // Create a list of Product objects List products = new List - { - new Product { Name = "Laptop", Category = "Electronics", Price = 1200.00M }, - new Product { Name = "Smartphone", Category = "Electronics", Price = 800.00M }, - new Product { Name = "Headphones", Category = "Electronics", Price = 200.00M }, - new Product { Name = "Shirt", Category = "Clothing", Price = 30.00M }, - new Product { Name = "Jeans", Category = "Clothing", Price = 60.00M }, - new Product { Name = "Sneakers", Category = "Footwear", Price = 100.00M } - }; + { + new Product { Name = "Laptop", Category = "Electronics", Price = 1200.00M }, + new Product { Name = "Smartphone", Category = "Electronics", Price = 800.00M }, + new Product { Name = "Headphones", Category = "Electronics", Price = 200.00M }, + new Product { Name = "Shirt", Category = "Clothing", Price = 30.00M }, + new Product { Name = "Jeans", Category = "Clothing", Price = 60.00M }, + new Product { Name = "Sneakers", Category = "Footwear", Price = 100.00M } + }; // Use LINQ to perform the following operations: // 1. Group products by category // 2. Count the number of products in each category // 3. Calculate the total price of products in each category // 4. Find the most expensive product in each category + + // Group products by category + var GroupByCategory = products.GroupBy(product => product.Category); + + // Count the number of products in each category + Console.WriteLine("Number of Products in Each Category:"); + foreach (var group in GroupByCategory) + { + Console.WriteLine($"{group.Key}: {group.Count()} products"); + } + + // Calculate the total price of products in each category + Console.WriteLine("\nTotal Price of Products in Each Category:"); + foreach (var group in GroupByCategory) + { + decimal totalPrice = group.Sum(product => product.Price); + Console.WriteLine($"{group.Key}: ${totalPrice}"); + } + + // Find the most expensive product in each category + Console.WriteLine("\nMost Expensive Product in Each Category:"); + foreach (var group in GroupByCategory) + { + var mostExpensiveProduct = group.OrderByDescending(product => product.Price).FirstOrDefault(); + if (mostExpensiveProduct != null) + { + Console.WriteLine($"{group.Key}: {mostExpensiveProduct.Name} => ${mostExpensiveProduct.Price}"); + } + } } } } \ No newline at end of file