using System;
// Program.cs
namespace Calculator
{
public class Program
{
static void Main(string[] args)
{
Application app = new Application();
app.Run();
}
}
}
// Application.cs
namespace Calculator
{
public class Application
{
private const string AppTitle = "Mini Calculator";
public void Run()
{
Console.Title = AppTitle;
Console.WriteLine("###############");
Console.WriteLine(AppTitle);
Console.WriteLine("###############\n");
string s;
do
{
var userInputs = Input();
var result = Calculate(userInputs);
OutputResult(result);
Console.WriteLine("Do you like to solve another arithmetical problem? (y/n)");
s = Console.ReadLine().Trim().ToLower();
} while (s.StartsWith("y"));
Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
public class UserInputs
{
public double Number1 { get; set; }
public double Number2 { get; set; }
public int Operator { get; set; }
}
public static UserInputs Input()
{
Console.WriteLine("Please enter your first number: ");
double number1 = double.Parse(Console.ReadLine());
Console.WriteLine($"You chose: {number1}");
Console.WriteLine("Please enter your second number: ");
double number2 = double.Parse(Console.ReadLine());
Console.WriteLine($"You chose: {number2}");
Console.WriteLine("Choose your operator by number and hit enter:");
Console.WriteLine("> 1.) Addition");
Console.WriteLine("> 2.) Subtraction");
Console.WriteLine("> 3.) Multiplication");
Console.WriteLine("> 4.) Division");
int op = 0;
while (true)
{
op = int.Parse(Console.ReadLine());
Console.WriteLine($"You chose: {op}");
if (op < 1 || op > 4)
{
Console.WriteLine("Your input is invalid. Try again, with numerals 1 - 4.");
continue;
}
break;
}
return new UserInputs() { Number1 = number1, Number2 = number2, Operator = op };
}
public double Calculate(UserInputs userInputs)
{
double result = 0;
switch (userInputs.Operator)
{
case 1:
result = Calculation.Addition(userInputs.Number1, userInputs.Number2);
break;
case 2:
result = Calculation.Subtraction(userInputs.Number1, userInputs.Number2);
break;
case 3:
result = Calculation.Multiplication(userInputs.Number1, userInputs.Number2);
break;
case 4:
result = Calculation.Division(userInputs.Number1, userInputs.Number2);
break;
}
return result;
}
public void OutputResult(double result)
{
Console.WriteLine($"Result: {result}");
}
}
}
// Calculation.cs
namespace Calculator
{
public static class Calculation
{
public static double Addition(double x, double y)
{
return x + y;
}
public static double Subtraction(double x, double y)
{
return x - y;
}
public static double Multiplication(double x, double y)
{
return x * y;
}
public static double Division(double x, double y)
{
return x / y;
}
}
}