LostNativ
Member
- Joined
- May 7, 2021
- Messages
- 5
- Programming Experience
- Beginner
I want to be able to catch errors and ask the user for valid inputs.
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace S1P1
{
class Program
{
public delegate float WattsLawDelegate(float num1, float num2);
static void Main(string[] args)
{
// Name : void Main(string[] args)
// Purpose : Main entry point to the program
// Re-use : DisplayMenu(); GetMenuOption(); GetValue(); CalcVoltage(); CalcResistance(); CalCurrent()
// Method Parameters : string[] args
// - console/command line arguments
// Output Type : None
string endCal = "";
float userInput1 = 0;
float userInput2 = 0;
float result = 0;
string messageCal = "";
bool input = false;
WattsLawDelegate wattsLaw = null;
DisplayMenu();
endCal =GetMenuOption();
while (endCal != "X")
{
//switch
switch (endCal)
{
case "P":
userInput1 = GetValue("current");
userInput2 = GetValue("Voltage");
wattsLaw = new WattsLawDelegate(CalcPower);
messageCal = "The Power is: ";
input = true;
break;
case "V":
userInput1 = GetValue("power");
userInput2 = GetValue("current");
wattsLaw = new WattsLawDelegate(CalcVoltage);
messageCal = "The Volatage is: ";
input = true;
break;
case "I":
userInput1 = GetValue("voltage");
userInput2 = GetValue("resistance");
wattsLaw = new WattsLawDelegate(CalCurrent);
break;
case "X":
;
break;
default:
Console.WriteLine("Invalid option, please try again");
break;
} // end switch
if (input)
{
result = wattsLaw(userInput1, userInput2);
Console.WriteLine(messageCal + " " + result);
input = false;
} // end if
DisplayMenu();
endCal = GetMenuOption();
}
}//main method
public static void DisplayMenu()
{
//
//Name : void DisplayMenu()
//Purpose : Display the menu to the user on screen
//Re-use : None
//Method Parameters : None
//Output Type : None
//
Console.WriteLine("Watt's Law Calculator");
Console.WriteLine("======================");
Console.WriteLine(" P - Calculate the power\n I - Calculate the current\n V - Calculate the voltage\n X -Exit");
}//end DisplayMenu
public static string GetMenuOption()
{
//
//Name : string GetMenuOption()
//Purpose : Prompt the user for an option
//Re-use : None
//Method Parameters : None
//Output Type : string
// - the option entered by the user
Console.Write("Please enter V, P, I or X: ");
return Console.ReadLine().ToUpper();
}//end GetMenuOption
public static float GetValue(string type)
{
//
//Name : int GetValue(string type)
//Purpose : Prompt the user for a value and parse it to float
//Re-use : None
//Method Parameters : string type
// - the type of value to get: voltage, power or current
//Output Type : float
// - the converted float entered by the user
//
try
{
Console.WriteLine("Please enter a " + type + " value: ");
}
catch(Exception ex)
{
Console.WriteLine("GetValue:" + ex.Message);
}
return float.Parse(Console.ReadLine());
}
public static float CalcPower(float current, float voltage)
{
//
//Name : int CalcResistance(float current, float voltage)
//Purpose : Calculate the Resistance using Watts Law
//Re-use : None
//Method Parameters : - float current
// - voltage value in volts
// - float voltage
// - current value in amps
//Output Type : float
// - the calculated power value
float power = 0;
try
{
power = current * voltage;
}//end try
catch(Exception ex)
{
Console.WriteLine("CalcPower:" + ex.Message);
}
return power;
}//end CalcPower
public static float CalcVoltage(float power, float current)
{
//
//Name : float CalcVoltage(float power, float power)
//Purpose : Calculate the Voltage using Watts Law
//Re-use : None
//Method Parameters : - float power
//
// - float power
//
//Output Type : float
// - the calculated Voltage value
float voltage = 0;
try
{
voltage = power / current;
}
catch (Exception ex)
{
Console.WriteLine("CalcVoltage:" + ex.Message);
}
return voltage;
}//end CalcVoltage
public static float CalCurrent(float power, float voltage)
{
//
//Name : int CalcCurrent(float power, float voltage)
//Purpose : Calculate the Current using Watts Law
//Re-use : None
//Method Parameters : - float power
//
// - float voltage
//
//Output Type : float
// - the calculated Current value
float current = 0;
try
{
current = power / voltage;
}
catch (Exception ex)
{
Console.WriteLine("CalCurrent:" + ex.Message);
}
return current;
}//end CalCurrent
}//end class
}//end namespace