Skip to content

Conversation

@c-stanton
Copy link

Worked with @Al3xramirez

Copy link

@ColtonKnopik ColtonKnopik 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. ✔

Copy link

@RyanHirte RyanHirte 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. ✔

Extra credit not implemented

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 great job! Just change the behavior of Program to default to the console implementations of writeline and readline.

Copy link

@KorbinWeiler KorbinWeiler 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. ✔
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 ✔

Al3xramirez and others added 3 commits November 5, 2025 12:32
Added <string> generic to tests that were missing that, took out duplicated proj lines that were already in the directory props file, changed divide method to simply divide and not check if it is 0, changed write and readline to set default behaviors
Fixed peer recommendations
used DataTestMethod for MathmaticalOperations unit test
Copy link

@PigPig-SDK PigPig-SDK 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 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. ✔
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 ✔

public static bool TryCalculate(string expression, out double result)
{
result = 0;
string[] parts = expression.Split(' ', StringSplitOptions.RemoveEmptyEntries);

Choose a reason for hiding this comment

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

Be sure to null check 'expression' before calling Split().

Choose a reason for hiding this comment

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

Thanks for that catch! Will add.


public Func<string?> ReadLine { get; init; } = Console.ReadLine;

public Program()

Choose a reason for hiding this comment

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

Could remove this constructor as it doesn't do anything. (Will be generated anyway)

Choose a reason for hiding this comment

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

Thank you for the recommendation, but Ill leave that there just for assignment specs.

Al3xramirez and others added 2 commits November 6, 2025 18:09
added nullorWhitespace recommended by peers
Added final Peer recommendations/corrections
@github-actions
Copy link

github-actions bot commented Nov 7, 2025

Summary

Summary
Generated on: 11/07/2025 - 02:14:07
Coverage date: 11/07/2025 - 02:14:04
Parser: MultiReport (2x Cobertura)
Assemblies: 1
Classes: 2
Files: 2
Line coverage: 86.3% (57 of 66)
Covered lines: 57
Uncovered lines: 9
Coverable lines: 66
Total lines: 117
Branch coverage: 80% (16 of 20)
Covered branches: 16
Total branches: 20
Method coverage: Feature is only available for sponsors
Tag: 394_19155998622

Coverage

Calculate - 86.3%
Name Line Branch
Calculate 86.3% 80%
Calculate.Calculator 86.4% 75%
Calculate.Program 86.2% 100%

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. ❌ nit: doesn't return a third parameter (my interpretation of this is using out param)
    • 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 ❌ Not fully unit tested.
  • Choose simplicity over complexity ✔

Comment on lines +200 to +225
String[] inputs =
{
"10 + 5",
"20 - 4",
"3 * 7",
"16 / 2",
""
};

int Index = 0;
var outputs = new List<string>();

var program = new Program
{
ReadLine = () => inputs[Index++],
WriteLine = s => outputs.Add(s)
};

//Act
program.Run();

//Assert
Assert.AreEqual<string>("Result: 15", outputs[2]);
Assert.AreEqual<string>("Result: 16", outputs[4]);
Assert.AreEqual<string>("Result: 21", outputs[6]);
Assert.AreEqual<string>("Result: 8", outputs[8]);
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 remove logic from your tests. Make them only have one reason to fail.

{
WriteLine("Calculator Program! Press Enter to exit.");

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.

Choose a reason for hiding this comment

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

In the instructions under the "Define a Program class" regarding the "two init-only setter properties" it mentions to "Write a test that sets these properties at construction time and then invokes the properties and verifies the expected behavior occurs."


if (MathematicalOperations.TryGetValue(op, out var operation))
{
if (op == '/' && number2 == 0)
Copy link

@quattro004 quattro004 Nov 9, 2025

Choose a reason for hiding this comment

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

I wonder if you could use the MathematicalOperations divide char instead of hard coding the op here?

return false;
}

if (MathematicalOperations.TryGetValue(op, 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 under "Define a Calculator class" it mentions to "Index into the MathematicalOperations method using the operator parsed during pattern matching to find the corresponding implementation and invoke it."

}

[TestMethod]
public void Run_Completes_Successfully()

Choose a reason for hiding this comment

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

I wonder if this could be written as a data test since there's so many assertions. Typically you want one assertion per test.

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.

8 participants