Solved

# Currency calculator

Posted on 2011-05-05
372 Views
I have created a currency calculator. I need to be able to allow the user to input one currency and give him a choice to either specify a currency to convert to or display results for all currencies. I need to use an array.

I was thinking of using radio buttons to specify the currency to convert to but how would implement an array.

I would appreciate the help. This is what I have.
``````Public Class Calculator

Dim inputnumber As Double
Dim Eurtotal As Double
Dim UStotal As Double
Dim Australiantotal As Double
Dim FranceFrantotal As Double
Dim Mexicototal As Double
Dim Japantotal As Double
Dim disablebox As Boolean

'Disables unchecked checkboxes after currency is selected
Sub DisableCheckboxes()
If chkEur.Checked Or chkUS.Checked Or chkCanadian.Checked Or chkAustralian.Checked Or chkFranceFrancs.Checked Or chkMexicoPesos.Checked Or chkJapanYen.Checked Then
chkEur.Enabled = False
chkUS.Enabled = False
chkAustralian.Enabled = False
chkFranceFrancs.Enabled = False
chkJapanYen.Enabled = False
chkMexicoPesos.Enabled = False
End If
End Sub
'Conversion formula
'Input amount divided conversion rate equals the sum
Sub ConversionRate()
If chkEur.Checked = True Then
Eurtotal = CDbl(txtInputAmount.Text) / 1
UStotal = Eurtotal * 2
Australiantotal = Eurtotal * 4
FranceFrantotal = Eurtotal * 5
Mexicototal = Eurtotal * 6
Japantotal = Eurtotal * 7
ElseIf chkUS.Checked = True Then
Eurtotal = CDbl(txtInputAmount.Text) / 2
UStotal = Eurtotal * 2
Australiantotal = Eurtotal * 4
FranceFrantotal = Eurtotal * 5
Mexicototal = Eurtotal * 6
Japantotal = Eurtotal * 7
Eurtotal = CDbl(txtInputAmount.Text) / 3
UStotal = Eurtotal * 2
Australiantotal = Eurtotal * 4
FranceFrantotal = Eurtotal * 5
Mexicototal = Eurtotal * 6
Japantotal = Eurtotal * 7
ElseIf chkAustralian.Checked = True Then
Eurtotal = CDbl(txtInputAmount.Text) / 4
UStotal = Eurtotal * 2
Australiantotal = Eurtotal * 4
FranceFrantotal = Eurtotal * 5
Mexicototal = Eurtotal * 6
Japantotal = Eurtotal * 7
ElseIf chkFranceFrancs.Checked = True Then
Eurtotal = CDbl(txtInputAmount.Text) / 5
UStotal = Eurtotal * 2
Australiantotal = Eurtotal * 4
FranceFrantotal = Eurtotal * 5
Mexicototal = Eurtotal * 6
Japantotal = Eurtotal * 7
ElseIf chkMexicoPesos.Checked = True Then
Eurtotal = CDbl(txtInputAmount.Text) / 6
UStotal = Eurtotal * 2
Australiantotal = Eurtotal * 4
FranceFrantotal = Eurtotal * 5
Mexicototal = Eurtotal * 6
Japantotal = Eurtotal * 7
ElseIf chkJapanYen.Checked = True Then
Eurtotal = CDbl(txtInputAmount.Text) / 7
UStotal = Eurtotal * 2
Australiantotal = Eurtotal * 4
FranceFrantotal = Eurtotal * 5
Mexicototal = Eurtotal * 6
Japantotal = Eurtotal * 7

End If
End Sub

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
ListBox1.Items.Clear()
'Validate input
'Only numbers 1-10,000
If Not IsNumeric(txtInputAmount.Text) And txtInputAmount.Text <> "" Or Val(txtInputAmount.Text) < 1 Or Val(txtInputAmount.Text) > 10000 Then

MessageBox.Show("Only use numbers 1 - 10,000" + Environment.NewLine & _
"Please enter valid value to continue.", "Input Error")

'Add results to listbox based on currency selected
ElseIf chkEur.Checked = True Then
Call ConversionRate()
ListBox1.Items.Add("Euro Rate" + " " + CStr(Eurtotal))
ListBox1.Items.Add("US Rate" + " " + CStr(UStotal))
ListBox1.Items.Add("Australian Rate" + " " + CStr(Australiantotal))
ListBox1.Items.Add("France Franc Rate" + " " + CStr(FranceFrantotal))
ListBox1.Items.Add("Mexico Pesos Rate" + " " + CStr(Mexicototal))
ListBox1.Items.Add("Japan Yen Rate" + " " + CStr(Japantotal))
ElseIf chkUS.Checked = True Then
Call ConversionRate()
ListBox1.Items.Add("Euro Rate" + " " + CStr(Eurtotal))
ListBox1.Items.Add("US Rate" + " " + CStr(UStotal))
ListBox1.Items.Add("Australian Rate" + " " + CStr(Australiantotal))
ListBox1.Items.Add("France Franc Rate" + " " + CStr(FranceFrantotal))
ListBox1.Items.Add("Mexico Pesos Rate" + " " + CStr(Mexicototal))
ListBox1.Items.Add("Japan Yen Rate" + " " + CStr(Japantotal))
Call ConversionRate()
ListBox1.Items.Add("Euro Rate" + " " + CStr(Eurtotal))
ListBox1.Items.Add("US Rate" + " " + CStr(UStotal))
ListBox1.Items.Add("Australian Rate" + " " + CStr(Australiantotal))
ListBox1.Items.Add("France Franc Rate" + " " + CStr(FranceFrantotal))
ListBox1.Items.Add("Mexico Pesos Rate" + " " + CStr(Mexicototal))
ListBox1.Items.Add("Japan Yen Rate" + " " + CStr(Japantotal))
ElseIf chkAustralian.Checked = True Then
Call ConversionRate()
ListBox1.Items.Add("Euro Rate" + " " + CStr(Eurtotal))
ListBox1.Items.Add("US Rate" + " " + CStr(UStotal))
ListBox1.Items.Add("Australian Rate" + " " + CStr(Australiantotal))
ListBox1.Items.Add("France Franc Rate" + " " + CStr(FranceFrantotal))
ListBox1.Items.Add("Mexico Pesos Rate" + " " + CStr(Mexicototal))
ListBox1.Items.Add("Japan Yen Rate" + " " + CStr(Japantotal))
ElseIf chkFranceFrancs.Checked = True Then
Call ConversionRate()
ListBox1.Items.Add("Euro Rate" + " " + CStr(Eurtotal))
ListBox1.Items.Add("US Rate" + " " + CStr(UStotal))
ListBox1.Items.Add("Australian Rate" + " " + CStr(Australiantotal))
ListBox1.Items.Add("France Franc Rate" + " " + CStr(FranceFrantotal))
ListBox1.Items.Add("Mexico Pesos Rate" + " " + CStr(Mexicototal))
ListBox1.Items.Add("Japan Yen Rate" + " " + CStr(Japantotal))
ElseIf chkMexicoPesos.Checked = True Then
Call ConversionRate()
ListBox1.Items.Add("Euro Rate" + " " + CStr(Eurtotal))
ListBox1.Items.Add("US Rate" + " " + CStr(UStotal))
ListBox1.Items.Add("Australian Rate" + " " + CStr(Australiantotal))
ListBox1.Items.Add("France Franc Rate" + " " + CStr(FranceFrantotal))
ListBox1.Items.Add("Mexico Pesos Rate" + " " + CStr(Mexicototal))
ListBox1.Items.Add("Japan Yen Rate" + " " + CStr(Japantotal))
ElseIf chkJapanYen.Checked = True Then
Call ConversionRate()
ListBox1.Items.Add("Euro Rate" + " " + CStr(Eurtotal))
ListBox1.Items.Add("US Rate" + " " + CStr(UStotal))
ListBox1.Items.Add("Australian Rate" + " " + CStr(Australiantotal))
ListBox1.Items.Add("France Franc Rate" + " " + CStr(FranceFrantotal))
ListBox1.Items.Add("Mexico Pesos Rate" + " " + CStr(Mexicototal))
ListBox1.Items.Add("Japan Yen Rate" + " " + CStr(Japantotal))
End If
End Sub

Private Sub chkEur_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEur.CheckedChanged
Call DisableCheckboxes()
End Sub

Private Sub chkUS_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkUS.CheckedChanged
Call DisableCheckboxes()
End Sub

Private Sub chkCanadian_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCanadian.CheckedChanged
Call DisableCheckboxes()
End Sub

Private Sub chkAustralian_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAustralian.CheckedChanged
Call DisableCheckboxes()
End Sub

Private Sub chkFranceFrancs_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFranceFrancs.CheckedChanged
Call DisableCheckboxes()
End Sub

Private Sub chkMexicoPesos_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkMexicoPesos.CheckedChanged
Call DisableCheckboxes()
End Sub

Private Sub chkJapanYen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkJapanYen.CheckedChanged
Call DisableCheckboxes()
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
chkMexicoPesos.Enabled = True
chkEur.Enabled = True
chkAustralian.Enabled = True
chkFranceFrancs.Enabled = True
chkUS.Enabled = True
chkJapanYen.Enabled = True

chkMexicoPesos.Checked = False
chkEur.Checked = False
chkAustralian.Checked = False
chkFranceFrancs.Checked = False
chkUS.Checked = False
chkJapanYen.Checked = False

ListBox1.Items.Clear()

txtInputAmount.Text = ""

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

Private Sub HelpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpToolStripMenuItem.Click
MsgBox("Input a numerical value 1-10,000 in the textfield." + Environment.NewLine & _
"Select currency by checking the country's check box." + Environment.NewLine & _
"Click the calculate button to display results in listbox." + Environment.NewLine & _
"Click the clear button to reset calculator." + Environment.NewLine & _
"Click the exit button to close program." + Environment.NewLine)
End Sub
End Class
``````
0
Question by:DOCDGA

LVL 24

Expert Comment

Where to start??

First, your code violates about every OO principle imaginable. Create an interface or abstract base class for the conversion, and instantiate the concrete class that is required when a button is clicked. This is generally referred to as the Strategy design pattern. Select Case statements or ElseIf statements are a good indication you should use this pattern.

Second, why do you "need" to use an array? Sounds like an artificial requirement.

Third, "+" is poor form in VB for string concatenation. Use & if you must; String.Format is probably a better option, especially when dealing with currency display.

Fourth, consider using OrElse and AndAlso to short-circuit Boolean evaluation.

Fifth, you've got a bunch of old VB syntax still. Val, Call, and CStr are generally considered outdated.

Sixth, you can have a single method handle events on multiple objects. For example,
Private Sub CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAustralian.CheckedChanged, chkFranceFrancs.CheckedChanged
DisableCheckboxes
End Sub
0

LVL 24

Expert Comment

Oh, and radio buttons would certainly remove teh logic for turning on/off check boxes.
0

LVL 24

Accepted Solution

And, since that sounds grumpy, here's working demo code that is somewhat better. (I'd split these into multiple files, but that's my preference.)

Public Class Form1
Private _converters() As EuroConverterBase = {New EuroConverter, New CanadianDollarConverter}

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
ListBox1.Items.Clear()

'Validate input
'Only numbers 1-10,000
If Not IsNumeric(txtInputAmount.Text) And txtInputAmount.Text <> "" Or Val(txtInputAmount.Text) < 1 Or Val(txtInputAmount.Text) > 10000 Then

MessageBox.Show("Only use numbers 1 - 10,000" + Environment.NewLine & _
"Please enter valid value to continue.", "Input Error")

'Add results to listbox based on currency selected
Else
Dim inputAmount As Double
Double.TryParse(txtInputAmount.Text, inputAmount)

Dim euroAmount As Double = _converter.ConvertToEuros(inputAmount)

For Each converter As EuroConverterBase In _converters
Next
End If
End Sub

Private _converter As EuroConverterBase
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
_converter = New EuroConverter
End Sub
End Class

Public MustInherit Class EuroConverterBase
Protected MustOverride ReadOnly Property ConversionRate As Double
Public MustOverride ReadOnly Property CurrencyName As String

''' <summary>
''' Converts from Euros to the specified currency
''' </summary>
''' <param name="amount"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ConvertFromEuros(ByVal amount As Double) As Double
Return amount * ConversionRate
End Function

''' <summary>
''' Converts to Euros from the specified currency
''' </summary>
''' <param name="amount"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ConvertToEuros(ByVal amount As Double) As Double
Return amount / ConversionRate
End Function
End Class

Inherits EuroConverterBase

Protected Overrides ReadOnly Property ConversionRate As Double
Get
Return 3.0#
End Get
End Property

Public Overrides ReadOnly Property CurrencyName As String
Get
End Get
End Property
End Class

Public Class EuroConverter
Inherits EuroConverterBase

Protected Overrides ReadOnly Property ConversionRate As Double
Get
Return 1.0#
End Get
End Property

Public Overrides ReadOnly Property CurrencyName As String
Get
Return "Euro"
End Get
End Property
End Class
0

## Featured Post

Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

#### Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!