#!perl -w use strict; =head1 NAME compound_interest.pl - more advanced one to generate a txt file =head1 DESCRIPTION Calculate compound interest given a nest_egg, APR, and duration in years. =head1 SYNOPSIS compound_interest.pl [man|help] compound_interest.pl [nest_egg=<loan_amount>] [apr=<interest>] [duration=<years>] [outfile=<filename>] =head1 OPTIONS All options must have a single or double hyphen in front of the name. Only the minimum name that is unique is required. =over 4 =item --apr Rate of interest annually. =item --duration Number of years to calculate the loan. =item --nest_egg Amount of loan to calculate compounding interest. =item --outfile Write to an outfile named filename or to Standard Out. =item --year Year to begin the calculation. =item --help or ? Show a help page =item --test Test all functions. =back =head1 EXAMPLES =head1 NOTES =head1 AUTHOR 5-1-2012 - Huimin Tao(Helen) 5-4-2012 - extended by David Scott =cut package main; # libraries use SelfLoader; use FileHandle; use Getopt::Long; use Pod::Usage; pod2usage(0) if $#ARGV == -1; # no arguments if @ARGV has no entries # options # First, we'll set up the variables we want to use. my $outfile = "interest.txt"; # This is the filename of our report. my $nest_egg = 10000; # $nest_egg is our starting amount my $year = 2000; # This is the starting year for our table. my $duration = 10; # How many years are we saving up? my $apr = 9.5; # This is our annual percentage rate. my $help = 0; my $test = 0; GetOptions( 'help|?' => \$help, test => \$test, "outfile:s" => \$outfile, "nest_egg:i" => \$nest_egg, "year:i" => \$year, "duration:i" => \$duration, "apr:f" => \$apr, ); pod2usage(1) if $help; pod2usage(-message => 'Spurious argument(s) left: ' . "@ARGV", -verbose => 1) if $#ARGV != -1; test() if $test; print "Writing interest calculation to ", $outfile ? $outfile : "Standard Out", "\n"; my $file = open_report($outfile); print $file print_headers(); print $file interest_report($nest_egg, $year, $duration, $apr); print $file report_footer($nest_egg, $duration, $apr); close $file; print "Completed writing to ", $outfile ? $outfile : "Standard Out", "\n"; exit(0); # Functions follow: # # my $file_handle = open_report(); sub open_report { my ($outfile) = @_; my $file = FileHandle->new(); if ( $outfile ) { open ($file, ">$outfile") or die("Can't open report: $!"); } else { open ($file, ">&STDOUT") or die("Can't open report to STDOUT: $!"); } return $file; } # my $text = print_headers(); sub print_headers { # Print the headers for our report. return "Year". "\t". "Balance". "\t". "Interest". "\t". "New balance". "\n"; } # my $interest = calculate_interest($nest_egg, $apr); sub calculate_interest { # Given a nest egg and an APR, how much interest do we collect? my ($nest_egg, $apr) = @_; return int (($apr / 100) * $nest_egg * 100) / 100; } # my $text = interest_report($nest_egg, $year, $duration, $apr); sub interest_report { # Get our parameters. Note that these variables won't clobber the # global variables with the same name. my ($nest_egg, $year, $duration, $apr) = @_; # We have two local variables, so we'll use my to declare them here. my ($i, $line); my $result = ""; # Calculate interest for each year. for $i (1 .. $duration) { $year++; my $interest = calculate_interest($nest_egg, $apr); $line = join("\t", $year, $nest_egg, $interest, $nest_egg + $interest) . "\n"; $result .= $line; $nest_egg += $interest; } return $result; } # my $text = report_footer($nest_egg, $duration, $apr); sub report_footer { my ($nest_egg, $duration, $apr) = @_; my $result = "\n Our original assumptions:\n"; $result .= " Nest egg: $nest_egg\n"; $result .= " Number of years: $duration\n"; $result .= " Interest rate: $apr\n"; return $result; } __END__ sub test { use Test::More tests => 5; my $headers = "Year\tBalance\tInterest\tNew balance\n"; is( print_headers(), $headers, 'Test header on interest calculation'); # my $interest = calculate_interest($nest_egg, $apr); my $amount = 50; is( calculate_interest(1000, 5.0), $amount, 'Calculate interest(1000,5.0)'); # my $text = interest_report($nest_egg, $year, $duration, $apr); my $report = "2001\t1000\t50\t1050\n". "2002\t1050\t52.5\t1102.5\n" . "2003\t1102.5\t55.12\t1157.62\n" . "2004\t1157.62\t57.88\t1215.5\n" . "2005\t1215.5\t60.77\t1276.27\n"; is( interest_report(1000, 2000, 5, 5.0), $report, 'interest report(1000, 2000, 5, 5.0)'); # my $text = report_footer($nest_egg, $duration, $apr); my $footer = "\n Our original assumptions:\n" . " Nest egg: 1000\n" . " Number of years: 5\n" . " Interest rate: 5\n"; is( report_footer(1000, 5, 5.0), $footer, 'report footer(1000, 5, 5.0)'); is(1,1,'Dummy Test'); exit(0); }