Skip to content

Conversation

@Hellothereyoko
Copy link

@Hellothereyoko Hellothereyoko commented Nov 3, 2025

Worked with Ryan Hirte

RyanHirte and others added 15 commits November 1, 2025 10:08
Followed instructions for program class I think it's done.
Work that I've done for assignment 6
 Changes to be committed:

	new file:   .vscode/launch.json
        - For some reason my C# Env was bugging out until I did this step.

	modified:   Calculate..sln
        - reconfiguration to include new Calculator and CalculatorTests projects.

	new file:   Calculate.Tests/CalculatorTests.cs
        - Added unit tests for mathematical operations and calculation logic.

	new file:   Calculate/Calculator.cs
        - Implemented basic arithmetic operations using lambda expressions.
Copy link

@Al3xramirez Al3xramirez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a Console project called "Calculate.". ✔
Define a Program Class
Define two init-only setter properties, WriteLine and ReadLine, that contain delegates for writing a line of text and reading a line of text respectively ✔
Write a test that sets these properties at construction time and then invokes the properties and verifies the expected behavior occurs. ✔
Set the default behavior for the WriteLine and ReadLine properties to invoke System.Console versions of the methods and add an empty default constructor. ✔
Define a Calculator class ✔
Define static Add, Subtract, Multiple, and Divide methods that have two parameters and return a third parameter. ✔
Define a read-only property, MathematicalOperations, of type System.Collections.Generics.IReadOnlyDictionary<TKey,TValue> that:
is initialized to a System.Collections.Generics.Dictionary<<TKey,TValue> instance that. ✔
Uses char for the key corresponding to the operators +, -, *, and /. ✔
Has values that correspond with the Add, Subtract, Multiple, and Divide methods. ✔
Implement a TryCalculate method following "TryParse" pattern ✔
Valid calculation expressions include such strings as "3 + 4", "42 - 2", etc. ✔
If there is no whitespace around the operator, you can assume the calculation is invalid and return false. Similarly if the operands are not integers. ✔
Use string.Split(), pattern matching, logical and operators to parse the string in their entirety ✔
Index into the MathematicalOperations method using the operator parsed during pattern matching to find the corresponding implementation and invoke it. ✔
Implement the Program class to instantiate the calculator and invoke it based on user input from the console. ✔
Be sure to use the WriteLine/ReadLine properties on Program for testing the input and output of your program. ✔

Place all shared project properties into a Directory.Build.props file. ✔
Place all shared project items into a Directory.Build.targets file. (optional)
nullable reference types is enabled ✔
Ensure that you turn on code analysis for all projects(EnableNETAnalyzers) ✔
Set LangVersion and the TargetFramework to the latest released versions available (preview versions optional) ✔
and enabled .NET analyzers for both projects ✔
For this assignment, always use Assert.AreEqual() (the generic version) ✔
All of the above should be unit tested ✔
Choose simplicity over complexity ✔

Great work!

Al3xramirez

This comment was marked as duplicate.

program.WriteLine("Invalid calculation. Use format: number operator number (e.g., '3 + 4')");
}
}

Copy link

@ulises-aguilar ulises-aguilar Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: You can remove these extra blank lines to keep the formatting consistent.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I respect that, is that more of a stylistic choice or?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since its not gonna break the program; I'm gonna put this on the back burner

Copy link

@ulises-aguilar ulises-aguilar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a Console project called "Calculate". ✔
Define a Program Class
Define two init-only setter properties, WriteLine and ReadLine, that contain delegates for writing a line of text and reading a line of text respectively ✔
Write a test that sets these properties at construction time and then invokes the properties and verifies the expected behavior occurs. ✔
Set the default behavior for the WriteLine and ReadLine properties to invoke System.Console versions of the methods and add an empty default constructor. ✔
Define a Calculator class ✔
Define static Add, Subtract, Multiply, and Divide methods that have two parameters and return a third parameter. ✔
Define a read-only property, MathematicalOperations, of type System.Collections.Generics.IReadOnlyDictionary<TKey,TValue> that:
is initialized to a System.Collections.Generics.Dictionary<<TKey,TValue> instance that. ✔
Uses char for the key corresponding to the operators +, -, *, and /. ✔
Has values that correspond with the Add, Subtract, Multiply, and Divide methods. ✔
Implement a TryCalculate method following "TryParse" pattern ✔
Valid calculation expressions include such strings as "3 + 4", "42 - 2", etc. ✔
If there is no whitespace around the operator, you can assume the calculation is invalid and return false. Similarly if the operands are not integers. ✔
Use string.Split(), pattern matching, logical and operators to parse the string in their entirety ✔
Index into the MathematicalOperations method using the operator parsed during pattern matching to find the corresponding implementation and invoke it. ✔
Implement the Program class to instantiate the calculator and invoke it based on user input from the console. ✔
Be sure to use the WriteLine/ReadLine properties on Program for testing the input and output of your program. ✔

@Hellothereyoko
Copy link
Author

Reviewed EACH AND EVERY PIECE OF FEEDBACK HERE. I addressed and responded to each one either seekng clarification or asking questions...

Copy link

@thigiang16 thigiang16 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instructions
Create a Console project called "Calculate". ✔
Define a Program Class
Define two init-only setter properties, WriteLine and ReadLine, that contain delegates for writing a line of text and reading a line of text respectively ✔
Write a test that sets these properties at construction time and then invokes the properties and verifies the expected behavior occurs. ✔
Set the default behavior for the WriteLine and ReadLine properties to invoke System.Console versions of the methods and add an empty default constructor. ✔
Define a Calculator class ✔
Define static Add, Subtract, Multiply, and Divide methods that have two parameters and return a third parameter. ✔
Define a read-only property, MathematicalOperations, of type System.Collections.Generics.IReadOnlyDictionary<TKey,TValue> that:
is initialized to a System.Collections.Generics.Dictionary<<TKey,TValue> instance that. ✔
Uses char for the key corresponding to the operators +, -, *, and /. ✔
Has values that correspond with the Add, Subtract, Multiply, and Divide methods. ✔
Implement a TryCalculate method following "TryParse" pattern ✔
Valid calculation expressions include such strings as "3 + 4", "42 - 2", etc. ✔
If there is no whitespace around the operator, you can assume the calculation is invalid and return false. Similarly if the operands are not integers. ✔
Use string.Split(), pattern matching, logical and operators to parse the string in their entirety ✔
Index into the MathematicalOperations method using the operator parsed during pattern matching to find the corresponding implementation and invoke it. ✔
Implement the Program class to instantiate the calculator and invoke it based on user input from the console. ✔
Be sure to use the WriteLine/ReadLine properties on Program for testing the input and output of your program. ✔
Extra Credit
Do one of the following two options (or both if you want extra, extra credit) :)

Refactor the redirect portion of the Program class into 'ProgramBase` ❌
Move ProgramBase into a ConsoleUtilities assembly to be used in other console-based projects ❌
Use generics the mathematical operations methods and consider using generic constraints (requires .NET 7.0) ❌
Fundamentals
Place all shared project properties into a Directory.Build.props file.
Place all shared project items into a Directory.Build.targets file. (optional)
nullable reference types is enabled ✔
Ensure that you turn on code analysis for all projects(EnableNETAnalyzers) ✔
Set LangVersion and the TargetFramework to the latest released versions available (preview versions optional) ✔
and enabled .NET analyzers for both projects ✔
For this assignment, always use Assert.AreEqual() (the generic version) ✔
All of the above should be unit tested ✔
Choose simplicity over complexity ✔

Overall, everything looks good! The program is clear, simple, and well-tested!

{

result = 0;
var parts = calculation.Split(' ');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you implemented looks good! You can make it a bit safer by using var parts = calculation.Split(' ', StringSplitOptions.RemoveEmptyEntries); instead of splitting on a single space. This way, extra spaces in the input won’t break the calculation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice will implement.

}

[TestMethod]
public void TryCalculate_ExtraSpaces_ReturnsFalse()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggestion I mentioned earlier would make this test pass with extra spaces but I don’t think handling extra spaces is part of the requirements, so you can just take it as an optional suggestion

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the instructions say that the operand has to have spaces around it and that's it, but since it's a quick implementation and test change I implemented it. I don't see how it could LOSE points making the program more robust and not work with little user errors like adding an extra space.

{
public static int Add(int a, int b) => a + b;
public static int Subtract(int a, int b) => a - b;
public static int Multiple(int a, int b) => a * b;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename Multiple to Multiply just in case. It might lose points since it doesn't exactly follow the instructions...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented thanks!

Added option to the split function in trycalculate to remove empty entries
@github-actions
Copy link

github-actions bot commented Nov 6, 2025

Summary

Summary
Generated on: 11/06/2025 - 15:11:05
Coverage date: 11/06/2025 - 15:11:03
Parser: MultiReport (2x Cobertura)
Assemblies: 1
Classes: 2
Files: 2
Line coverage: 96% (49 of 51)
Covered lines: 49
Uncovered lines: 2
Coverable lines: 51
Total lines: 82
Branch coverage: 95% (19 of 20)
Covered branches: 19
Total branches: 20
Method coverage: Feature is only available for sponsors
Tag: 388_19140247637

Coverage

Calculate. - 96%
Name Line Branch
Calculate. 96% 95%
Calculate.Calculator 92.8% 92.8%
Calculate.Program 100% 100%

@RyanHirte
Copy link

Don't know why GitHub isn't letting me resolve conversations but all comments have been addressed.

Copy link
Collaborator

@Joshua-Lester3 Joshua-Lester3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instructions

  • Create a Console project called "Calculate". ✔
  • Define a Program Class
    • Define two init-only setter properties, WriteLine and ReadLine, that contain delegates for writing a line of text and reading a line of text respectively ✔
    • Write a test that sets these properties at construction time and then invokes the properties and verifies the expected behavior occurs. ✔
    • Set the default behavior for the WriteLine and ReadLine properties to invoke System.Console versions of the methods and add an empty default constructor. ✔
  • Define a Calculator class ✔
    • Define static Add, Subtract, Multiply, and Divide methods that have two parameters and return a third parameter. ❌ It's a little nit-picky, but I'm assuming third parameter means having a third parameter labeled out. That's my interpretation of 'returning a parameter'.
    • Define a read-only property, MathematicalOperations, of type System.Collections.Generics.IReadOnlyDictionary<TKey,TValue> that:
      • is initialized to a System.Collections.Generics.Dictionary<<TKey,TValue> instance that. ✔
        • Uses char for the key corresponding to the operators +, -, *, and /. ✔
        • Has values that correspond with the Add, Subtract, Multiply, and Divide methods. ✔
    • Implement a TryCalculate method following "TryParse" pattern ✔
      • Valid calculation expressions include such strings as "3 + 4", "42 - 2", etc. ✔
      • If there is no whitespace around the operator, you can assume the calculation is invalid and return false. Similarly if the operands are not integers. ✔
      • Use string.Split(), pattern matching, logical and operators to parse the string in their entirety ✔
      • Index into the MathematicalOperations method using the operator parsed during pattern matching to find the corresponding implementation and invoke it. ✔
  • Implement the Program class to instantiate the calculator and invoke it based on user input from the console. ✔
  • Be sure to use the WriteLine/ReadLine properties on Program for testing the input and output of your program. ✔

Extra Credit

Do one of the following two options (or both if you want extra, extra credit) :)

  • Refactor the redirect portion of the Program class into 'ProgramBase` ❌
    • Move ProgramBase into a ConsoleUtilities assembly to be used in other console-based projects
  • Use generics the mathematical operations methods and consider using generic constraints (requires .NET 7.0) ❌

Fundamentals

  • Place all shared project properties into a Directory.Build.props file.
  • Place all shared project items into a Directory.Build.targets file. (optional)
  • nullable reference types is enabled ✔
  • Ensure that you turn on code analysis for all projects(EnableNETAnalyzers) ✔
  • Set LangVersion and the TargetFramework to the latest released versions available (preview versions optional) ✔
  • and enabled .NET analyzers for both projects ✔
  • For this assignment, always use Assert.AreEqual<T>() (the generic version) ✔
  • All of the above should be unit tested ✔
  • Choose simplicity over complexity ✔

parts[1].Length != 1 ||
!int.TryParse(parts[0], out int left) ||
!int.TryParse(parts[2], out int right) ||
!MathematicalOperations.TryGetValue(parts[1][0], out var operation))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd specify the type instead of var here. So Func<int, int, int>. There's a guideline or convention that when the type isn't immediately obvious to specify it - I'd say that applies here.


program.WriteLine("Enter calculation (e.g., '3 + 4') or 'exit' to quit:");

while (true)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider that having an infinite loop creates a certain kind of interactive experience like a REPL vs if you did not have the loop the app would exit on each invocation and could be used like in an automated tool call fashion.

private static readonly string[] ExitCommandVariations = new[] { "EXIT", "exit", "Exit" };

// Lock object to prevent parallel test execution from interfering with Console redirection
private static readonly object ConsoleLock = new();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job putting a comment on why this lock exists, however, I can't put my finger on it but something does not smell right here. It seems like you might not need a lock, you want to try to isolate the tests as much as possible.


// Assert
Assert.HasCount(3, outputs);
Assert.AreEqual<string>("First", outputs[0]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you typically only want a single assertion so that the test fails for a specific reason.

parts[1].Length != 1 ||
!int.TryParse(parts[0], out int left) ||
!int.TryParse(parts[2], out int right) ||
!MathematicalOperations.TryGetValue(parts[1][0], out var operation))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the instructions it mentions to "Index into the MathematicalOperations method using the operator parsed during pattern matching to find the corresponding implementation and invoke it."

}

// prevent division by zero
if (parts[1][0] == '/' && right == 0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the MathematicalOperations division key instead of hard coding this here.

Copy link

@quattro004 quattro004 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants