[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

Currency calculator

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 Canadiantotal 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
            chkCanadian.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
            Canadiantotal = Eurtotal * 3
            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
            Canadiantotal = Eurtotal * 3
            Australiantotal = Eurtotal * 4
            FranceFrantotal = Eurtotal * 5
            Mexicototal = Eurtotal * 6
            Japantotal = Eurtotal * 7
        ElseIf chkCanadian.Checked = True Then
            Eurtotal = CDbl(txtInputAmount.Text) / 3
            UStotal = Eurtotal * 2
            Canadiantotal = Eurtotal * 3
            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
            Canadiantotal = Eurtotal * 3
            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
            Canadiantotal = Eurtotal * 3
            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
            Canadiantotal = Eurtotal * 3
            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
            Canadiantotal = Eurtotal * 3
            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("Canadian Rate" + " " + CStr(Canadiantotal))
            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("Canadian Rate" + " " + CStr(Canadiantotal))
            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 chkCanadian.Checked = True Then
            Call ConversionRate()
            ListBox1.Items.Add("Euro Rate" + " " + CStr(Eurtotal))
            ListBox1.Items.Add("US Rate" + " " + CStr(UStotal))
            ListBox1.Items.Add("Canadian Rate" + " " + CStr(Canadiantotal))
            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("Canadian Rate" + " " + CStr(Canadiantotal))
            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("Canadian Rate" + " " + CStr(Canadiantotal))
            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("Canadian Rate" + " " + CStr(Canadiantotal))
            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("Canadian Rate" + " " + CStr(Canadiantotal))
            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
        chkCanadian.Enabled = True
        chkAustralian.Enabled = True
        chkFranceFrancs.Enabled = True
        chkUS.Enabled = True
        chkJapanYen.Enabled = True

        chkMexicoPesos.Checked = False
        chkEur.Checked = False
        chkCanadian.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

Open in new window

0
DOCDGA
Asked:
DOCDGA
  • 3
1 Solution
 
Jeff CertainCommented:
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
 
Jeff CertainCommented:
Oh, and radio buttons would certainly remove teh logic for turning on/off check boxes.
0
 
Jeff CertainCommented:
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
                ListBox1.Items.Add(String.Format("{0} rate: {1:F2}", converter.CurrencyName, converter.ConvertFromEuros(euroAmount)))
            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
        _converter = New CanadianDollarConverter
    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

Public Class CanadianDollarConverter
    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
            Return "Canadian dollar"
        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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now