Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
454 changes: 454 additions & 0 deletions .gitignore

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions KDU-DOTNET.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "homework-1", "homework-1\homework-1.csproj", "{D8745BCC-B6E9-46B7-91C5-26BEFF3BDBDE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D8745BCC-B6E9-46B7-91C5-26BEFF3BDBDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8745BCC-B6E9-46B7-91C5-26BEFF3BDBDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8745BCC-B6E9-46B7-91C5-26BEFF3BDBDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8745BCC-B6E9-46B7-91C5-26BEFF3BDBDE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3F2F8AEF-7CF3-4C45-B95B-2278498B5792}
EndGlobalSection
EndGlobal
46 changes: 46 additions & 0 deletions homework-1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;

public class BankAccount
{
private int balance;

public BankAccount()
{
balance = 0;
}

public void Deposit(int amount)
{
balance += amount;
Console.WriteLine($"Deposited Rs{amount}. New balance: Rs{balance}");
}

public void Withdraw(int amount)
{

if (balance >= amount)
{
balance -= amount;
Console.WriteLine($"Withdrawn Rs{amount}. New balance: Rs{balance}");
}
else
{
Console.WriteLine("Error: Insufficient balance.");
}
}
public int GetBalance()
{
return balance;
}
}

public class Program
{
public static void Main(string[] args)
{
BankAccount account = new BankAccount();
account.Deposit(1000);
account.Withdraw(500);
account.Withdraw(800);
}
}
11 changes: 11 additions & 0 deletions homework-1/homework-1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>homework_1</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
44 changes: 44 additions & 0 deletions homework-2/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;

class Person
{
private string name;
private int age;

// Parameterized constructor that accepts values for name and age
public Person(string name, int age)
{
this.name = name;
this.age = age;
}

// Default constructor that sets name to unknown and age to 0
public Person()
{
name = "Unknown";
age = 0;
}

// Destructor which also displays message
~Person()
{
Console.WriteLine("The object is being destroyed.");
}

public void DisplayInfo()
{
Console.WriteLine($"Name: {name}, Age: {age}");
}
}

class Program
{
static void Main()
{
Person person1 = new Person("Prashant", 22);
person1.DisplayInfo();

Person person2 = new Person();
person2.DisplayInfo();
}
}
11 changes: 11 additions & 0 deletions homework-2/homework-2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>homework_2</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
41 changes: 41 additions & 0 deletions homework-3/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.IO;

public class Logger : IDisposable
{
private static Logger? instance;
private readonly string logFilePath;
private StreamWriter? logWriter;

private Logger(string logFilePath)
{
this.logFilePath = logFilePath;
logWriter = new StreamWriter(logFilePath, true);
}

public static Logger Instance
{
get
{
instance ??= new Logger("log.txt");
return instance;
}
}

private void Log(string message)
{
logWriter?.WriteLine($"{DateTime.Now}: {message}");
logWriter?.Flush();
}

public void Dispose()
{
logWriter?.Dispose();
}

public static void Main(string[] args)
{
using var logger = Logger.Instance;
logger.Log("Logging a message to the log file.");
}
}
11 changes: 11 additions & 0 deletions homework-3/homework-3.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>homework_3</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions homework-3/log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5/12/2024 12:27:14 AM: Logging a message to the log file.
106 changes: 106 additions & 0 deletions homework-4/Program.cs

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have created separate class for ListOperation and DictionaryOperation

Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp
{
public class Program
{
static List<int> list = new List<int>();
static Dictionary<int, string> dictionary = new Dictionary<int, string>();

static void Main(string[] args)
{
Menu();
}

static void Menu()
{
Console.WriteLine("Please enter a number from the given menu:");
Console.WriteLine("1. Add an element to the List.");
Console.WriteLine("2. Print all the elements present in the List.");
Console.WriteLine("3. Delete the last element from the List.");
Console.WriteLine("4. Delete the first element from the List.");
Console.WriteLine("5. Delete the middle element from the List.");
Console.WriteLine("6. Calculate the average of the elements present in the List.");
Console.WriteLine("7. Exit the application.");
Comment on lines +19 to +26

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are the dictionary operaitons????


string choice = Console.ReadLine();

switch (choice)
{
case "1":
Console.Write("Enter a number to add to the List: ");
int numToAdd = Convert.ToInt32(Console.ReadLine());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if i enter 'a' in the console will it be converted? Or will it throw an error. Also did you looked into int.TryParse()?

list.Add(numToAdd);
Menu();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are calling the Menu function again.. that means for every operation I will be creating another stack of function call... could have use a while(true) loop

break;
case "2":
Console.WriteLine("Elements in the List:");
foreach (var num in list)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use var

{
Console.Write(num + " ");
}
Console.WriteLine();
Menu();
break;
case "3":
if (list.Count > 0)
{
list.RemoveAt(list.Count - 1);
Console.WriteLine("Last element deleted from the List.");
}
else
{
Console.WriteLine("List is empty.");
}
Menu();
break;
case "4":
if (list.Count > 0)
{
list.RemoveAt(0);
Console.WriteLine("First element deleted from the List.");
}
else
{
Console.WriteLine("List is empty.");
}
Menu();
break;
case "5":
if (list.Count > 0)
{
int middleIndex = list.Count / 2;
list.RemoveAt(middleIndex);
Console.WriteLine("Middle element deleted from the List.");
}
else
{
Console.WriteLine("List is empty.");
}
Menu();
break;
case "6":
if (list.Count > 0)
{
double average = list.Average();
Console.WriteLine("Average of elements in the List: " + average);
}
else
{
Console.WriteLine("List is empty.");
}
Menu();
break;
case "7":
Environment.Exit(0);
break;
default:
Console.WriteLine("Please enter a valid number.");
Menu();
break;
}
}
}
}
11 changes: 11 additions & 0 deletions homework-4/homework-4.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>homework_4</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>