Last active
February 19, 2020 02:45
-
-
Save kirasiris/30d9ca0f33bd5bb8f39382878a60f7b1 to your computer and use it in GitHub Desktop.
TO COMPLETE!! Needs string validation!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Throws Exception if invalid input is submitted