Skip to content

Instantly share code, notes, and snippets.

@kirasiris
Last active February 19, 2020 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirasiris/30d9ca0f33bd5bb8f39382878a60f7b1 to your computer and use it in GitHub Desktop.
Save kirasiris/30d9ca0f33bd5bb8f39382878a60f7b1 to your computer and use it in GitHub Desktop.
TO COMPLETE!! Needs string validation!
using System;
namespace Unit2_Calculator_AzuaraFonseca {
class MathMethods {
public void showMenu() {
Console.WriteLine("|----------------------------------------------|");
Console.WriteLine("| A: Addition |");
Console.WriteLine("| B: Substraction |");
Console.WriteLine("| C: Multiplication |");
Console.WriteLine("| D: Division |");
Console.WriteLine("| E: exit program |");
Console.WriteLine("|----------------------------------------------|");
}
public void handleOption(string opt) {
if (opt == "A" || opt == "a") {
Console.WriteLine("Please enter the first number");
string fNum = Console.ReadLine();
Console.WriteLine("Please enter the second number");
string sNum = Console.ReadLine();
int k = 0;
int l = 0;
if (!Int32.TryParse(fNum, out k) || !Int32.TryParse(sNum, out l)) {
throw new ArgumentException("Arguments are not numeric!");
}; // the values are returned as strings.
// the values are converted to ints
int result = Convert.ToInt32(fNum) + Convert.ToInt32(sNum);
Console.WriteLine("The sum of " + fNum + " + " + sNum + " = " + result + " ");
}
else if (opt == "B" || opt == "b") {
Console.WriteLine("Please enter the first number");
string fNum = Console.ReadLine();
Console.WriteLine("Please enter the second number");
string sNum = Console.ReadLine();
int k = 0;
int l = 0;
if (!Int32.TryParse(fNum, out k) || !Int32.TryParse(sNum, out l)) {
throw new ArgumentException("Arguments are not numeric!");
}; // the values are returned as strings.
int result = Convert.ToInt32(fNum) - Convert.ToInt32(sNum);
Console.WriteLine("The substraction of " + fNum + " - " + sNum + " = " + result + " ");
}
else if (opt == "C" || opt == "c") {
Console.WriteLine("Please enter the first number");
string fNum = Console.ReadLine();
Console.WriteLine("Please enter the second number");
string sNum = Console.ReadLine();
int k = 0;
int l = 0;
if (!Int32.TryParse(fNum, out k) || !Int32.TryParse(sNum, out l)) {
throw new ArgumentException("Arguments are not numeric!");
}; // the values are returned as strings.
int result = Convert.ToInt32(fNum) * Convert.ToInt32(sNum);
Console.WriteLine("The multiplication of " + fNum + " * " + sNum + " = " + result + " ");
}
else if (opt == "D" || opt == "d") {
Console.WriteLine("Please enter the first number");
string fNum = Console.ReadLine();
Console.WriteLine("Please enter the second number");
string sNum = Console.ReadLine();
int k = 0;
int l = 0;
if (!Int32.TryParse(fNum, out k) || !Int32.TryParse(sNum, out l)) {
throw new ArgumentException("Arguments are not numeric!");
}; // the values are returned as strings.
int result = Convert.ToInt32(fNum) / Convert.ToInt32(sNum);
Console.WriteLine("The division of " + fNum + " / " + sNum + " = " + result + " ");
}
else {
Console.WriteLine("Invalid Input. Please try again");
}
}
}
}
using static System.Console;
namespace Unit2_Calculator_AzuaraFonseca {
class Program {
static void Main(string[] args) {
// Instantiate Class
MathMethods m = new MathMethods();
string option= "";
do {
m.showMenu();
WriteLine("Please enter an option");
option = ReadLine();
m.handleOption(option);
} while (option != "e" && option != "E");
}
}
}
@kirasiris
Copy link
Author

Throws Exception if invalid input is submitted

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