C#C
C#3y ago
Mek

✅ ✅ C# Academy Calculator Project Challenge -- Counting Times Used

Calculator.mekasu0124/Program.cs - https://pastebin.com/Wvz9eHUM
ClassLibrary/CalculatorLibrary.cs - https://pastebin.com/qucuTXXm

This project had us create 2 projects in one. The calculator and the class library. One of the challenges is to keep track of the number of times the calculator was used. I want to do this based off when the user receives a result, not when the program may error due to
Invalid Inputs
,
This operation will result in a mathematical error
, or
Oh no! An exception occurred trying to do the math.\n - Details: ", + e.Message
. This project had a requirement of following the tutorial on https://learn.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-console?view=vs-2022 and so I did which is the same code in the top two links. In
CalculatorLibrary.cs
we use
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("Usage");
    writer.WriteValue(0);
    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();
}
however in
Program.cs
we use this try case
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);
}
to determine the result and whether it errors or not. Under the line
Console.WriteLine("Your result: {0:0.##}\,", result);
I want to add an
UpdateCount();
function. I attempted to do
using System.Text.Json;

public static void UpdateCount()
{
    string file = "calculatorlog.json";
    var jsonObj = JsonSerializer.Deserialize(file);
    jsonObj["Usage"]++;
    var jsonObj2 = JsonSerializer.Serialize(jsonObj, file);
}
but this didn't work. So, following the same code patterns and such in the files, how would I increment the usage variable that is wrttien in at the top of the
CalculatorLibrary.cs
file in the
public Calculator()
function where I have
writer.WritePropertyName("Usage"); writer.WriteValue(0);
?
Was this page helpful?