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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions ConsoleCalculator/CalculatorLibrary/CalculatorLibrary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using Newtonsoft.Json;

namespace CalculatorLibrary
{
public class Calculator
{

JsonWriter writer;
private int uses;

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();

uses++;
return result;
}

public void Finish()
{
writer.WriteEndArray();
writer.WriteEndObject();
writer.Close();
}

public int getUses()
{
return uses;
}

public void setUses(int input)
{
uses = input;
}
}
}
13 changes: 13 additions & 0 deletions ConsoleCalculator/CalculatorLibrary/CalculatorLibrary.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions ConsoleCalculator/ConsoleCalculator.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Solution>
<Project Path="CalculatorLibrary/CalculatorLibrary.csproj" />
<Project Path="ConsoleCalculator/ConsoleCalculator.csproj" />
<Project Path="OperationLog/OperationLog.csproj" />
</Solution>
19 changes: 19 additions & 0 deletions ConsoleCalculator/ConsoleCalculator/ConsoleCalculator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CalculatorLibrary\CalculatorLibrary.csproj" />
<ProjectReference Include="..\OperationLog\OperationLog.csproj" />
</ItemGroup>

</Project>
84 changes: 84 additions & 0 deletions ConsoleCalculator/ConsoleCalculator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using CalculatorLibrary;
using System.Text.RegularExpressions;

namespace CalculatorProgram
{
class Program
{
static void Main(string[] args)
{
bool endApp = false;

Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");

Calculator calculator = new Calculator();
while (!endApp)
{
string? numInput1;
string? numInput2;
double result;

Console.Write("Type a number, then press Enter: ");
numInput1 = Console.ReadLine();

double cleanNum1;
while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter an numeric value: ");
numInput1 = Console.ReadLine();
}

Console.Write("Type another number, then press Enter: ");
numInput2 = Console.ReadLine();

double cleanNum2;
while (!double.TryParse(numInput2, out cleanNum2))
{
Console.Write("This is not valid input. Please enter a numeric value: ");
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? ");

string? op = Console.ReadLine();

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");
Console.Write("Type 'q' to close the app, 'n' to display total # of uses, or press any other key and Enter to continue: ");

string? next = Console.ReadLine();
if (next == "n") Console.WriteLine($"# of Equations: {calculator.getUses()}");
if (next == "q") endApp = true;

Console.WriteLine("\n");
}
calculator.Finish();
return;
}
}
}