Module Module1
#Region " Enumerations "
''' <summary>
''' Indicates an arithmetic operation.
''' </summary>
Public Enum ArithmeticOperation As Integer
''' <summary>
''' Any arithmetic operation.
''' </summary>
None = 0
''' <summary>
''' Sums the values.
''' </summary>
Sum = 1I
''' <summary>
''' Subtracts the values.
''' </summary>
Subtract = 2I
''' <summary>
''' Multiplies the values.
''' </summary>
Multiply = 3I
''' <summary>
''' Divides the values.
''' </summary>
Divide = 4I
End Enum
#End Region
#Region " Entry Point (Main)"
''' <summary>
''' Defines the entry point of the application.
''' </summary>
Public Sub Main()
DoArithmetics()
End Sub
#End Region
#Region " Public Methods "
Public Sub DoArithmetics()
Dim ExitString As String = String.Empty
Dim ExitDemand As Boolean = False
Dim Operation As ArithmeticOperation = ArithmeticOperation.None
Dim Value1 As Decimal = 0D
Dim Value2 As Decimal = 0D
Dim Result As Decimal = 0D
Do Until ExitDemand
SetValue(Value1, "Ingresar el primer numero: ")
SetValue(Value2, "Ingresar el segundo numero: ")
Console.WriteLine()
SetOperation(Operation, "Elegir una operacion aritmética: ")
SetResult(Operation, Value1, Value2, Result)
Console.WriteLine()
Console.WriteLine(String.Format("El resultado es: {0}", CStr(Result)))
Console.WriteLine()
SetExit(ExitDemand, "S"c, "N"c, "¿Quiere salir? [S/N]: ")
Console.Clear()
Loop ' ExitDemand
End Sub
''' <summary>
''' Asks for a value and waits for the user-input to set the value.
''' </summary>
''' <param name="Value">The ByRefered variable to set the input result.</param>
''' <param name="message">The output message.</param>
Public Sub SetValue(ByRef Value As Decimal,
Optional ByVal message As String = "Enter a value: ")
Dim Success As Boolean = False
Do Until Success
Console.Write(message)
Try
Value = Decimal.Parse(Console.ReadLine().Replace("."c, ","c))
Success = True
Catch ex As Exception
Console.WriteLine(ex.Message)
Console.WriteLine()
End Try
Loop ' Success
End Sub
''' <summary>
''' Asks for an arithmetic operation and waits for the user-input to set the value.
''' </summary>
''' <param name="Operation">The ByRefered variable to set the input result.</param>
''' <param name="message">The output message.</param>
Public Sub SetOperation(ByRef Operation As ArithmeticOperation,
Optional ByVal message As String = "Choose an operation: ")
Dim Success As Boolean = False
For Each Value As ArithmeticOperation In [Enum].GetValues(GetType(ArithmeticOperation))
Console.Write(String.Format("{0}={1} | ", CStr(Value), Value.ToString))
Next Value
Console.WriteLine()
Do Until Success
Console.Write(message)
Try
Operation = [Enum].Parse(GetType(ArithmeticOperation), Console.ReadLine, True)
Success = True
Catch ex As Exception
Console.WriteLine(ex.Message)
Console.WriteLine()
End Try
Loop ' Success
End Sub
''' <summary>
''' Performs an arithmetic operation on the given values.
''' </summary>
''' <param name="Operation">The arithmetic operation to perform.</param>
''' <param name="Value1">The first value.</param>
''' <param name="Value2">The second value.</param>
''' <param name="Result">The ByRefered variable to set the operation result value.</param>
Public Sub SetResult(ByVal Operation As ArithmeticOperation,
ByVal Value1 As Decimal,
ByVal Value2 As Decimal,
ByRef Result As Decimal)
Select Case Operation
Case ArithmeticOperation.Sum
Result = Sum(Value1, Value2)
Case ArithmeticOperation.Subtract
Result = Subtract(Value1, Value2)
Case ArithmeticOperation.Multiply
Result = Multiply(Value1, Value2)
Case ArithmeticOperation.Divide
Result = Divide(Value1, Value2)
Case ArithmeticOperation.None
' Do Nothing
End Select
End Sub
''' <summary>
''' Asks for a Boolean value and waits for the user-input to set the value.
''' </summary>
''' <param name="ExitBool">The ByRefered variable to set the input result.</param>
''' <param name="TrueChar">Indicates the character threated as 'True'.</param>
''' <param name="FalseChar">Indicates the character threated as 'False'.</param>
''' <param name="message">The output message.</param>
Public Sub SetExit(ByRef ExitBool As Boolean,
Optional ByVal TrueChar As Char = "Y"c,
Optional ByVal FalseChar As Char = "N"c,
Optional ByVal message As String = "¿Want to exit? [Y/N]: ")
Dim Success As Boolean = False
Dim Result As Char = Nothing
Console.Write(message)
Do Until Success
Result = Console.ReadKey().KeyChar
Select Case Char.ToLower(Result)
Case Char.ToLower(TrueChar)
ExitBool = True
Success = True
Case Char.ToLower(FalseChar)
ExitBool = False
Success = True
Case Else
Console.Beep()
If Not Char.IsLetterOrDigit(Result) Then
Console.CursorLeft = 0
SetExit(ExitBool, TrueChar, FalseChar, message)
Else
Console.CursorLeft = Console.CursorLeft - 1
Console.Write(" ")
Console.CursorLeft = Console.CursorLeft - 1
End If
End Select ' Char.ToLower(Result)
Loop ' Success
End Sub
#End Region
#Region " Private Functions "
''' <summary>
''' Performs a Sum operation on the specified values.
''' </summary>
Private Function Sum(ByVal Value1 As Decimal,
ByVal Value2 As Decimal) As Decimal
Return (Value1 + Value2)
End Function
''' <summary>
''' Performs a subtraction operation on the specified values.
''' </summary>
Private Function Subtract(ByVal Value1 As Decimal,
ByVal Value2 As Decimal) As Decimal
Return (Value1 - Value2)
End Function
''' <summary>
''' Performs a multiplier operation on the specified values.
''' </summary>
Private Function Multiply(ByVal Value1 As Decimal,
ByVal Value2 As Decimal) As Decimal
Return (Value1 * Value2)
End Function
''' <summary>
''' Performs a divider operation on the specified values.
''' </summary>
Private Function Divide(ByVal Value1 As Decimal,
ByVal Value2 As Decimal) As Decimal
Return (Value1 / Value2)
End Function
#End Region
End Module