Module modChange
Sub Main()
'Setup Change Bin
Dim CurrentChange As sChange ' This is what the container has before starting
With CurrentChange
.sQuarters = 100
.sDimes = 100
.sNickles = 100
.sPennies = 100
End With
'I Better Introduce myself...
Console.WriteLine("Welcome to the Change Calculator")
Console.WriteLine("You enter a price and I'll calculate the change")
'leave 3 Lines
Console.WriteLine("")
Console.WriteLine("")
Console.WriteLine("")
Console.WriteLine("Are you ready to begin?")
'Lets get this party started....
Do While Console.ReadLine().ToUpper.Equals("YES")
Console.WriteLine("Please Type the price: ")
Dim decCost As Decimal
Try
decCost = CDec(Console.ReadLine())
Catch ex As InvalidCastException
Console.WriteLine("Invaild Number!")
End Try
Dim NewChange As sChange ' This is what the container has after starting (is is passed byref)
Dim ClientsChange As sChange ' this is what the result is
'Get The change
ClientsChange = CalculateChange(CurrentChange, decCost, NewChange)
'Display some info the the user
'leave 3 Lines
Console.WriteLine("")
Console.WriteLine("")
Console.WriteLine("")
Console.WriteLine("Calculation Results....")
With ClientsChange
Console.WriteLine("")
Console.WriteLine("Total Change: $" & .Total)
Console.WriteLine("Quarters: " & .sQuarters)
Console.WriteLine("Dimes: " & .sDimes)
Console.WriteLine("Nickles: " & .sNickles)
Console.WriteLine("Pennies: " & .sPennies)
End With
'leave 3 Lines
Console.WriteLine("")
Console.WriteLine("")
Console.WriteLine("")
Console.WriteLine("Whats now in the contaner...")
With NewChange
Console.WriteLine("")
Console.WriteLine("Total Change: $" & .Total)
Console.WriteLine("Quarters: " & .sQuarters)
Console.WriteLine("Dimes: " & .sDimes)
Console.WriteLine("Nickles: " & .sNickles)
Console.WriteLine("Pennies: " & .sPennies)
End With
Console.WriteLine("Would you like to make more change?")
'Remove the following line if you dont want the bins capacity to carry over
CurrentChange = NewChange
Loop
Console.WriteLine("GoodBye")
End Sub
Public Structure sChange
Dim sQuarters As Short
Dim sDimes As Short
Dim sNickles As Short
Dim sPennies As Short
Function Total() As Decimal
Dim decTotal As Decimal
decTotal += CDec(sQuarters * 0.25)
decTotal += CDec(sDimes * 0.1)
decTotal += CDec(sNickles * 0.05)
decTotal += CDec(sPennies * 0.01)
Return decTotal
End Function
End Structure
Public Function CalculateChange(ByVal CurrentChange As sChange, ByVal decCost As Decimal, ByRef NewChange As sChange) As sChange
'Calculate the most efficent way to give the client change
Dim ClientsChange As sChange
Dim decCurrentValue As Decimal = decCost
Dim sTmp As Short
'Get the most quarters that we can use...
sTmp = Fix(decCurrentValue / 0.25)
'Do we have enough quarters?
If sTmp > CurrentChange.sQuarters Then
'Subtract what we can use
decCurrentValue -= (CurrentChange.sQuarters * 0.25)
'update new change
ClientsChange.sQuarters = CurrentChange.sQuarters
NewChange.sQuarters = 0
Else
decCurrentValue -= (sTmp * 0.25)
'update new change (remove the quarters we used
ClientsChange.sQuarters = sTmp
NewChange.sQuarters = CurrentChange.sQuarters - sTmp
End If
':Move on to the next biggest divison, Dimes
'Get the most Dimes that we can use...
sTmp = Fix(decCurrentValue / 0.1)
'Do we have enough Dimes?
If sTmp > CurrentChange.sDimes Then
'Subtract what we can use
decCurrentValue -= (CurrentChange.sDimes * 0.25)
'update new change
ClientsChange.sDimes = CurrentChange.sDimes
NewChange.sDimes = 0
Else
decCurrentValue -= (sTmp * 0.1)
'update new change (remove the Dimes we used
ClientsChange.sDimes = sTmp
NewChange.sDimes = CurrentChange.sDimes - sTmp
End If
':Move on to the next biggest divison, Nickels
'Get the most Nickles that we can use...
sTmp = Fix(decCurrentValue / 0.05)
'Do we have enough Nickles?
If sTmp > CurrentChange.sNickles Then
'Subtract what we can use
decCurrentValue -= (CurrentChange.sNickles * 0.05)
'update new change
ClientsChange.sNickles = CurrentChange.sNickles
NewChange.sNickles = 0
Else
decCurrentValue -= (sTmp * 0.05)
'update new change (remove the Nickles we used
ClientsChange.sNickles = sTmp
NewChange.sNickles = CurrentChange.sNickles - sTmp
End If
':Move on to the next, and last, biggest divison, Pennies
'Get the most Pennies that we can use...
sTmp = Fix(decCurrentValue / 0.01)
'Do we have enough Pennies?
If sTmp > CurrentChange.sPennies Then
'Subtract what we can use
decCurrentValue -= (CurrentChange.sPennies * 0.01)
'update new change
ClientsChange.sPennies = CurrentChange.sPennies
NewChange.sPennies = 0
Else
decCurrentValue -= (sTmp * 0.01)
'update new change (remove the Pennies we used
ClientsChange.sPennies = sTmp
NewChange.sPennies = CurrentChange.sPennies - sTmp
End If
'Change calculation complete
'Should check here if we didnt have enough change to give the client
If decCurrentValue <> 0 Then
'We gotz a problem
'but you can handle this
'if you want...
MsgBox("Error Not Enough Change!")
End If
Return ClientsChange
End Function
End Module