Calculator.
cs
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace CalcLoginApp
{
public class Calculator
{
public int Add(int a, int b) => a + b;
public int Subtract(int a, int b) => a - b;
public int Multiply(int a, int b) => a * b;
public double Divide(int a, int b)
{
if (b == 0) throw new DivideByZeroException("Cannot divide by zero.");
return (double)a / b; // Now returns double for precise division
}
public int Modulus(int a, int b)
{
if (b == 0) throw new DivideByZeroException("Cannot modulus by zero.");
return a % b;
}
}
}
[Link]
using System;
using [Link];
namespace CalcLoginApp
{
public class LoginValidator
{
public bool IsValidEmail(string email)
{
if ([Link](email))
return false;
try
{
var addr = new MailAddress(email);
return [Link] == email;
}
catch
{
return false;
}
}
public bool IsValidPassword(string password)
{
if ([Link](password) || [Link] < 6)
return false;
bool hasLetter = false;
bool hasDigit = false;
foreach (char c in password)
{
if ([Link](c))
hasLetter = true;
else if ([Link](c))
hasDigit = true;
if (hasLetter && hasDigit)
return true;
}
return false;
}
// ✅ Add this method to support login validation
public bool IsLoginValid(string email, string password)
{
return IsValidEmail(email) && IsValidPassword(password);
}
}
[Link]
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace CalcLoginApp
{
class Program
{
static void Main(string[] args)
{
// Calculator demo
Calculator calc = new Calculator();
[Link]("Calculator Demo:");
[Link]($"5 + 3 = {[Link](5, 3)}");
[Link]($"10 / 3 = {[Link](10, 3):0.00}");
// Login validation demo
LoginValidator validator = new LoginValidator();
[Link]("\nLogin Validation Demo:");
[Link]("Enter email: ");
string email = [Link]();
[Link]("Enter password: ");
string password = [Link]();
bool isValid = [Link](email, password);
[Link]($"Login valid: {isValid}");
[Link]("\nPress any key to exit...");
[Link]();
}
}
}
[Link]
using [Link];
using CalcLoginApp;
namespace [Link]
{
[TestFixture]
public class CalculatorTests
{
private Calculator _calculator;
[SetUp]
public void Setup()
{
_calculator = new Calculator();
}
[Test]
[TestCase(4, 6, 10)]
[TestCase(-2, -3, -5)]
[TestCase(0, 5, 5)]
public void Add_VariousNumbers_ReturnsCorrectSum(int a, int b, int expected)
{
[Link](_calculator.Add(a, b), [Link](expected));
}
[Test]
public void Divide_ByNonZero_ReturnsPreciseResult()
{
double result = _calculator.Divide(10, 3);
[Link](result, [Link](3.33).Within(0.01));
}
[Test]
public void Divide_ByZero_ThrowsException()
{
[Link](() => _calculator.Divide(10, 0), [Link]<DivideByZeroException>());
}
[Test]
public void Modulus_ValidInput_ReturnsCorrectRemainder()
{
[Link](_calculator.Modulus(10, 3), [Link](1));
}
}
}
[Link]
using [Link];
using [Link];
using CalcLoginApp;
namespace [Link]
{
[TestFixture]
public class LoginValidatorTests
{
private LoginValidator _validator;
[SetUp]
public void Setup()
{
_validator = new LoginValidator();
}
[Test]
[TestCase("test@[Link]", true)]
[TestCase("[Link]+tag@[Link]", true)]
[TestCase("[Link]@[Link]", true)]
[TestCase("email@[Link]", true)]
[TestCase("firstname-lastname@[Link]", true)]
[TestCase("[Link]@", false)]
[TestCase("[Link]", false)]
[TestCase("plainstring", false)]
[TestCase("@[Link]", false)]
[TestCase("", false)]
public void IsValidEmail_VariousInputs_ReturnsCorrectResult(string email, bool expected)
{
[Link](_validator.IsValidEmail(email), [Link](expected),
$"Email validation failed for: {email}");
}
[Test]
public void IsValidEmail_NullInput_ReturnsFalse()
{
[Link](_validator.IsValidEmail(null), [Link]);
}
[Test]
[TestCase("abc123", true)]
[TestCase("P@ssw0rd", true)]
[TestCase("LongPassword1", true)]
[TestCase("12345", false)]
[TestCase("password", false)]
[TestCase("123456", false)]
[TestCase("", false)]
public void IsValidPassword_VariousInputs_ReturnsCorrectResult(string password, bool
expected)
{
[Link](_validator.IsValidPassword(password), [Link](expected),
$"Password validation failed for: {password}");
}
[Test]
public void IsValidPassword_NullInput_ReturnsFalse()
{
[Link](_validator.IsValidPassword(null), [Link]);
}
}
}
<Project Sdk="[Link]">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<!-- This is the key change -->
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="[Link]" Version="6.0.2" />
<PackageReference Include="[Link]" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="[Link]" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CalcLoginApp\[Link]" />
</ItemGroup>
</Project>