'
' Program name: loan.bas
'
' Program that computes for a client the montly payments, interest, principal
' and balance for a certain amount with a certain fixed interest rate and for
' a certain number of years and prints the amortization table.
'
' The executable can be launched with four parameters : name of the client,
' amount, interest and term.
'
' The parameters must be entered using a command line arguments in a string of
' the form: "&name=abcd&amount=xxx&rate=rr.rr&term=zz"
' where name is the client name (a string without space)
' xxx is the amount (a long)
' rr.rr is the rate (a float) - maximum 20
' and zz is the number of years (an int) - maximun 99
' e.g. loan &name=John&amount=12000&rate=2.57&term=5
'
' NOTE:
' Space(s) can be included between arguments but not between the argument
' and the sign "=" nor between the sign "=" and the argument value such as:
' e.g. loan & name=John & amount=12000 & rate=2.57 & term=5
'
' NOTE:
' Only the dot sign "." is allowed in float to separate the integer part from
' its decimal part (no comma sign "," anywhere in a float).
'
' NOTE:
' If no parameters are set, the program ask you the values.
' If there is no value entered (just hit the Enter key), the program computes
' the default values (Client, 10000.00, 3.5% and 5 years).
'
'
' title of the windows when used in a console
'
'
' miscelaneous variables initialisation
'
Arg$ = ""
NbArg% = 0
lastPayment# = 1.00
Year% = 1
'
' Get the Arguments of the command line
'
'
' force parameters if the argument line is empty.
'
' LineArg$ = "& name=- & amount=10000.00 & rate=3.5 & term=1"
PRINT "Please enter your name (any name, may be empty but no space): ";
PRINT "Enter the amount to borrow (long, decimal characters) e.g. 1000000: ";
PRINT "Enter the % rate of the loan (float, decimal characters with .) e.g. 1.65 : ";
PRINT "Enter the number of year of the loan (integer, decinal characters) e.g. 12: ";
LineArg$ = "& name=" + yourname$ + " & amount=" + amount$ + " & rate=" + rate$ + " & term=" + term$
'
' store the execution start time
'
timeStart#
= TIMER(0.001)
'
' check if the expected arguments names and argument values are the right ones and if so get their relevant values
' if one argument name or argument type isn't what is expected, use the default argument values.
'
GetChar$
= MID$(LineArg$
, i%
, 1) IF (GetChar$
<> "" AND GetChar$
<> " " AND GetChar$
<> "&") THEN Arg$ = Arg$ + GetChar$
' PRINT "problem with the " + CHR$(34) + "first argument naming that should be name= but is: " + CHR$(34) + LEFT$(Arg$, 5) + CHR$(34)
' PRINT "problem with the content of name= that is: " + CHR$(34) + MID$(Arg$, 6) + CHR$(34) + " and isn't a valid string!"
' PRINT "problem with the " + CHR$(34) + "second argument naming that should be amount= but is: " + CHR$(34) + LEFT$(Arg$, 7) + CHR$(34)
' PRINT "problem with the content of amount= that is: " + CHR$(34) + MID$(Arg$, 8) + CHR$(34) + " and isn't a valid float!"
' PRINT "problem with the " + CHR$(34) + "third argument naming that should be rate= but is: " + CHR$(34) + LEFT$(Arg$, 5) + CHR$(34)
' PRINT "problem with the content of rate= that is: " + CHR$(34) + MID$(Arg$, 6) + CHR$(34) + " and isn't a valid float!"
' PRINT "problem with the " + CHR$(34) + "last argument naming that should be term= but is: " + CHR$(34) + LEFT$(Arg$, 5) + CHR$(34)
' PRINT "problem with the content of rate= that is: " + CHR$(34) + MID$(Arg$, 6) + CHR$(34) + " and isn't a valid integer!"
ArrayArg$(NbArg%) = Arg$
Arg$ = ""
NbArg% = NbArg% + 1
'
' Default arguments
'
IF ArrayArg$
(0) <> "" AND ArrayArg$
(0) <> " " AND ArrayArg$
(0) <> "-" THEN name$
= "Client" ' default name
IF ArrayArg$
(1) <> "" AND ArrayArg$
(0) <> " " AND VAL(ArrayArg$
(1)) > 1 THEN Amount#
= VAL(ArrayArg$
(1)) Amount# = 10000.00 ' default amount
IF ArrayArg$
(2) <> "" AND ArrayArg$
(2) <> " " AND VAL(ArrayArg$
(2)) > 0 THEN Rate#
= VAL(ArrayArg$
(2)) / 100 Rate# = 3.5 / 100 ' default rate
Rate# = 20.00 ' default maximum rate
IF ArrayArg$
(3) <> "" AND ArrayArg$
(3) <> " " AND VAL(ArrayArg$
(3)) > 0 THEN Term# = 5 ' default term
Term# = 99 ' default maximum term
'
' start the main routines of the program
'
'
' computation formulas of the monthly payment and global cost
'
Payment# = Amount# * Rate# / 12 * ((1 + Rate# / 12) ^ (Term# * 12)) / ((1 + Rate# / 12) ^ (Term# * 12) - 1)
Cost# = (Term# * 12 * Payment#) - Amount#
'
' prepare the first result$ string for output.
'
result$
= CHR$(13) + CHR$(10) + "Dear " + name$
+ ", the requested loan goes as follows:" + CHR$(13) + CHR$(10)Ratio# = 100 / (Amount# / Cost#)
'
' add the next statements
'
result$
= result$
+ "Loan" + CHR$(9) + "details" + CHR$(13) + CHR$(10)result$
= result$
+ "Amount" + CHR$(9) + printUsing$
(Amount#
) + CHR$(13) + CHR$(10)result$
= result$
+ "Rate" + CHR$(9) + printUsing$
(Rate#
* 100) + "%" + CHR$(13) + CHR$(10)result$
= result$
+ "Cost" + CHR$(9) + printUsing$
(Cost#
) + " (" + printUsing$
(Ratio#
) + "%)" + CHR$(13) + CHR$(10)
'
' add the monthly computations
'
'
' add the next statements
'
result$
= result$
+ "month" + CHR$(9) + CHR$(9) + "payment" + CHR$(9) + CHR$(9) + "interest" + CHR$(9) + "principal" + CHR$(9) + "balance" + CHR$(13) + CHR$(10) '
' Call the compute sub routine
'
CALL compute
(Month%
, Amount#
, Rate#
, Interest#
, Payment#
, Principal#
, lastPayment#
, result$
) Month% = 0
Year% = Year% + 1
'
' add the elapsed time to execute the program
'
'
' add the last statements
'
result$ = result$ + "This table was generated in " + printUsing$(((timeEnd# - timeStart#) * 1000)) + " ms. "
result$ = result$ + "(on a 3GHz CPU 1 ms = 3,000,000 cycles)"
'
' option: unrem to create the .txt files
'
'CALL createFile("loan-calculator.txt", result$)
'
' print the result into the console
'
'
' wait for an input
'
'INPUT (a)
'
' end of the program and exit from the console with a 200 return
'
'SYSTEM 200
'
' sub routine to create the loan-bas.html and loan-bas.txt files
'
SUB createFile
(fileName$
, result$
) '
' delete the old fileName$ file
'
'
' create the fileName$ file
'
IF existFile%
(fileName$
) = 0 THEN
'
' sub routine to compute and print the monthly elements
'
SUB compute
(Month%
, Amount#
, Rate#
, Interest#
, Payment#
, Principal#
, lastPayment#
, result$
) Amount#, _
Rate#, _
Interest#, _
Payment#, _
Principal#, _
lastPayment#, _
formatNumber$, _
result$
monthList$(0) = "January"
monthList$(1) = "February"
monthList$(2) = "March"
monthList$(3) = "April"
monthList$(4) = "May "
monthList$(5) = "June"
monthList$(6) = "July"
monthList$(7) = "August"
monthList$(8) = "September"
monthList$(9) = "October"
monthList$(10) = "November"
monthList$(11) = "December"
interest$ = "Interest: "
payment$ = "Payment: "
principal$ = "Principal: "
Interest# = (Amount# * Rate#) / 12
Amount# = (Amount# - Payment#) + Interest#
Principal# = Payment# - Interest#
lastPayment# = 0
Payment# = Amount#
Principal# = Amount# - Interest#
Amount# = 0
Amount# = 0
Payment# = 0
Interest# = 0
Principal# = 0
'
' add the next statements
'
IF monthList$
(Month%
) = "February" OR monthList$
(Month%
) = "September" OR monthList$
(Month%
) = "November" OR monthList$
(Month%
) = "December" THEN result$
= result$
+ monthList$
(Month%
) + CHR$(9) result$
= result$
+ monthList$
(Month%
) + CHR$(9) + CHR$(9) result$
= result$
+ printUsing$
(Payment#
) + CHR$(9) result$
= result$
+ printUsing$
(Payment#
) + CHR$(9) + CHR$(9) result$
= result$
+ printUsing$
(Interest#
) + CHR$(9) result$
= result$
+ printUsing$
(Interest#
) + CHR$(9) + CHR$(9) result$
= result$
+ printUsing$
(Principal#
) + CHR$(9) result$
= result$
+ printUsing$
(Principal#
) + CHR$(9) + CHR$(9) result$
= result$
+ printUsing$
(Amount#
) + CHR$(13) + CHR$(10)
'
' function to check if an argument is a valid string to be processed by this loan.bas program
'
isValidArg% = 0
isStr% = 0
base$
= "abcdefghijklmnopqrstuvwxyz-" isValidArg% = 0
arg$ = ""
isValidArg% = 0
isValidArg% = 0
isStr% = 0
argChar$
= MID$(t$
, i
, 1) isValidArg% = isValidArg% + 1
isStr% = 1
isStr% = 0
isStr% = 0
isValidArg% = 0
'
'
'
existFile% = 1
existFile% = 0
'
' function to format the integer part of a float and round its decimal part
'
printUsing$ = intPart$(nb#) + decPart$(nb#)
'
' function to format and round the decimal part of a float
'
dot$ = "."
partdec$ = ".00"
partdec$ = ".00"
partdec$ = partdec$ + "0"
partdec$
= LEFT$(partdec$
, 3) decPart$ = partdec$
'
' function to format the integer part of a float
'
idx = m3 + 4 + (m3 = 0)
i$
= LEFT$(i$
, idx
) + "," + MID$(i$
, idx
+ 1) idx = idx + 4
i$ = "0"
i$ = "-" + i$
intPart$ = i$