-
Notifications
You must be signed in to change notification settings - Fork 0
Homework 4 #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Homework 4 #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| 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 |
| 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); | ||
| } | ||
| } |
| 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> |
| 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(); | ||
| } | ||
| } |
| 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> |
| 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."); | ||
| } | ||
| } |
| 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> |
| 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. |
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| break; | ||
| case "2": | ||
| Console.WriteLine("Elements in the List:"); | ||
| foreach (var num in list) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't use |
||
| { | ||
| 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; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 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> |
There was a problem hiding this comment.
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