From c5aa9232b7e2a1a609f7e1de6d33870e9004bebc Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Wed, 6 May 2026 20:18:50 -0400 Subject: [PATCH 01/26] initial commit --- .../Calculator.aaronkagan.sln | 16 +++ .../Calculator.aaronkagan.csproj | 10 ++ .../Calculator.aaronkagan/Program.cs | 113 ++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 Calculator.aaronkagan/Calculator.aaronkagan.sln create mode 100644 Calculator.aaronkagan/Calculator.aaronkagan/Calculator.aaronkagan.csproj create mode 100644 Calculator.aaronkagan/Calculator.aaronkagan/Program.cs diff --git a/Calculator.aaronkagan/Calculator.aaronkagan.sln b/Calculator.aaronkagan/Calculator.aaronkagan.sln new file mode 100644 index 00000000..7ddaa107 --- /dev/null +++ b/Calculator.aaronkagan/Calculator.aaronkagan.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calculator.aaronkagan", "Calculator.aaronkagan\Calculator.aaronkagan.csproj", "{ED336576-8AEF-470F-B8C6-A21AAC4453C1}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {ED336576-8AEF-470F-B8C6-A21AAC4453C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ED336576-8AEF-470F-B8C6-A21AAC4453C1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ED336576-8AEF-470F-B8C6-A21AAC4453C1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ED336576-8AEF-470F-B8C6-A21AAC4453C1}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Calculator.aaronkagan.csproj b/Calculator.aaronkagan/Calculator.aaronkagan/Calculator.aaronkagan.csproj new file mode 100644 index 00000000..6c1dc922 --- /dev/null +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Calculator.aaronkagan.csproj @@ -0,0 +1,10 @@ + + + + Exe + net10.0 + enable + enable + + + diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs new file mode 100644 index 00000000..cb4aa674 --- /dev/null +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -0,0 +1,113 @@ +using System.Text.RegularExpressions; + +class Calculator +{ + public static double DoOperation(double num1, double num2, string op) + { + double result = double.NaN; + + switch (op) + { + case "a": + result = num1 + num2; + break; + case "s": + result = num1 - num2; + break; + case "m": + result = num1 * num2; + break; + case "d": + if (num2 != 0) + { + result = num1 / num2; + } + break; + default: + break; + } + return result; + } +} + +class Program +{ + static void Main(string[] args) + { + bool endApp = false; + // Display title as the C# console calculator app. + Console.WriteLine("Console Calculator in C#\r"); + Console.WriteLine("------------------------\n"); + + while (!endApp) + { + // Declare variables and set to empty. + // Use Nullable types (with ?) to match type of System.Console.ReadLine + string? numInput1 = ""; + string? numInput2 = ""; + double result = 0; + + // Ask the user to type the first number. + Console.Write("Type a number, and then press Enter: "); + numInput1 = Console.ReadLine(); + + double cleanNum1 = 0; + while (!double.TryParse(numInput1, out cleanNum1)) + { + Console.Write("This is not valid input. Please enter a numeric value: "); + numInput1 = Console.ReadLine(); + } + + // Ask the user to type the second number. + Console.Write("Type another number, and then press Enter: "); + numInput2 = Console.ReadLine(); + + double cleanNum2 = 0; + while (!double.TryParse(numInput2, out cleanNum2)) + { + Console.Write("This is not valid input. Please enter a numeric value: "); + numInput2 = Console.ReadLine(); + } + + // Ask the user to choose an operator. + Console.WriteLine("Choose an operator from the following list:"); + Console.WriteLine("\ta - Add"); + Console.WriteLine("\ts - Subtract"); + Console.WriteLine("\tm - Multiply"); + Console.WriteLine("\td - Divide"); + Console.Write("Your option? "); + + string? op = Console.ReadLine(); + + // Validate input is not null, and matches the pattern + if (op == null || ! Regex.IsMatch(op, "[a|s|m|d]")) + { + Console.WriteLine("Error: Unrecognized input."); + } + else + { + try + { + result = Calculator.DoOperation(cleanNum1, cleanNum2, op); + if (double.IsNaN(result)) + { + Console.WriteLine("This operation will result in a mathematical error.\n"); + } + else Console.WriteLine("Your result: {0:0.##}\n", result); + } + catch (Exception e) + { + Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); + } + } + Console.WriteLine("------------------------\n"); + + // Wait for the user to respond before closing. + Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: "); + if (Console.ReadLine() == "n") endApp = true; + + Console.WriteLine("\n"); // Friendly linespacing. + } + return; + } +} \ No newline at end of file From 221673eb11e04e113d2806d814f6d31dc08c8127 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Wed, 6 May 2026 20:19:36 -0400 Subject: [PATCH 02/26] removing comments --- .../Calculator.aaronkagan/Program.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index cb4aa674..d31a38a5 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -35,19 +35,16 @@ class Program static void Main(string[] args) { bool endApp = false; - // Display title as the C# console calculator app. Console.WriteLine("Console Calculator in C#\r"); Console.WriteLine("------------------------\n"); while (!endApp) { - // Declare variables and set to empty. - // Use Nullable types (with ?) to match type of System.Console.ReadLine + string? numInput1 = ""; string? numInput2 = ""; double result = 0; - // Ask the user to type the first number. Console.Write("Type a number, and then press Enter: "); numInput1 = Console.ReadLine(); @@ -58,7 +55,6 @@ static void Main(string[] args) numInput1 = Console.ReadLine(); } - // Ask the user to type the second number. Console.Write("Type another number, and then press Enter: "); numInput2 = Console.ReadLine(); @@ -69,7 +65,6 @@ static void Main(string[] args) numInput2 = Console.ReadLine(); } - // Ask the user to choose an operator. Console.WriteLine("Choose an operator from the following list:"); Console.WriteLine("\ta - Add"); Console.WriteLine("\ts - Subtract"); @@ -79,7 +74,6 @@ static void Main(string[] args) string? op = Console.ReadLine(); - // Validate input is not null, and matches the pattern if (op == null || ! Regex.IsMatch(op, "[a|s|m|d]")) { Console.WriteLine("Error: Unrecognized input."); @@ -102,11 +96,10 @@ static void Main(string[] args) } Console.WriteLine("------------------------\n"); - // Wait for the user to respond before closing. Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: "); if (Console.ReadLine() == "n") endApp = true; - Console.WriteLine("\n"); // Friendly linespacing. + Console.WriteLine("\n"); } return; } From aafdd0dcb89eef55c220c74c11f5768c2d4eacaf Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Wed, 6 May 2026 20:21:17 -0400 Subject: [PATCH 03/26] moving things around --- .../Calculator.aaronkagan/Program.cs | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index d31a38a5..95ab1f1b 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -5,7 +5,6 @@ class Calculator public static double DoOperation(double num1, double num2, string op) { double result = double.NaN; - switch (op) { case "a": @@ -40,7 +39,6 @@ static void Main(string[] args) while (!endApp) { - string? numInput1 = ""; string? numInput2 = ""; double result = 0; @@ -76,23 +74,23 @@ static void Main(string[] args) if (op == null || ! Regex.IsMatch(op, "[a|s|m|d]")) { - Console.WriteLine("Error: Unrecognized input."); + Console.WriteLine("Error: Unrecognized input."); } else { - try - { - result = Calculator.DoOperation(cleanNum1, cleanNum2, op); - if (double.IsNaN(result)) - { - Console.WriteLine("This operation will result in a mathematical error.\n"); - } - else Console.WriteLine("Your result: {0:0.##}\n", result); - } - catch (Exception e) - { - Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); - } + try + { + result = Calculator.DoOperation(cleanNum1, cleanNum2, op); + if (double.IsNaN(result)) + { + Console.WriteLine("This operation will result in a mathematical error.\n"); + } + else Console.WriteLine("Your result: {0:0.##}\n", result); + } + catch (Exception e) + { + Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); + } } Console.WriteLine("------------------------\n"); From b60b3d94c6656a1ca94ef3f27a85fd9daf841103 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Wed, 6 May 2026 20:33:44 -0400 Subject: [PATCH 04/26] adding code --- .../Calculator.aaronkagan.sln | 6 + .../Calculator.aaronkagan.csproj | 4 + .../Calculator.aaronkagan/Program.cs | 139 ++++++++---------- .../CalculatorLibrary/CalculatorLibrary.cs | 69 +++++++++ .../CalculatorLibrary.csproj | 13 ++ 5 files changed, 150 insertions(+), 81 deletions(-) create mode 100644 Calculator.aaronkagan/CalculatorLibrary/CalculatorLibrary.cs create mode 100644 Calculator.aaronkagan/CalculatorLibrary/CalculatorLibrary.csproj diff --git a/Calculator.aaronkagan/Calculator.aaronkagan.sln b/Calculator.aaronkagan/Calculator.aaronkagan.sln index 7ddaa107..46db32aa 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan.sln +++ b/Calculator.aaronkagan/Calculator.aaronkagan.sln @@ -2,6 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calculator.aaronkagan", "Calculator.aaronkagan\Calculator.aaronkagan.csproj", "{ED336576-8AEF-470F-B8C6-A21AAC4453C1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CalculatorLibrary", "CalculatorLibrary\CalculatorLibrary.csproj", "{D6629EDB-7357-4842-8923-F251BB16D010}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +14,9 @@ Global {ED336576-8AEF-470F-B8C6-A21AAC4453C1}.Debug|Any CPU.Build.0 = Debug|Any CPU {ED336576-8AEF-470F-B8C6-A21AAC4453C1}.Release|Any CPU.ActiveCfg = Release|Any CPU {ED336576-8AEF-470F-B8C6-A21AAC4453C1}.Release|Any CPU.Build.0 = Release|Any CPU + {D6629EDB-7357-4842-8923-F251BB16D010}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D6629EDB-7357-4842-8923-F251BB16D010}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D6629EDB-7357-4842-8923-F251BB16D010}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D6629EDB-7357-4842-8923-F251BB16D010}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Calculator.aaronkagan.csproj b/Calculator.aaronkagan/Calculator.aaronkagan/Calculator.aaronkagan.csproj index 6c1dc922..595f1500 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Calculator.aaronkagan.csproj +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Calculator.aaronkagan.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 95ab1f1b..5d33188d 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -1,104 +1,81 @@ using System.Text.RegularExpressions; +using CalculatorLibrary; -class Calculator +namespace CalculatorProgram { - public static double DoOperation(double num1, double num2, string op) - { - double result = double.NaN; - switch (op) - { - case "a": - result = num1 + num2; - break; - case "s": - result = num1 - num2; - break; - case "m": - result = num1 * num2; - break; - case "d": - if (num2 != 0) - { - result = num1 / num2; - } - break; - default: - break; - } - return result; - } -} -class Program -{ - static void Main(string[] args) + class Program { - bool endApp = false; - Console.WriteLine("Console Calculator in C#\r"); - Console.WriteLine("------------------------\n"); - - while (!endApp) + static void Main() { - string? numInput1 = ""; - string? numInput2 = ""; - double result = 0; - - Console.Write("Type a number, and then press Enter: "); - numInput1 = Console.ReadLine(); + bool endApp = false; + Console.WriteLine("Console Calculator in C#\r"); + Console.WriteLine("------------------------\n"); - double cleanNum1 = 0; - while (!double.TryParse(numInput1, out cleanNum1)) + Calculator calculator = new Calculator(); + while (!endApp) { - Console.Write("This is not valid input. Please enter a numeric value: "); + string? numInput1; + string? numInput2; + double result; + + Console.Write("Type a number, and then press Enter: "); numInput1 = Console.ReadLine(); - } - Console.Write("Type another number, and then press Enter: "); - numInput2 = Console.ReadLine(); + double cleanNum1; + while (!double.TryParse(numInput1, out cleanNum1)) + { + Console.Write("This is not valid input. Please enter an integer value: "); + numInput1 = Console.ReadLine(); + } - double cleanNum2 = 0; - while (!double.TryParse(numInput2, out cleanNum2)) - { - Console.Write("This is not valid input. Please enter a numeric value: "); + Console.Write("Type another number, and then press Enter: "); numInput2 = Console.ReadLine(); - } - Console.WriteLine("Choose an operator from the following list:"); - Console.WriteLine("\ta - Add"); - Console.WriteLine("\ts - Subtract"); - Console.WriteLine("\tm - Multiply"); - Console.WriteLine("\td - Divide"); - Console.Write("Your option? "); + double cleanNum2; + while (!double.TryParse(numInput2, out cleanNum2)) + { + Console.Write("This is not valid input. Please enter an integer value: "); + numInput2 = Console.ReadLine(); + } - string? op = Console.ReadLine(); + Console.WriteLine("Choose an operator from the following list:"); + Console.WriteLine("\ta - Add"); + Console.WriteLine("\ts - Subtract"); + Console.WriteLine("\tm - Multiply"); + Console.WriteLine("\td - Divide"); + Console.Write("Your option? "); - if (op == null || ! Regex.IsMatch(op, "[a|s|m|d]")) - { - Console.WriteLine("Error: Unrecognized input."); - } - else - { - try + string? op = Console.ReadLine(); + + if (op == null || ! Regex.IsMatch(op, "[a|s|m|d]")) { - result = Calculator.DoOperation(cleanNum1, cleanNum2, op); - if (double.IsNaN(result)) - { - Console.WriteLine("This operation will result in a mathematical error.\n"); - } - else Console.WriteLine("Your result: {0:0.##}\n", result); + Console.WriteLine("Error: Unrecognized input."); } - catch (Exception e) - { - Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); + else + { + try + { + result = calculator.DoOperation(cleanNum1, cleanNum2, op); + if (double.IsNaN(result)) + { + Console.WriteLine("This operation will result in a mathematical error.\n"); + } + else Console.WriteLine("Your result: {0:0.##}\n", result); + } + catch (Exception e) + { + Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); + } } - } - Console.WriteLine("------------------------\n"); + Console.WriteLine("------------------------\n"); - Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: "); - if (Console.ReadLine() == "n") endApp = true; + Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: "); + if (Console.ReadLine() == "n") endApp = true; - Console.WriteLine("\n"); + Console.WriteLine("\n"); + } + calculator.Finish(); } - return; } } \ No newline at end of file diff --git a/Calculator.aaronkagan/CalculatorLibrary/CalculatorLibrary.cs b/Calculator.aaronkagan/CalculatorLibrary/CalculatorLibrary.cs new file mode 100644 index 00000000..42afa1aa --- /dev/null +++ b/Calculator.aaronkagan/CalculatorLibrary/CalculatorLibrary.cs @@ -0,0 +1,69 @@ +using Newtonsoft.Json; + +namespace CalculatorLibrary +{ + public class Calculator + { + + JsonWriter writer; + + public Calculator() + { + StreamWriter logFile = File.CreateText("calculatorlog.json"); + logFile.AutoFlush = true; + writer = new JsonTextWriter(logFile); + writer.Formatting = Formatting.Indented; + writer.WriteStartObject(); + writer.WritePropertyName("Operations"); + writer.WriteStartArray(); + } + + public double DoOperation(double num1, double num2, string op) + { + double result = double.NaN; + writer.WriteStartObject(); + writer.WritePropertyName("Operand1"); + writer.WriteValue(num1); + writer.WritePropertyName("Operand2"); + writer.WriteValue(num2); + writer.WritePropertyName("Operation"); + + switch (op) + { + case "a": + result = num1 + num2; + writer.WriteValue("Add"); + break; + case "s": + result = num1 - num2; + writer.WriteValue("Subtract"); + break; + case "m": + result = num1 * num2; + writer.WriteValue("Multiply"); + break; + case "d": + if (num2 != 0) + { + result = num1 / num2; + } + writer.WriteValue("Divide"); + break; + default: + break; + } + writer.WritePropertyName("Result"); + writer.WriteValue(result); + writer.WriteEndObject(); + + return result; + } + + public void Finish() + { + writer.WriteEndArray(); + writer.WriteEndObject(); + writer.Close(); + } + } +} \ No newline at end of file diff --git a/Calculator.aaronkagan/CalculatorLibrary/CalculatorLibrary.csproj b/Calculator.aaronkagan/CalculatorLibrary/CalculatorLibrary.csproj new file mode 100644 index 00000000..b05421ed --- /dev/null +++ b/Calculator.aaronkagan/CalculatorLibrary/CalculatorLibrary.csproj @@ -0,0 +1,13 @@ + + + + net10.0 + enable + enable + + + + + + + From 0d06e91774d2f14d1fd44abdaf5c02e7a665e848 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Thu, 7 May 2026 16:20:21 -0400 Subject: [PATCH 05/26] addjing full history functionality --- .../Calculator.aaronkagan/Program.cs | 83 ++++++++++++++++++- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 5d33188d..7ba0c31c 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -13,6 +13,7 @@ static void Main() Console.WriteLine("------------------------\n"); Calculator calculator = new Calculator(); + CalculationHistory history = new CalculationHistory(); while (!endApp) { string? numInput1; @@ -61,7 +62,12 @@ static void Main() { Console.WriteLine("This operation will result in a mathematical error.\n"); } - else Console.WriteLine("Your result: {0:0.##}\n", result); + else + { + Console.WriteLine("Your result: {0:0.##}\n", result); + history.Add(cleanNum1, cleanNum2, op); + } + } catch (Exception e) { @@ -70,12 +76,83 @@ static void Main() } Console.WriteLine("------------------------\n"); - Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: "); - if (Console.ReadLine() == "n") endApp = true; + Console.Write("Press 'n', 'h' to show the calculation history or press Enter to continue: "); + + var input = Console.ReadLine(); + + switch (input) + { + case "n": + endApp = true; + break; + case "h": + { + history.Print(); + Console.WriteLine("Press 'd' then Enter to delete the history or any other key and Enter to continue"); + var answer = Console.ReadLine(); + if (answer == "d") + { + history.Delete(); + Console.WriteLine("History deleted"); + } + break; + } + default: + continue; + } Console.WriteLine("\n"); } calculator.Finish(); } } + + class CalculationHistory + { + private List _history = []; + public void Print() + { + if (_history.Count == 0) + { + Console.WriteLine("There is no history to show"); + } + else + { + Console.WriteLine("HISTORY"); + Console.WriteLine("-----------------"); + foreach (var calculation in _history) + { + Console.WriteLine(calculation); + } + Console.WriteLine("-----------------"); + } + + } + public void Add(double left, double right, string operation) + { + string calculationText = ""; + Calculator calculator = new Calculator(); + + switch (operation) + { + case "a" : + calculationText = $"{left} + {right} = {calculator.DoOperation(left, right, operation)}"; + break; + case "s" : + calculationText = $"{left} - {right} = {calculator.DoOperation(left, right, operation)}"; + break; + case "m" : + calculationText = $"{left} * {right} = {calculator.DoOperation(left, right, operation)}"; + break; + case "d" : + calculationText = $"{left} / {right} = {calculator.DoOperation(left, right, operation)}"; + break; + } + _history.Add(calculationText); + } + public void Delete() + { + _history = []; + } + } } \ No newline at end of file From 2d9266269bc1c81624d8f2ad290f9e38369b953d Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Thu, 7 May 2026 16:34:43 -0400 Subject: [PATCH 06/26] adding calculation count --- Calculator.aaronkagan/Calculator.aaronkagan/Program.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 7ba0c31c..651a1b8d 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -66,6 +66,7 @@ static void Main() { Console.WriteLine("Your result: {0:0.##}\n", result); history.Add(cleanNum1, cleanNum2, op); + history.IncrementCount(); } } @@ -110,6 +111,7 @@ static void Main() class CalculationHistory { private List _history = []; + private int _count = 0; public void Print() { if (_history.Count == 0) @@ -125,6 +127,7 @@ public void Print() Console.WriteLine(calculation); } Console.WriteLine("-----------------"); + Console.WriteLine("The calculator has been used " + _count + " " + (_count == 1 ? "time" : "times")); } } @@ -154,5 +157,10 @@ public void Delete() { _history = []; } + + public void IncrementCount() + { + _count++; + } } } \ No newline at end of file From 01c3f47c2baf983913a4454ad35e7bbe2d67b5d5 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Thu, 7 May 2026 16:36:20 -0400 Subject: [PATCH 07/26] fixinf count feature --- Calculator.aaronkagan/Calculator.aaronkagan/Program.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 651a1b8d..d9c6913c 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -66,7 +66,6 @@ static void Main() { Console.WriteLine("Your result: {0:0.##}\n", result); history.Add(cleanNum1, cleanNum2, op); - history.IncrementCount(); } } @@ -111,7 +110,6 @@ static void Main() class CalculationHistory { private List _history = []; - private int _count = 0; public void Print() { if (_history.Count == 0) @@ -127,7 +125,7 @@ public void Print() Console.WriteLine(calculation); } Console.WriteLine("-----------------"); - Console.WriteLine("The calculator has been used " + _count + " " + (_count == 1 ? "time" : "times")); + Console.WriteLine("The calculator has been used " + _history.Count + " " + (_history.Count == 1 ? "time" : "times")); } } @@ -157,10 +155,5 @@ public void Delete() { _history = []; } - - public void IncrementCount() - { - _count++; - } } } \ No newline at end of file From ae29f2769ab5cd0d65de13c6ccfe4eeab58c0f39 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Thu, 7 May 2026 18:18:20 -0400 Subject: [PATCH 08/26] storing results --- .../Calculator.aaronkagan/Program.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index d9c6913c..03ff4b51 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -110,6 +110,7 @@ static void Main() class CalculationHistory { private List _history = []; + private List _results = []; public void Print() { if (_history.Count == 0) @@ -120,9 +121,9 @@ public void Print() { Console.WriteLine("HISTORY"); Console.WriteLine("-----------------"); - foreach (var calculation in _history) + foreach (var (index, calculation) in _history.Index()) { - Console.WriteLine(calculation); + Console.WriteLine($"{index + 1} {calculation}"); } Console.WriteLine("-----------------"); Console.WriteLine("The calculator has been used " + _history.Count + " " + (_history.Count == 1 ? "time" : "times")); @@ -132,28 +133,31 @@ public void Print() public void Add(double left, double right, string operation) { string calculationText = ""; - Calculator calculator = new Calculator(); - + Calculator calculator = new Calculator(); + double result = calculator.DoOperation(left, right, operation); + switch (operation) { case "a" : - calculationText = $"{left} + {right} = {calculator.DoOperation(left, right, operation)}"; + calculationText = $"{left} + {right} = {result}"; break; case "s" : - calculationText = $"{left} - {right} = {calculator.DoOperation(left, right, operation)}"; + calculationText = $"{left} - {right} = {result}"; break; case "m" : - calculationText = $"{left} * {right} = {calculator.DoOperation(left, right, operation)}"; + calculationText = $"{left} * {right} = {result}"; break; case "d" : - calculationText = $"{left} / {right} = {calculator.DoOperation(left, right, operation)}"; + calculationText = $"{left} / {right} = {result}"; break; } _history.Add(calculationText); + _results.Add(result); } public void Delete() { _history = []; + _results = []; } } } \ No newline at end of file From bb93c695bae8c631dbb8dd8ce2f04bee57bc8127 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Thu, 7 May 2026 20:42:18 -0400 Subject: [PATCH 09/26] rewoking the adding of results --- Calculator.aaronkagan/Calculator.aaronkagan/Program.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 03ff4b51..548011e4 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -65,7 +65,7 @@ static void Main() else { Console.WriteLine("Your result: {0:0.##}\n", result); - history.Add(cleanNum1, cleanNum2, op); + history.Add(cleanNum1, cleanNum2, op, result); } } @@ -130,12 +130,9 @@ public void Print() } } - public void Add(double left, double right, string operation) + public void Add(double left, double right, string operation, double result) { string calculationText = ""; - Calculator calculator = new Calculator(); - double result = calculator.DoOperation(left, right, operation); - switch (operation) { case "a" : From bd4844a4ca83d3b876cf153d2f2ad4cb02b8ec9d Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Wed, 13 May 2026 11:39:11 -0400 Subject: [PATCH 10/26] adding console clears --- .../Calculator.aaronkagan/Program.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 548011e4..38860b1f 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -76,9 +76,10 @@ static void Main() } Console.WriteLine("------------------------\n"); - Console.Write("Press 'n', 'h' to show the calculation history or press Enter to continue: "); + Console.Write("Press 'n' to exit, 'h' to show the calculation history or press Enter to continue: "); var input = Console.ReadLine(); + Console.Clear(); switch (input) { @@ -90,6 +91,7 @@ static void Main() history.Print(); Console.WriteLine("Press 'd' then Enter to delete the history or any other key and Enter to continue"); var answer = Console.ReadLine(); + Console.Clear(); if (answer == "d") { history.Delete(); @@ -123,10 +125,22 @@ public void Print() Console.WriteLine("-----------------"); foreach (var (index, calculation) in _history.Index()) { - Console.WriteLine($"{index + 1} {calculation}"); + Console.WriteLine($"{index + 1}) {calculation}"); } Console.WriteLine("-----------------"); Console.WriteLine("The calculator has been used " + _history.Count + " " + (_history.Count == 1 ? "time" : "times")); + + // Console.WriteLine("/n"); + // Console.WriteLine("Would you like to use any of the above results to do a new calculation? [y/N]"); + // + // var input = Console.ReadLine(); + // + // if (input is "y" or "Y") + // { + // + // } + + } } From 2502ea5a9015c897ae284098e0d69849424249a4 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Wed, 13 May 2026 12:53:59 -0400 Subject: [PATCH 11/26] moving things around --- .../Calculator.aaronkagan/Program.cs | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 38860b1f..8f40c06d 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -89,6 +89,15 @@ static void Main() case "h": { history.Print(); + // Console.WriteLine("/n"); + // Console.WriteLine("Would you like to use any of the above results to do a new calculation? [y/N]"); + // + // var input = Console.ReadLine(); + // + // if (input is "y" or "Y") + // {m + // + // } Console.WriteLine("Press 'd' then Enter to delete the history or any other key and Enter to continue"); var answer = Console.ReadLine(); Console.Clear(); @@ -129,18 +138,6 @@ public void Print() } Console.WriteLine("-----------------"); Console.WriteLine("The calculator has been used " + _history.Count + " " + (_history.Count == 1 ? "time" : "times")); - - // Console.WriteLine("/n"); - // Console.WriteLine("Would you like to use any of the above results to do a new calculation? [y/N]"); - // - // var input = Console.ReadLine(); - // - // if (input is "y" or "Y") - // { - // - // } - - } } From f0c4f36a887da7c072080c3c956d373f34441385 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Wed, 13 May 2026 13:12:52 -0400 Subject: [PATCH 12/26] removing old code --- Calculator.aaronkagan/Calculator.aaronkagan/Program.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 8f40c06d..c92abcf4 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -89,15 +89,6 @@ static void Main() case "h": { history.Print(); - // Console.WriteLine("/n"); - // Console.WriteLine("Would you like to use any of the above results to do a new calculation? [y/N]"); - // - // var input = Console.ReadLine(); - // - // if (input is "y" or "Y") - // {m - // - // } Console.WriteLine("Press 'd' then Enter to delete the history or any other key and Enter to continue"); var answer = Console.ReadLine(); Console.Clear(); From 7f702e46e7412fda18c74587ffb269561fe679d9 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Thu, 14 May 2026 13:14:59 -0400 Subject: [PATCH 13/26] creating calculation class --- .../Calculator.aaronkagan/Program.cs | 55 +++++++++++-------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index c92abcf4..517d5c66 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -65,7 +65,8 @@ static void Main() else { Console.WriteLine("Your result: {0:0.##}\n", result); - history.Add(cleanNum1, cleanNum2, op, result); + Calculation calculation = new Calculation(cleanNum1, cleanNum2, op, result); + history.Add(calculation); } } @@ -109,10 +110,25 @@ static void Main() } } + class Calculation + { + public double _leftNumber; + public double _rightNumber; + public string _operation; + public double _result; + + public Calculation(double leftNumber, double rightNumber, string @operation, double result) + { + _leftNumber = leftNumber; + _rightNumber = rightNumber; + _operation = @operation; + _result = result; + } + } + class CalculationHistory { - private List _history = []; - private List _results = []; + private List _history = []; public void Print() { if (_history.Count == 0) @@ -123,40 +139,31 @@ public void Print() { Console.WriteLine("HISTORY"); Console.WriteLine("-----------------"); + foreach (var (index, calculation) in _history.Index()) { - Console.WriteLine($"{index + 1}) {calculation}"); + char operation = calculation._operation switch + { + "a" => '+', + "s" => '-', + "m" => '*', + "d" => '/', + }; + Console.WriteLine($"{index + 1}) {calculation._leftNumber} {operation} {calculation._rightNumber} = {calculation._result}"); } + Console.WriteLine("-----------------"); Console.WriteLine("The calculator has been used " + _history.Count + " " + (_history.Count == 1 ? "time" : "times")); } } - public void Add(double left, double right, string operation, double result) + public void Add(Calculation calculation) { - string calculationText = ""; - switch (operation) - { - case "a" : - calculationText = $"{left} + {right} = {result}"; - break; - case "s" : - calculationText = $"{left} - {right} = {result}"; - break; - case "m" : - calculationText = $"{left} * {right} = {result}"; - break; - case "d" : - calculationText = $"{left} / {right} = {result}"; - break; - } - _history.Add(calculationText); - _results.Add(result); + _history.Add(calculation); } public void Delete() { _history = []; - _results = []; } } } \ No newline at end of file From 919ab71af2dd8133c7e8e3af7c9d126343f593fe Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Thu, 14 May 2026 13:22:07 -0400 Subject: [PATCH 14/26] making fields readonly --- Calculator.aaronkagan/Calculator.aaronkagan/Program.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 517d5c66..e1cde722 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -112,10 +112,10 @@ static void Main() class Calculation { - public double _leftNumber; - public double _rightNumber; - public string _operation; - public double _result; + public readonly double _leftNumber; + public readonly double _rightNumber; + public readonly string _operation; + public readonly double _result; public Calculation(double leftNumber, double rightNumber, string @operation, double result) { From 045fd736082364cecce90e853b97ebaa694a0232 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Thu, 14 May 2026 13:50:39 -0400 Subject: [PATCH 15/26] moving things around --- .../Calculator.aaronkagan/Program.cs | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index e1cde722..3222b17b 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -90,7 +90,6 @@ static void Main() case "h": { history.Print(); - Console.WriteLine("Press 'd' then Enter to delete the history or any other key and Enter to continue"); var answer = Console.ReadLine(); Console.Clear(); if (answer == "d") @@ -112,17 +111,17 @@ static void Main() class Calculation { - public readonly double _leftNumber; - public readonly double _rightNumber; - public readonly string _operation; - public readonly double _result; + public readonly double LeftNumber; + public readonly double RightNumber; + public readonly string Operation; + public readonly double Result; public Calculation(double leftNumber, double rightNumber, string @operation, double result) { - _leftNumber = leftNumber; - _rightNumber = rightNumber; - _operation = @operation; - _result = result; + LeftNumber = leftNumber; + RightNumber = rightNumber; + Operation = @operation; + Result = result; } } @@ -134,6 +133,8 @@ public void Print() if (_history.Count == 0) { Console.WriteLine("There is no history to show"); + Console.WriteLine("Press Enter to continue"); + } else { @@ -142,18 +143,20 @@ public void Print() foreach (var (index, calculation) in _history.Index()) { - char operation = calculation._operation switch + char operation = calculation.Operation switch { "a" => '+', "s" => '-', "m" => '*', "d" => '/', + _ => throw new InvalidOperationException() }; - Console.WriteLine($"{index + 1}) {calculation._leftNumber} {operation} {calculation._rightNumber} = {calculation._result}"); + Console.WriteLine($"{index + 1}) {calculation.LeftNumber} {operation} {calculation.RightNumber} = {calculation.Result}"); } Console.WriteLine("-----------------"); Console.WriteLine("The calculator has been used " + _history.Count + " " + (_history.Count == 1 ? "time" : "times")); + Console.WriteLine("Press 'd' then Enter to delete the history or Enter to continue"); } } From e4740e5b82ceda15ede65ac1225ceaecf4d75679 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Thu, 14 May 2026 15:07:40 -0400 Subject: [PATCH 16/26] implementing rider suggestions --- .../Calculator.aaronkagan/Program.cs | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 3222b17b..0aba8425 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -4,7 +4,7 @@ namespace CalculatorProgram { - class Program + static class Program { static void Main() { @@ -16,12 +16,9 @@ static void Main() CalculationHistory history = new CalculationHistory(); while (!endApp) { - string? numInput1; - string? numInput2; - double result; Console.Write("Type a number, and then press Enter: "); - numInput1 = Console.ReadLine(); + string? numInput1 = Console.ReadLine(); double cleanNum1; while (!double.TryParse(numInput1, out cleanNum1)) @@ -31,7 +28,7 @@ static void Main() } Console.Write("Type another number, and then press Enter: "); - numInput2 = Console.ReadLine(); + string? numInput2 = Console.ReadLine(); double cleanNum2; while (!double.TryParse(numInput2, out cleanNum2)) @@ -57,7 +54,7 @@ static void Main() { try { - result = calculator.DoOperation(cleanNum1, cleanNum2, op); + double result = calculator.DoOperation(cleanNum1, cleanNum2, op); if (double.IsNaN(result)) { Console.WriteLine("This operation will result in a mathematical error.\n"); @@ -109,20 +106,12 @@ static void Main() } } - class Calculation + class Calculation(double leftNumber, double rightNumber, string @operation, double result) { - public readonly double LeftNumber; - public readonly double RightNumber; - public readonly string Operation; - public readonly double Result; - - public Calculation(double leftNumber, double rightNumber, string @operation, double result) - { - LeftNumber = leftNumber; - RightNumber = rightNumber; - Operation = @operation; - Result = result; - } + public readonly double LeftNumber = leftNumber; + public readonly double RightNumber = rightNumber; + public readonly string Operation = @operation; + public readonly double Result = result; } class CalculationHistory From eed6cd11ba30cf047b480a448826ac395bad644c Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Thu, 14 May 2026 15:10:19 -0400 Subject: [PATCH 17/26] removing unnecessary code --- Calculator.aaronkagan/Calculator.aaronkagan/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 0aba8425..985e8e43 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -12,8 +12,8 @@ static void Main() Console.WriteLine("Console Calculator in C#\r"); Console.WriteLine("------------------------\n"); - Calculator calculator = new Calculator(); - CalculationHistory history = new CalculationHistory(); + Calculator calculator = new (); + CalculationHistory history = new (); while (!endApp) { @@ -62,7 +62,7 @@ static void Main() else { Console.WriteLine("Your result: {0:0.##}\n", result); - Calculation calculation = new Calculation(cleanNum1, cleanNum2, op, result); + Calculation calculation = new (cleanNum1, cleanNum2, op, result); history.Add(calculation); } From 1b74b4a199ebb48c68b5136d267b7f936b942710 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Thu, 14 May 2026 15:21:16 -0400 Subject: [PATCH 18/26] fixing flow --- .../Calculator.aaronkagan/Program.cs | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 985e8e43..27075115 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -87,12 +87,14 @@ static void Main() case "h": { history.Print(); + Console.WriteLine(history.History.Count == 0 + ? "Press Enter to continue" + : "Press 'd' then Enter to delete the history or Enter to continue"); var answer = Console.ReadLine(); Console.Clear(); if (answer == "d") { history.Delete(); - Console.WriteLine("History deleted"); } break; } @@ -116,21 +118,21 @@ class Calculation(double leftNumber, double rightNumber, string @operation, doub class CalculationHistory { - private List _history = []; + public readonly List History = []; public void Print() { - if (_history.Count == 0) + if (History.Count == 0) { Console.WriteLine("There is no history to show"); Console.WriteLine("Press Enter to continue"); - + } else { Console.WriteLine("HISTORY"); Console.WriteLine("-----------------"); - foreach (var (index, calculation) in _history.Index()) + foreach (var (index, calculation) in History.Index()) { char operation = calculation.Operation switch { @@ -144,18 +146,18 @@ public void Print() } Console.WriteLine("-----------------"); - Console.WriteLine("The calculator has been used " + _history.Count + " " + (_history.Count == 1 ? "time" : "times")); - Console.WriteLine("Press 'd' then Enter to delete the history or Enter to continue"); + Console.WriteLine("The calculator has been used " + History.Count + " " + (History.Count == 1 ? "time" : "times")); } } public void Add(Calculation calculation) { - _history.Add(calculation); + History.Add(calculation); } public void Delete() { - _history = []; + History.Clear(); + Console.WriteLine("History deleted"); } } } \ No newline at end of file From d1ae5978cbfe00222957c9df8673f4b7419e96e3 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Fri, 15 May 2026 16:52:18 -0400 Subject: [PATCH 19/26] moving loop into app class --- .../Calculator.aaronkagan/Program.cs | 214 ++++++++++-------- 1 file changed, 123 insertions(+), 91 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 27075115..5bdc9f18 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -8,77 +8,70 @@ static class Program { static void Main() { - bool endApp = false; Console.WriteLine("Console Calculator in C#\r"); Console.WriteLine("------------------------\n"); - Calculator calculator = new (); - CalculationHistory history = new (); - while (!endApp) - { + CalculatorApp calculatorApp = new(calculator); + calculatorApp.Start(); + calculator.Finish(); + } + } - Console.Write("Type a number, and then press Enter: "); - string? numInput1 = Console.ReadLine(); + class CalculatorApp + { + private readonly Calculator _calculator; - double cleanNum1; - while (!double.TryParse(numInput1, out cleanNum1)) - { - Console.Write("This is not valid input. Please enter an integer value: "); - numInput1 = Console.ReadLine(); - } + public CalculatorApp(Calculator calculator) + { + _calculator = calculator; + } - Console.Write("Type another number, and then press Enter: "); - string? numInput2 = Console.ReadLine(); + public void Start() + { + CalculationHistory history = new(); + InputHandler inputHandler = new(); + bool endApp = false; + while (!endApp) + { - double cleanNum2; - while (!double.TryParse(numInput2, out cleanNum2)) - { - Console.Write("This is not valid input. Please enter an integer value: "); - numInput2 = Console.ReadLine(); - } + Console.Write("Type a number, and then press Enter: "); + double cleanNum1 = inputHandler.GetValidNumber(); + Console.Write("Type another number, and then press Enter: "); + double cleanNum2 = inputHandler.GetValidNumber(); Console.WriteLine("Choose an operator from the following list:"); Console.WriteLine("\ta - Add"); Console.WriteLine("\ts - Subtract"); Console.WriteLine("\tm - Multiply"); Console.WriteLine("\td - Divide"); Console.Write("Your option? "); + string op = inputHandler.GetValidOperation(); - string? op = Console.ReadLine(); - - if (op == null || ! Regex.IsMatch(op, "[a|s|m|d]")) + try { - Console.WriteLine("Error: Unrecognized input."); + double result = _calculator.DoOperation(cleanNum1, cleanNum2, op); + if (double.IsNaN(result)) + { + Console.WriteLine("This operation will result in a mathematical error.\n"); + } + else + { + Console.WriteLine("Your result: {0:0.##}\n", result); + Calculation calculation = new(cleanNum1, cleanNum2, op, result); + history.Add(calculation); + } + } - else - { - try - { - double result = calculator.DoOperation(cleanNum1, cleanNum2, op); - if (double.IsNaN(result)) - { - Console.WriteLine("This operation will result in a mathematical error.\n"); - } - else - { - Console.WriteLine("Your result: {0:0.##}\n", result); - Calculation calculation = new (cleanNum1, cleanNum2, op, result); - history.Add(calculation); - } - - } - catch (Exception e) - { - Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); - } + catch (Exception e) + { + Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); } - Console.WriteLine("------------------------\n"); + Console.WriteLine("------------------------\n"); Console.Write("Press 'n' to exit, 'h' to show the calculation history or press Enter to continue: "); - var input = Console.ReadLine(); Console.Clear(); - + switch (input) { case "n": @@ -96,68 +89,107 @@ static void Main() { history.Delete(); } + break; } default: continue; } - Console.WriteLine("\n"); + Console.WriteLine("\n"); + + } - calculator.Finish(); } - } - - class Calculation(double leftNumber, double rightNumber, string @operation, double result) - { - public readonly double LeftNumber = leftNumber; - public readonly double RightNumber = rightNumber; - public readonly string Operation = @operation; - public readonly double Result = result; - } - class CalculationHistory - { - public readonly List History = []; - public void Print() + class InputHandler { - if (History.Count == 0) + public double GetValidNumber() { - Console.WriteLine("There is no history to show"); - Console.WriteLine("Press Enter to continue"); - + string? numInput = Console.ReadLine(); + + double cleanNum; + while (!double.TryParse(numInput, out cleanNum)) + { + Console.Write("This is not valid input. Please enter an integer value: "); + numInput = Console.ReadLine(); + } + + return cleanNum; } - else - { - Console.WriteLine("HISTORY"); - Console.WriteLine("-----------------"); - foreach (var (index, calculation) in History.Index()) + public string GetValidOperation() + { + string? op; + do { - char operation = calculation.Operation switch + op = Console.ReadLine(); + if (op == null || !Regex.IsMatch(op, "[a|s|m|d]")) { - "a" => '+', - "s" => '-', - "m" => '*', - "d" => '/', - _ => throw new InvalidOperationException() - }; - Console.WriteLine($"{index + 1}) {calculation.LeftNumber} {operation} {calculation.RightNumber} = {calculation.Result}"); - } - - Console.WriteLine("-----------------"); - Console.WriteLine("The calculator has been used " + History.Count + " " + (History.Count == 1 ? "time" : "times")); + Console.WriteLine("Invalid Operation. Please try again."); + } + } while (op == null || !Regex.IsMatch(op, "[a|s|m|d]")); + + return op; } - } - public void Add(Calculation calculation) + + class Calculation(double leftNumber, double rightNumber, string @operation, double result) { - History.Add(calculation); + public readonly double LeftNumber = leftNumber; + public readonly double RightNumber = rightNumber; + public readonly string Operation = @operation; + public readonly double Result = result; } - public void Delete() + + class CalculationHistory { - History.Clear(); - Console.WriteLine("History deleted"); + public readonly List History = []; + + public void Print() + { + if (History.Count == 0) + { + Console.WriteLine("There is no history to show"); + Console.WriteLine("Press Enter to continue"); + + } + else + { + Console.WriteLine("HISTORY"); + Console.WriteLine("-----------------"); + + foreach (var (index, calculation) in History.Index()) + { + char operation = calculation.Operation switch + { + "a" => '+', + "s" => '-', + "m" => '*', + "d" => '/', + _ => throw new InvalidOperationException() + }; + Console.WriteLine( + $"{index + 1}) {calculation.LeftNumber} {operation} {calculation.RightNumber} = {calculation.Result}"); + } + + Console.WriteLine("-----------------"); + Console.WriteLine("The calculator has been used " + History.Count + " " + + (History.Count == 1 ? "time" : "times")); + } + + } + + public void Add(Calculation calculation) + { + History.Add(calculation); + } + + public void Delete() + { + History.Clear(); + Console.WriteLine("History deleted"); + } } } } \ No newline at end of file From 601b4f009d200d274198356ce31b163a38818029 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Sat, 16 May 2026 11:39:21 -0400 Subject: [PATCH 20/26] moving nested classes into their own class --- .../Calculator.aaronkagan/Program.cs | 142 +++++++++--------- 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 5bdc9f18..24f2b78e 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -10,7 +10,8 @@ static void Main() { Console.WriteLine("Console Calculator in C#\r"); Console.WriteLine("------------------------\n"); - Calculator calculator = new (); + + Calculator calculator = new(); CalculatorApp calculatorApp = new(calculator); calculatorApp.Start(); calculator.Finish(); @@ -101,95 +102,94 @@ public void Start() } } + } - class InputHandler + class InputHandler + { + public double GetValidNumber() { - public double GetValidNumber() - { - string? numInput = Console.ReadLine(); + string? numInput = Console.ReadLine(); - double cleanNum; - while (!double.TryParse(numInput, out cleanNum)) - { - Console.Write("This is not valid input. Please enter an integer value: "); - numInput = Console.ReadLine(); - } - - return cleanNum; - } - - public string GetValidOperation() + double cleanNum; + while (!double.TryParse(numInput, out cleanNum)) { - string? op; - do - { - op = Console.ReadLine(); - if (op == null || !Regex.IsMatch(op, "[a|s|m|d]")) - { - Console.WriteLine("Invalid Operation. Please try again."); - } - } while (op == null || !Regex.IsMatch(op, "[a|s|m|d]")); - - return op; + Console.Write("This is not valid input. Please enter an integer value: "); + numInput = Console.ReadLine(); } - } - class Calculation(double leftNumber, double rightNumber, string @operation, double result) - { - public readonly double LeftNumber = leftNumber; - public readonly double RightNumber = rightNumber; - public readonly string Operation = @operation; - public readonly double Result = result; + return cleanNum; } - class CalculationHistory + public string GetValidOperation() { - public readonly List History = []; - - public void Print() + string? op; + do { - if (History.Count == 0) + op = Console.ReadLine(); + if (op == null || !Regex.IsMatch(op, "[a|s|m|d]")) { - Console.WriteLine("There is no history to show"); - Console.WriteLine("Press Enter to continue"); - + Console.WriteLine("Invalid Operation. Please try again."); } - else - { - Console.WriteLine("HISTORY"); - Console.WriteLine("-----------------"); + } while (op == null || !Regex.IsMatch(op, "[a|s|m|d]")); - foreach (var (index, calculation) in History.Index()) - { - char operation = calculation.Operation switch - { - "a" => '+', - "s" => '-', - "m" => '*', - "d" => '/', - _ => throw new InvalidOperationException() - }; - Console.WriteLine( - $"{index + 1}) {calculation.LeftNumber} {operation} {calculation.RightNumber} = {calculation.Result}"); - } + return op; + } + } - Console.WriteLine("-----------------"); - Console.WriteLine("The calculator has been used " + History.Count + " " + - (History.Count == 1 ? "time" : "times")); - } + class Calculation(double leftNumber, double rightNumber, string @operation, double result) + { + public readonly double LeftNumber = leftNumber; + public readonly double RightNumber = rightNumber; + public readonly string Operation = @operation; + public readonly double Result = result; + } - } + class CalculationHistory + { + public readonly List History = []; - public void Add(Calculation calculation) + public void Print() + { + if (History.Count == 0) { - History.Add(calculation); + Console.WriteLine("There is no history to show"); + Console.WriteLine("Press Enter to continue"); } - - public void Delete() + else { - History.Clear(); - Console.WriteLine("History deleted"); + Console.WriteLine("HISTORY"); + Console.WriteLine("-----------------"); + foreach (var (index, calculation) in History.Index()) + { + char operation = calculation.Operation switch + { + "a" => '+', + "s" => '-', + "m" => '*', + "d" => '/', + _ => throw new InvalidOperationException() + }; + Console.WriteLine( + $"{index + 1}) {calculation.LeftNumber} {operation} {calculation.RightNumber} = {calculation.Result}"); + } + + Console.WriteLine("-----------------"); + Console.WriteLine("The calculator has been used " + History.Count + " " + + (History.Count == 1 ? "time" : "times")); } + + } + + public void Add(Calculation calculation) + { + History.Add(calculation); + } + + public void Delete() + { + History.Clear(); + Console.WriteLine("History deleted"); } } -} \ No newline at end of file +} + From ccea261b295116bb3dbf25e7dd3c8faf4da0e912 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Sat, 16 May 2026 12:44:37 -0400 Subject: [PATCH 21/26] reorganizing --- .../Calculator.aaronkagan/Program.cs | 159 +++++++++++------- 1 file changed, 98 insertions(+), 61 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 24f2b78e..5107854a 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -26,6 +26,7 @@ public CalculatorApp(Calculator calculator) { _calculator = calculator; } + public void Start() { @@ -34,76 +35,107 @@ public void Start() bool endApp = false; while (!endApp) { - - - Console.Write("Type a number, and then press Enter: "); - double cleanNum1 = inputHandler.GetValidNumber(); - Console.Write("Type another number, and then press Enter: "); - double cleanNum2 = inputHandler.GetValidNumber(); - Console.WriteLine("Choose an operator from the following list:"); - Console.WriteLine("\ta - Add"); - Console.WriteLine("\ts - Subtract"); - Console.WriteLine("\tm - Multiply"); - Console.WriteLine("\td - Divide"); - Console.Write("Your option? "); - string op = inputHandler.GetValidOperation(); - - try - { - double result = _calculator.DoOperation(cleanNum1, cleanNum2, op); - if (double.IsNaN(result)) - { - Console.WriteLine("This operation will result in a mathematical error.\n"); - } - else - { - Console.WriteLine("Your result: {0:0.##}\n", result); - Calculation calculation = new(cleanNum1, cleanNum2, op, result); - history.Add(calculation); - } - - } - catch (Exception e) - { - Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); - } - + + var calculationRequest = ShowMainMenu(inputHandler); + RunCalculation(calculationRequest.LeftNumber, calculationRequest.RightNumber, calculationRequest.Op, history); Console.WriteLine("------------------------\n"); Console.Write("Press 'n' to exit, 'h' to show the calculation history or press Enter to continue: "); + + var input = Console.ReadLine(); Console.Clear(); - switch (input) + if (HandleUSerChoice(input, endApp, history)) + { + endApp = true; + } + + Console.WriteLine("\n"); + } + } + + private bool HandleUSerChoice(string? input, bool endApp, CalculationHistory history) + { + switch (input) + { + case "n": + endApp = true; + break; + case "h": { - case "n": - endApp = true; - break; - case "h": + history.Print(); + Console.WriteLine(history.Count() == 0 + ? "Press Enter to continue" + : "Press 'd' then Enter to delete the history or Enter to continue"); + var answer = Console.ReadLine(); + Console.Clear(); + if (answer == "d") { - history.Print(); - Console.WriteLine(history.History.Count == 0 - ? "Press Enter to continue" - : "Press 'd' then Enter to delete the history or Enter to continue"); - var answer = Console.ReadLine(); - Console.Clear(); - if (answer == "d") - { - history.Delete(); - } - - break; + history.Delete(); } - default: - continue; + + break; } + default: + Console.WriteLine("Invalid choice"); + break; + } - Console.WriteLine("\n"); + return endApp; + } + private CalculationRequest ShowMainMenu(InputHandler inputHandler) + { + Console.Write("Type a number, and then press Enter: "); + double cleanNum1 = inputHandler.GetValidNumber(); + Console.Write("Type another number, and then press Enter: "); + double cleanNum2 = inputHandler.GetValidNumber(); + Console.WriteLine("Choose an operator from the following list:"); + Console.WriteLine("\ta - Add"); + Console.WriteLine("\ts - Subtract"); + Console.WriteLine("\tm - Multiply"); + Console.WriteLine("\td - Divide"); + Console.Write("Your option? "); + string op = inputHandler.GetValidOperation(); + + CalculationRequest calculationRequest = new(cleanNum1, cleanNum2, op); + + return calculationRequest; + } + + private void RunCalculation(double cleanNum1, double cleanNum2, string op, CalculationHistory history) + { + try + { + double result = _calculator.DoOperation(cleanNum1, cleanNum2, op); + if (double.IsNaN(result)) + { + Console.WriteLine("This operation will result in a mathematical error.\n"); + } + else + { + Console.WriteLine("Your result: {0:0.##}\n", result); + Calculation calculation = new(cleanNum1, cleanNum2, op, result); + history.Add(calculation); + } } + catch (Exception e) + { + Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); + } + } } + class CalculationRequest (double cleanNumber1, double cleanNumber2, string op) + { + public double LeftNumber { get; } = cleanNumber1; + public double RightNumber { get; } = cleanNumber2; + public string Op { get; } = op; + + } + class InputHandler { public double GetValidNumber() @@ -146,11 +178,11 @@ class Calculation(double leftNumber, double rightNumber, string @operation, doub class CalculationHistory { - public readonly List History = []; + private readonly List _history = []; public void Print() { - if (History.Count == 0) + if (_history.Count == 0) { Console.WriteLine("There is no history to show"); Console.WriteLine("Press Enter to continue"); @@ -159,7 +191,7 @@ public void Print() { Console.WriteLine("HISTORY"); Console.WriteLine("-----------------"); - foreach (var (index, calculation) in History.Index()) + foreach (var (index, calculation) in _history.Index()) { char operation = calculation.Operation switch { @@ -174,22 +206,27 @@ public void Print() } Console.WriteLine("-----------------"); - Console.WriteLine("The calculator has been used " + History.Count + " " + - (History.Count == 1 ? "time" : "times")); + Console.WriteLine("The calculator has been used " + _history.Count + " " + + (_history.Count == 1 ? "time" : "times")); } } public void Add(Calculation calculation) { - History.Add(calculation); + _history.Add(calculation); } public void Delete() { - History.Clear(); + _history.Clear(); Console.WriteLine("History deleted"); } + + public int Count() + { + return _history.Count; + } } } From 1cb44b3a6763b3b812610caaafe21f321a717971 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Sun, 17 May 2026 11:08:01 -0400 Subject: [PATCH 22/26] fixing typo --- Calculator.aaronkagan/Calculator.aaronkagan/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 5107854a..2b970d61 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -45,7 +45,7 @@ public void Start() var input = Console.ReadLine(); Console.Clear(); - if (HandleUSerChoice(input, endApp, history)) + if (HandleUserChoice(input, endApp, history)) { endApp = true; } @@ -54,7 +54,7 @@ public void Start() } } - private bool HandleUSerChoice(string? input, bool endApp, CalculationHistory history) + private bool HandleUserChoice(string? input, bool endApp, CalculationHistory history) { switch (input) { From a91444b163ffe60ecb2ccfc0799e2e1c5a31a197 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Sun, 17 May 2026 17:08:19 -0400 Subject: [PATCH 23/26] OOP changes --- .../Calculator.aaronkagan/Program.cs | 111 ++++++++++++------ 1 file changed, 76 insertions(+), 35 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 2b970d61..377efa64 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -36,7 +36,7 @@ public void Start() while (!endApp) { - var calculationRequest = ShowMainMenu(inputHandler); + var calculationRequest = ShowMainMenu(inputHandler, history); RunCalculation(calculationRequest.LeftNumber, calculationRequest.RightNumber, calculationRequest.Op, history); Console.WriteLine("------------------------\n"); Console.Write("Press 'n' to exit, 'h' to show the calculation history or press Enter to continue: "); @@ -53,6 +53,7 @@ public void Start() Console.WriteLine("\n"); } } + private bool HandleUserChoice(string? input, bool endApp, CalculationHistory history) { @@ -76,31 +77,18 @@ private bool HandleUserChoice(string? input, bool endApp, CalculationHistory his break; } - default: - Console.WriteLine("Invalid choice"); - break; } return endApp; } - private CalculationRequest ShowMainMenu(InputHandler inputHandler) + private CalculationRequest ShowMainMenu(InputHandler inputHandler, CalculationHistory history) { - Console.Write("Type a number, and then press Enter: "); - double cleanNum1 = inputHandler.GetValidNumber(); - Console.Write("Type another number, and then press Enter: "); - double cleanNum2 = inputHandler.GetValidNumber(); - Console.WriteLine("Choose an operator from the following list:"); - Console.WriteLine("\ta - Add"); - Console.WriteLine("\ts - Subtract"); - Console.WriteLine("\tm - Multiply"); - Console.WriteLine("\td - Divide"); - Console.Write("Your option? "); - string op = inputHandler.GetValidOperation(); - - CalculationRequest calculationRequest = new(cleanNum1, cleanNum2, op); - - return calculationRequest; + double cleanNum1 = inputHandler.GetValidNumber(history); + double cleanNum2 = inputHandler.GetValidNumber(history); + string op = inputHandler.GetValidOperation(); + CalculationRequest calculationRequest = new(cleanNum1, cleanNum2, op); + return calculationRequest; } private void RunCalculation(double cleanNum1, double cleanNum2, string op, CalculationHistory history) @@ -138,22 +126,69 @@ class CalculationRequest (double cleanNumber1, double cleanNumber2, string op) class InputHandler { - public double GetValidNumber() + public double GetValidNumber(CalculationHistory history) { - string? numInput = Console.ReadLine(); + bool getFromHistory = false; + if (history.Count() > 0) + { + Console.WriteLine("Would you like to get the number from results history? y for yes or any other key for no: "); + Console.WriteLine(); + if (Console.ReadKey().Key == ConsoleKey.Y) + { + getFromHistory = true; + Console.WriteLine(); + } + } + + + if (getFromHistory) + { + double historyResult = GetValidHistoryResult(history); + return historyResult; + } + else + { + Console.WriteLine("Please enter a number: "); + string? numInput = Console.ReadLine(); - double cleanNum; - while (!double.TryParse(numInput, out cleanNum)) + double cleanNum; + while (!double.TryParse(numInput, out cleanNum)) + { + Console.Write("This is not valid input. Please enter an integer value: "); + numInput = Console.ReadLine(); + } + + return cleanNum; + } + } + private double GetValidHistoryResult(CalculationHistory history) + { + history.Print(); + Console.WriteLine("Which result from history would you like to use? Enter the line number then press enter: "); + string? input = Console.ReadLine(); + int cleanNum; + + while (!int.TryParse(input, out cleanNum) || cleanNum > history.Count() || cleanNum < 0) { - Console.Write("This is not valid input. Please enter an integer value: "); - numInput = Console.ReadLine(); + Console.Write("This is not valid input. Please enter a line number that exists: "); + input = Console.ReadLine(); + + int.TryParse(input, out cleanNum); } - return cleanNum; + var index = cleanNum - 1; + + return history.GetResult(index); } public string GetValidOperation() { + Console.WriteLine("Choose an operator from the following list:"); + Console.WriteLine("\ta - Add"); + Console.WriteLine("\ts - Subtract"); + Console.WriteLine("\tm - Multiply"); + Console.WriteLine("\td - Divide"); + Console.Write("Your option? "); string? op; do { @@ -178,11 +213,11 @@ class Calculation(double leftNumber, double rightNumber, string @operation, doub class CalculationHistory { - private readonly List _history = []; + private readonly List History = []; public void Print() { - if (_history.Count == 0) + if (History.Count == 0) { Console.WriteLine("There is no history to show"); Console.WriteLine("Press Enter to continue"); @@ -191,7 +226,7 @@ public void Print() { Console.WriteLine("HISTORY"); Console.WriteLine("-----------------"); - foreach (var (index, calculation) in _history.Index()) + foreach (var (index, calculation) in History.Index()) { char operation = calculation.Operation switch { @@ -206,27 +241,33 @@ public void Print() } Console.WriteLine("-----------------"); - Console.WriteLine("The calculator has been used " + _history.Count + " " + - (_history.Count == 1 ? "time" : "times")); + Console.WriteLine("The calculator has been used " + History.Count + " " + + (History.Count == 1 ? "time" : "times")); } } public void Add(Calculation calculation) { - _history.Add(calculation); + History.Add(calculation); } public void Delete() { - _history.Clear(); + History.Clear(); Console.WriteLine("History deleted"); } public int Count() { - return _history.Count; + return History.Count; } + + public double GetResult(int index) + { + return History[index].Result; + } + } } From 90406726bc372f9c5fb78e6eff1470a031b7cfc3 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Sun, 17 May 2026 17:11:11 -0400 Subject: [PATCH 24/26] removing unneccesary code --- .../Calculator.aaronkagan/Program.cs | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 377efa64..43a41a8f 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -128,38 +128,27 @@ class InputHandler { public double GetValidNumber(CalculationHistory history) { - bool getFromHistory = false; if (history.Count() > 0) { Console.WriteLine("Would you like to get the number from results history? y for yes or any other key for no: "); Console.WriteLine(); if (Console.ReadKey().Key == ConsoleKey.Y) { - getFromHistory = true; Console.WriteLine(); + double historyResult = GetValidHistoryResult(history); + return historyResult; } } - - if (getFromHistory) - { - double historyResult = GetValidHistoryResult(history); - return historyResult; - } - else + Console.WriteLine("Please enter a number: "); + string? numInput = Console.ReadLine(); + double cleanNum; + while (!double.TryParse(numInput, out cleanNum)) { - Console.WriteLine("Please enter a number: "); - string? numInput = Console.ReadLine(); - - double cleanNum; - while (!double.TryParse(numInput, out cleanNum)) - { - Console.Write("This is not valid input. Please enter an integer value: "); - numInput = Console.ReadLine(); - } - - return cleanNum; + Console.Write("This is not valid input. Please enter an integer value: "); + numInput = Console.ReadLine(); } + return cleanNum; } private double GetValidHistoryResult(CalculationHistory history) { From a0ae28b71f6f41d59502271256e661a1206e6805 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Sun, 17 May 2026 17:18:28 -0400 Subject: [PATCH 25/26] movign classes to own files --- .../Calculator.aaronkagan/Calculation.cs | 9 + .../CalculationHistory.cs | 60 +++++ .../CalculationRequest.cs | 9 + .../Calculator.aaronkagan/CalculatorApp.cs | 101 ++++++++ .../Calculator.aaronkagan/InputHandler.cs | 71 +++++ .../Calculator.aaronkagan/Program.cs | 245 +----------------- 6 files changed, 251 insertions(+), 244 deletions(-) create mode 100644 Calculator.aaronkagan/Calculator.aaronkagan/Calculation.cs create mode 100644 Calculator.aaronkagan/Calculator.aaronkagan/CalculationHistory.cs create mode 100644 Calculator.aaronkagan/Calculator.aaronkagan/CalculationRequest.cs create mode 100644 Calculator.aaronkagan/Calculator.aaronkagan/CalculatorApp.cs create mode 100644 Calculator.aaronkagan/Calculator.aaronkagan/InputHandler.cs diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Calculation.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Calculation.cs new file mode 100644 index 00000000..9eb859ac --- /dev/null +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Calculation.cs @@ -0,0 +1,9 @@ +namespace CalculatorProgram; + +class Calculation(double leftNumber, double rightNumber, string @operation, double result) +{ + public readonly double LeftNumber = leftNumber; + public readonly double RightNumber = rightNumber; + public readonly string Operation = @operation; + public readonly double Result = result; +} \ No newline at end of file diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/CalculationHistory.cs b/Calculator.aaronkagan/Calculator.aaronkagan/CalculationHistory.cs new file mode 100644 index 00000000..4e191dc8 --- /dev/null +++ b/Calculator.aaronkagan/Calculator.aaronkagan/CalculationHistory.cs @@ -0,0 +1,60 @@ +namespace CalculatorProgram; + +class CalculationHistory +{ + private readonly List History = []; + + public void Print() + { + if (History.Count == 0) + { + Console.WriteLine("There is no history to show"); + Console.WriteLine("Press Enter to continue"); + } + else + { + Console.WriteLine("HISTORY"); + Console.WriteLine("-----------------"); + foreach (var (index, calculation) in History.Index()) + { + char operation = calculation.Operation switch + { + "a" => '+', + "s" => '-', + "m" => '*', + "d" => '/', + _ => throw new InvalidOperationException() + }; + Console.WriteLine( + $"{index + 1}) {calculation.LeftNumber} {operation} {calculation.RightNumber} = {calculation.Result}"); + } + + Console.WriteLine("-----------------"); + Console.WriteLine("The calculator has been used " + History.Count + " " + + (History.Count == 1 ? "time" : "times")); + } + + } + + public void Add(Calculation calculation) + { + History.Add(calculation); + } + + public void Delete() + { + History.Clear(); + Console.WriteLine("History deleted"); + } + + public int Count() + { + return History.Count; + } + + public double GetResult(int index) + { + return History[index].Result; + } + +} \ No newline at end of file diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/CalculationRequest.cs b/Calculator.aaronkagan/Calculator.aaronkagan/CalculationRequest.cs new file mode 100644 index 00000000..532d1433 --- /dev/null +++ b/Calculator.aaronkagan/Calculator.aaronkagan/CalculationRequest.cs @@ -0,0 +1,9 @@ +namespace CalculatorProgram; + +class CalculationRequest (double cleanNumber1, double cleanNumber2, string op) +{ + public double LeftNumber { get; } = cleanNumber1; + public double RightNumber { get; } = cleanNumber2; + public string Op { get; } = op; + +} \ No newline at end of file diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/CalculatorApp.cs b/Calculator.aaronkagan/Calculator.aaronkagan/CalculatorApp.cs new file mode 100644 index 00000000..55e65d3d --- /dev/null +++ b/Calculator.aaronkagan/Calculator.aaronkagan/CalculatorApp.cs @@ -0,0 +1,101 @@ +using CalculatorLibrary; + +namespace CalculatorProgram; + +class CalculatorApp +{ + private readonly Calculator _calculator; + + public CalculatorApp(Calculator calculator) + { + _calculator = calculator; + } + + + public void Start() + { + CalculationHistory history = new(); + InputHandler inputHandler = new(); + bool endApp = false; + while (!endApp) + { + + var calculationRequest = ShowMainMenu(inputHandler, history); + RunCalculation(calculationRequest.LeftNumber, calculationRequest.RightNumber, calculationRequest.Op, history); + Console.WriteLine("------------------------\n"); + Console.Write("Press 'n' to exit, 'h' to show the calculation history or press Enter to continue: "); + + + var input = Console.ReadLine(); + Console.Clear(); + + if (HandleUserChoice(input, endApp, history)) + { + endApp = true; + } + + Console.WriteLine("\n"); + } + } + + + private bool HandleUserChoice(string? input, bool endApp, CalculationHistory history) + { + switch (input) + { + case "n": + endApp = true; + break; + case "h": + { + history.Print(); + Console.WriteLine(history.Count() == 0 + ? "Press Enter to continue" + : "Press 'd' then Enter to delete the history or Enter to continue"); + var answer = Console.ReadLine(); + Console.Clear(); + if (answer == "d") + { + history.Delete(); + } + + break; + } + } + + return endApp; + } + + private CalculationRequest ShowMainMenu(InputHandler inputHandler, CalculationHistory history) + { + double cleanNum1 = inputHandler.GetValidNumber(history); + double cleanNum2 = inputHandler.GetValidNumber(history); + string op = inputHandler.GetValidOperation(); + CalculationRequest calculationRequest = new(cleanNum1, cleanNum2, op); + return calculationRequest; + } + + private void RunCalculation(double cleanNum1, double cleanNum2, string op, CalculationHistory history) + { + try + { + double result = _calculator.DoOperation(cleanNum1, cleanNum2, op); + if (double.IsNaN(result)) + { + Console.WriteLine("This operation will result in a mathematical error.\n"); + } + else + { + Console.WriteLine("Your result: {0:0.##}\n", result); + Calculation calculation = new(cleanNum1, cleanNum2, op, result); + history.Add(calculation); + } + + } + catch (Exception e) + { + Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); + } + + } +} \ No newline at end of file diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/InputHandler.cs b/Calculator.aaronkagan/Calculator.aaronkagan/InputHandler.cs new file mode 100644 index 00000000..1e939b77 --- /dev/null +++ b/Calculator.aaronkagan/Calculator.aaronkagan/InputHandler.cs @@ -0,0 +1,71 @@ +using System.Text.RegularExpressions; + +namespace CalculatorProgram; + +class InputHandler +{ + public double GetValidNumber(CalculationHistory history) + { + if (history.Count() > 0) + { + Console.WriteLine("Would you like to get the number from results history? y for yes or any other key for no: "); + Console.WriteLine(); + if (Console.ReadKey().Key == ConsoleKey.Y) + { + Console.WriteLine(); + double historyResult = GetValidHistoryResult(history); + return historyResult; + } + } + + Console.WriteLine("Please enter a number: "); + string? numInput = Console.ReadLine(); + double cleanNum; + while (!double.TryParse(numInput, out cleanNum)) + { + Console.Write("This is not valid input. Please enter an integer value: "); + numInput = Console.ReadLine(); + } + return cleanNum; + } + private double GetValidHistoryResult(CalculationHistory history) + { + history.Print(); + Console.WriteLine("Which result from history would you like to use? Enter the line number then press enter: "); + string? input = Console.ReadLine(); + int cleanNum; + + while (!int.TryParse(input, out cleanNum) || cleanNum > history.Count() || cleanNum < 0) + { + Console.Write("This is not valid input. Please enter a line number that exists: "); + input = Console.ReadLine(); + + int.TryParse(input, out cleanNum); + } + + var index = cleanNum - 1; + + return history.GetResult(index); + } + + public string GetValidOperation() + { + Console.WriteLine("Choose an operator from the following list:"); + Console.WriteLine("\ta - Add"); + Console.WriteLine("\ts - Subtract"); + Console.WriteLine("\tm - Multiply"); + Console.WriteLine("\td - Divide"); + Console.Write("Your option? "); + string? op; + do + { + op = Console.ReadLine(); + if (op == null || !Regex.IsMatch(op, "[a|s|m|d]")) + { + Console.WriteLine("Invalid Operation. Please try again."); + } + } while (op == null || !Regex.IsMatch(op, "[a|s|m|d]")); + + return op; + } +} \ No newline at end of file diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs index 43a41a8f..fb618d91 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/Program.cs @@ -1,9 +1,7 @@ -using System.Text.RegularExpressions; -using CalculatorLibrary; +using CalculatorLibrary; namespace CalculatorProgram { - static class Program { static void Main() @@ -17,246 +15,5 @@ static void Main() calculator.Finish(); } } - - class CalculatorApp - { - private readonly Calculator _calculator; - - public CalculatorApp(Calculator calculator) - { - _calculator = calculator; - } - - - public void Start() - { - CalculationHistory history = new(); - InputHandler inputHandler = new(); - bool endApp = false; - while (!endApp) - { - - var calculationRequest = ShowMainMenu(inputHandler, history); - RunCalculation(calculationRequest.LeftNumber, calculationRequest.RightNumber, calculationRequest.Op, history); - Console.WriteLine("------------------------\n"); - Console.Write("Press 'n' to exit, 'h' to show the calculation history or press Enter to continue: "); - - - var input = Console.ReadLine(); - Console.Clear(); - - if (HandleUserChoice(input, endApp, history)) - { - endApp = true; - } - - Console.WriteLine("\n"); - } - } - - - private bool HandleUserChoice(string? input, bool endApp, CalculationHistory history) - { - switch (input) - { - case "n": - endApp = true; - break; - case "h": - { - history.Print(); - Console.WriteLine(history.Count() == 0 - ? "Press Enter to continue" - : "Press 'd' then Enter to delete the history or Enter to continue"); - var answer = Console.ReadLine(); - Console.Clear(); - if (answer == "d") - { - history.Delete(); - } - - break; - } - } - - return endApp; - } - - private CalculationRequest ShowMainMenu(InputHandler inputHandler, CalculationHistory history) - { - double cleanNum1 = inputHandler.GetValidNumber(history); - double cleanNum2 = inputHandler.GetValidNumber(history); - string op = inputHandler.GetValidOperation(); - CalculationRequest calculationRequest = new(cleanNum1, cleanNum2, op); - return calculationRequest; - } - - private void RunCalculation(double cleanNum1, double cleanNum2, string op, CalculationHistory history) - { - try - { - double result = _calculator.DoOperation(cleanNum1, cleanNum2, op); - if (double.IsNaN(result)) - { - Console.WriteLine("This operation will result in a mathematical error.\n"); - } - else - { - Console.WriteLine("Your result: {0:0.##}\n", result); - Calculation calculation = new(cleanNum1, cleanNum2, op, result); - history.Add(calculation); - } - - } - catch (Exception e) - { - Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message); - } - - } - } - - class CalculationRequest (double cleanNumber1, double cleanNumber2, string op) - { - public double LeftNumber { get; } = cleanNumber1; - public double RightNumber { get; } = cleanNumber2; - public string Op { get; } = op; - - } - - class InputHandler - { - public double GetValidNumber(CalculationHistory history) - { - if (history.Count() > 0) - { - Console.WriteLine("Would you like to get the number from results history? y for yes or any other key for no: "); - Console.WriteLine(); - if (Console.ReadKey().Key == ConsoleKey.Y) - { - Console.WriteLine(); - double historyResult = GetValidHistoryResult(history); - return historyResult; - } - } - - Console.WriteLine("Please enter a number: "); - string? numInput = Console.ReadLine(); - double cleanNum; - while (!double.TryParse(numInput, out cleanNum)) - { - Console.Write("This is not valid input. Please enter an integer value: "); - numInput = Console.ReadLine(); - } - return cleanNum; - } - private double GetValidHistoryResult(CalculationHistory history) - { - history.Print(); - Console.WriteLine("Which result from history would you like to use? Enter the line number then press enter: "); - string? input = Console.ReadLine(); - int cleanNum; - - while (!int.TryParse(input, out cleanNum) || cleanNum > history.Count() || cleanNum < 0) - { - Console.Write("This is not valid input. Please enter a line number that exists: "); - input = Console.ReadLine(); - - int.TryParse(input, out cleanNum); - } - - var index = cleanNum - 1; - - return history.GetResult(index); - } - - public string GetValidOperation() - { - Console.WriteLine("Choose an operator from the following list:"); - Console.WriteLine("\ta - Add"); - Console.WriteLine("\ts - Subtract"); - Console.WriteLine("\tm - Multiply"); - Console.WriteLine("\td - Divide"); - Console.Write("Your option? "); - string? op; - do - { - op = Console.ReadLine(); - if (op == null || !Regex.IsMatch(op, "[a|s|m|d]")) - { - Console.WriteLine("Invalid Operation. Please try again."); - } - } while (op == null || !Regex.IsMatch(op, "[a|s|m|d]")); - - return op; - } - } - - class Calculation(double leftNumber, double rightNumber, string @operation, double result) - { - public readonly double LeftNumber = leftNumber; - public readonly double RightNumber = rightNumber; - public readonly string Operation = @operation; - public readonly double Result = result; - } - - class CalculationHistory - { - private readonly List History = []; - - public void Print() - { - if (History.Count == 0) - { - Console.WriteLine("There is no history to show"); - Console.WriteLine("Press Enter to continue"); - } - else - { - Console.WriteLine("HISTORY"); - Console.WriteLine("-----------------"); - foreach (var (index, calculation) in History.Index()) - { - char operation = calculation.Operation switch - { - "a" => '+', - "s" => '-', - "m" => '*', - "d" => '/', - _ => throw new InvalidOperationException() - }; - Console.WriteLine( - $"{index + 1}) {calculation.LeftNumber} {operation} {calculation.RightNumber} = {calculation.Result}"); - } - - Console.WriteLine("-----------------"); - Console.WriteLine("The calculator has been used " + History.Count + " " + - (History.Count == 1 ? "time" : "times")); - } - - } - - public void Add(Calculation calculation) - { - History.Add(calculation); - } - - public void Delete() - { - History.Clear(); - Console.WriteLine("History deleted"); - } - - public int Count() - { - return History.Count; - } - - public double GetResult(int index) - { - return History[index].Result; - } - - } } From c2aacc3ee98fa473a67d92eded7cc84ecd025ed0 Mon Sep 17 00:00:00 2001 From: Aaron Kagan Date: Sun, 17 May 2026 17:22:47 -0400 Subject: [PATCH 26/26] fixing UI --- Calculator.aaronkagan/Calculator.aaronkagan/InputHandler.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Calculator.aaronkagan/Calculator.aaronkagan/InputHandler.cs b/Calculator.aaronkagan/Calculator.aaronkagan/InputHandler.cs index 1e939b77..d1e399b1 100644 --- a/Calculator.aaronkagan/Calculator.aaronkagan/InputHandler.cs +++ b/Calculator.aaronkagan/Calculator.aaronkagan/InputHandler.cs @@ -9,7 +9,6 @@ public double GetValidNumber(CalculationHistory history) if (history.Count() > 0) { Console.WriteLine("Would you like to get the number from results history? y for yes or any other key for no: "); - Console.WriteLine(); if (Console.ReadKey().Key == ConsoleKey.Y) { Console.WriteLine(); @@ -17,7 +16,8 @@ public double GetValidNumber(CalculationHistory history) return historyResult; } } - + + Console.WriteLine(); Console.WriteLine("Please enter a number: "); string? numInput = Console.ReadLine(); double cleanNum;