From 79a9f5d40d364436ce8d428b58d7d3635de0e157 Mon Sep 17 00:00:00 2001 From: antony-k-sebastian Date: Mon, 3 Aug 2026 15:41:25 +0100 Subject: [PATCH 1/3] Calculator Part 1 --- CodeReviews.Console.Calculator.csproj | 10 +++ Program.cs | 117 ++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 CodeReviews.Console.Calculator.csproj create mode 100644 Program.cs diff --git a/CodeReviews.Console.Calculator.csproj b/CodeReviews.Console.Calculator.csproj new file mode 100644 index 00000000..fd4bd08d --- /dev/null +++ b/CodeReviews.Console.Calculator.csproj @@ -0,0 +1,10 @@ + + + + Exe + net9.0 + enable + enable + + + diff --git a/Program.cs b/Program.cs new file mode 100644 index 00000000..446825d2 --- /dev/null +++ b/Program.cs @@ -0,0 +1,117 @@ + +using System.Text.RegularExpressions; + +class Calculator +{ + public static double DoOperation(double num1, double num2, string op) + { + double result = double.NaN; // Default value is "not-a-number" if an operation, such as division, could result in an error. + + // Use a switch statement to do the math. + switch (op) + { + case "a": + result = num1 + num2; + break; + case "s": + result = num1 - num2; + break; + case "m": + result = num1 * num2; + break; + case "d": + // Ask the user to enter a non-zero divisor. + if (num2 != 0) + { + result = num1 / num2; + } + break; + // Return text for an incorrect option entry. + 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 4bdb04fe0186246a96b15f9268444da2e692e827 Mon Sep 17 00:00:00 2001 From: antony-k-sebastian Date: Mon, 3 Aug 2026 19:54:19 +0100 Subject: [PATCH 2/3] Json Log --- Calculator.sln | 28 ++++++++ CalculatorLibrary/CalculatorLibrary.cs | 72 +++++++++++++++++++ CalculatorLibrary/CalculatorLibrary.csproj | 13 ++++ .../.codacy.yml | 0 .../CodeReviews.Console.Calculator.csproj | 4 ++ .../Program.cs | 41 ++--------- .../calculatorlog.json | 10 +++ 7 files changed, 131 insertions(+), 37 deletions(-) create mode 100644 Calculator.sln create mode 100644 CalculatorLibrary/CalculatorLibrary.cs create mode 100644 CalculatorLibrary/CalculatorLibrary.csproj rename .codacy.yml => CodeReviews.Console.Calculator/.codacy.yml (100%) rename CodeReviews.Console.Calculator.csproj => CodeReviews.Console.Calculator/CodeReviews.Console.Calculator.csproj (68%) rename Program.cs => CodeReviews.Console.Calculator/Program.cs (74%) create mode 100644 CodeReviews.Console.Calculator/calculatorlog.json diff --git a/Calculator.sln b/Calculator.sln new file mode 100644 index 00000000..4d23c69a --- /dev/null +++ b/Calculator.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeReviews.Console.Calculator", "CodeReviews.Console.Calculator\CodeReviews.Console.Calculator.csproj", "{526DCBA8-896A-4097-A3B9-B21A57F0D9ED}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CalculatorLibrary", "CalculatorLibrary\CalculatorLibrary.csproj", "{F5A74BD6-8F67-44DB-A6FD-2D5046EAE9C6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {526DCBA8-896A-4097-A3B9-B21A57F0D9ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {526DCBA8-896A-4097-A3B9-B21A57F0D9ED}.Debug|Any CPU.Build.0 = Debug|Any CPU + {526DCBA8-896A-4097-A3B9-B21A57F0D9ED}.Release|Any CPU.ActiveCfg = Release|Any CPU + {526DCBA8-896A-4097-A3B9-B21A57F0D9ED}.Release|Any CPU.Build.0 = Release|Any CPU + {F5A74BD6-8F67-44DB-A6FD-2D5046EAE9C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F5A74BD6-8F67-44DB-A6FD-2D5046EAE9C6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F5A74BD6-8F67-44DB-A6FD-2D5046EAE9C6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F5A74BD6-8F67-44DB-A6FD-2D5046EAE9C6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/CalculatorLibrary/CalculatorLibrary.cs b/CalculatorLibrary/CalculatorLibrary.cs new file mode 100644 index 00000000..098ae38b --- /dev/null +++ b/CalculatorLibrary/CalculatorLibrary.cs @@ -0,0 +1,72 @@ +namespace CalculatorLibrary; + +using System.Diagnostics; +using Newtonsoft.Json; + +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; // Default value is "not-a-number" if an operation, such as division, could result in an error. + writer.WriteStartObject(); + writer.WritePropertyName("operand1"); + writer.WriteValue(num1); + writer.WritePropertyName("opearand2"); + writer.WriteValue(num2); + writer.WritePropertyName("opearation"); + + // Use a switch statement to do the math. + switch (op) + { + case "a": + result = num1 + num2; + writer.WriteValue("Add"); + break; + case "s": + result = num1 - num2; + writer.WriteValue("Substract"); + break; + case "m": + result = num1 * num2; + writer.WriteValue("Multiply"); + break; + case "d": + // Ask the user to enter a non-zero divisor. + if (num2 != 0) + { + result = num1 / num2; + writer.WriteValue("Division"); + } + break; + // Return text for an incorrect option entry. + default: + break; + } + writer.WritePropertyName("Result"); + writer.WriteValue(result); + writer.WriteEndObject(); + + Finish(); + return result; + } + + public void Finish() + { + writer.WriteEndArray(); + writer.WriteEndObject(); + writer.Close(); + } +} + + diff --git a/CalculatorLibrary/CalculatorLibrary.csproj b/CalculatorLibrary/CalculatorLibrary.csproj new file mode 100644 index 00000000..181661d7 --- /dev/null +++ b/CalculatorLibrary/CalculatorLibrary.csproj @@ -0,0 +1,13 @@ + + + + net9.0 + enable + enable + + + + + + + diff --git a/.codacy.yml b/CodeReviews.Console.Calculator/.codacy.yml similarity index 100% rename from .codacy.yml rename to CodeReviews.Console.Calculator/.codacy.yml diff --git a/CodeReviews.Console.Calculator.csproj b/CodeReviews.Console.Calculator/CodeReviews.Console.Calculator.csproj similarity index 68% rename from CodeReviews.Console.Calculator.csproj rename to CodeReviews.Console.Calculator/CodeReviews.Console.Calculator.csproj index fd4bd08d..c49d67a7 100644 --- a/CodeReviews.Console.Calculator.csproj +++ b/CodeReviews.Console.Calculator/CodeReviews.Console.Calculator.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/Program.cs b/CodeReviews.Console.Calculator/Program.cs similarity index 74% rename from Program.cs rename to CodeReviews.Console.Calculator/Program.cs index 446825d2..5e428059 100644 --- a/Program.cs +++ b/CodeReviews.Console.Calculator/Program.cs @@ -1,44 +1,11 @@ - -using System.Text.RegularExpressions; - -class Calculator -{ - public static double DoOperation(double num1, double num2, string op) - { - double result = double.NaN; // Default value is "not-a-number" if an operation, such as division, could result in an error. - - // Use a switch statement to do the math. - switch (op) - { - case "a": - result = num1 + num2; - break; - case "s": - result = num1 - num2; - break; - case "m": - result = num1 * num2; - break; - case "d": - // Ask the user to enter a non-zero divisor. - if (num2 != 0) - { - result = num1 / num2; - } - break; - // Return text for an incorrect option entry. - default: - break; - } - return result; - } -} - +using System.Text.RegularExpressions; +using CalculatorLibrary; class Program { static void Main(string[] args) { bool endApp = false; + Calculator calculator = new Calculator(); // Display title as the C# console calculator app. Console.WriteLine("Console Calculator in C#\r"); Console.WriteLine("------------------------\n"); @@ -92,7 +59,7 @@ static void Main(string[] args) { try { - result = Calculator.DoOperation(cleanNum1, cleanNum2, op); + result = calculator.DoOperation(cleanNum1, cleanNum2, op); if (double.IsNaN(result)) { Console.WriteLine("This operation will result in a mathematical error.\n"); diff --git a/CodeReviews.Console.Calculator/calculatorlog.json b/CodeReviews.Console.Calculator/calculatorlog.json new file mode 100644 index 00000000..7d984bee --- /dev/null +++ b/CodeReviews.Console.Calculator/calculatorlog.json @@ -0,0 +1,10 @@ +{ + "operations": [ + { + "operand1": 23.0, + "opearand2": 23.0, + "opearation": "Add", + "Result": 46.0 + } + ] +} \ No newline at end of file From ce0f86eadb30e9994d30bd26f17175fa7b6060b7 Mon Sep 17 00:00:00 2001 From: antony-k-sebastian Date: Fri, 21 Aug 2026 12:41:35 +0100 Subject: [PATCH 3/3] Fix: Breaks when trying more than one operation --- CodeReviews.Console.Calculator/Program.cs | 3 ++- CodeReviews.Console.Calculator/calculatorlog.json | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CodeReviews.Console.Calculator/Program.cs b/CodeReviews.Console.Calculator/Program.cs index 5e428059..397f3791 100644 --- a/CodeReviews.Console.Calculator/Program.cs +++ b/CodeReviews.Console.Calculator/Program.cs @@ -5,13 +5,14 @@ class Program static void Main(string[] args) { bool endApp = false; - Calculator calculator = new Calculator(); // Display title as the C# console calculator app. Console.WriteLine("Console Calculator in C#\r"); Console.WriteLine("------------------------\n"); while (!endApp) { + Calculator calculator = new Calculator(); + // Declare variables and set to empty. // Use Nullable types (with ?) to match type of System.Console.ReadLine string? numInput1 = ""; diff --git a/CodeReviews.Console.Calculator/calculatorlog.json b/CodeReviews.Console.Calculator/calculatorlog.json index 7d984bee..1b79a3db 100644 --- a/CodeReviews.Console.Calculator/calculatorlog.json +++ b/CodeReviews.Console.Calculator/calculatorlog.json @@ -3,8 +3,8 @@ { "operand1": 23.0, "opearand2": 23.0, - "opearation": "Add", - "Result": 46.0 + "opearation": "Division", + "Result": 1.0 } ] } \ No newline at end of file