C
C#14mo ago
Mekasu0124

✅ ✅ 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();
}
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);
}
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);
}
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);?
10 Replies
Angius
Angius14mo ago
Well, uh, a json serializer serializes/deserializes... Json, not files. It can take a stream, but not a file path So... read file -> deserialize to a class -> increment a property -> serialize it back -> write it back to the file Alternatively, the same but with a stream
Mekasu0124
Mekasu0124OP14mo ago
ok so the tutorial doesn't use model classes within it
public static void UpdateCount()
{
string jsonFile = "calculatorlog.json";
var jsonData = File.ReadAllText(jsonFile);
var jsonObj = JsonSerializer.Deserialize(jsonData);
}
}
}

public class FileModel
{
public int Usage { get; set; }
public List<OperationsModel> Operations { get; set; }
}

public class OperationsModel
{
public double Operand1 { get; set; }
public double Operand2 { get; set; }
public string Operation { get; set; }
public double Result { get; set; }
}
public static void UpdateCount()
{
string jsonFile = "calculatorlog.json";
var jsonData = File.ReadAllText(jsonFile);
var jsonObj = JsonSerializer.Deserialize(jsonData);
}
}
}

public class FileModel
{
public int Usage { get; set; }
public List<OperationsModel> Operations { get; set; }
}

public class OperationsModel
{
public double Operand1 { get; set; }
public double Operand2 { get; set; }
public string Operation { get; set; }
public double Result { get; set; }
}
so here's where I'm at in Program.cs
Angius
Angius14mo ago
Ah, they're just discarding type safety and rawdogging it with JObject or something, gotcha Well, not the way I'd do it lol
Mekasu0124
Mekasu0124OP14mo ago
I fixed my code. It should've been public List<OperationsModel> Operations isntead I, honestly, wouldn't do it the way the tutorial said to do it either. Especially when using models. I don't even use the JsonWriter that they used here either. I just use the regular way
Angius
Angius14mo ago
I'm gonna be honest, I never worked with type-unsafe Json handling, so not sure if I'll be of any help there
Mekasu0124
Mekasu0124OP14mo ago
I do not mind making this into type-safe classes whatsoever. I'm just not familiar with how they used JsonWriter to do the storing of the data so I have next to zero idea on how to convert the code in the CalculatorLibrary.cs file I could made a save operation function that would take the num1, num2, operation, and result and put it through the OperationsModel class and then write that information, but I don't know if that would fail me for the project
Angius
Angius14mo ago
Right, yeah Some teachers can really be fossilized hard in their ways I'll be honest, no clue
Mekasu0124
Mekasu0124OP14mo ago
I think I'm going to ask them if I can do the calculator in a different way than the tutorial that way I can include type-safe classes I'm waiting for a response, but I'm just going to re-write it anyways and attempt to submit while I"m waiting for a response. Thank you for attempting to help ❤️
Mekasu0124
Mekasu0124OP14mo ago
So the purpose of the project is learning documentation and following it. Which is understandable. I still am unsure of how to use the StreamWriter the JsonWriter the way they have to keep track of the number of times the calculator is used, however, without the challenges included I’ve already passed the project so I’m going to move on for now to the next project. On a later date, I may re-write my own calculator to do the things they’re offering as challenges and incorporate type-safe data storage and usage, but for now I’m not going to worry with it
No description
No description
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server