#!/usr/bin/perl -w
# Please run with -h for help
# Please run with -c for configuration (first time)
use strict;
# use Date::Calc; -- for later
my ($i, %conf);
CheckArg(@ARGV) if @ARGV;
die "file simplemoney.conf does not exist, please run -c to configure\
+n" unless (-e "simplemoney.conf"); #check for existance of configurat
+ion file
open (RCONF, "simplemoney.conf") || die "Could not open simplemoney.c
+onf $!";
foreach ("rt", "balance", "transaction"){ #load config variables into
+a hash
chomp ($conf{$_} = <RCONF>);
}
close (RCONF) || die "Could not close simplemoney.conf $!";
unless (-e $conf{rt} && -e $conf{balance} && -e $conf{transaction}) {
+
die "One of the required files is missing, please run with -c to co
+nfigure\n";
}
while (1) {
my ($command, $arg1, $arg2);
print "Your command (help for list of commands):\n";
chomp ($command = <STDIN>);
if ($command =~ /^balance/) {
print CalcBalance($conf{balance});
} elsif ($command =~ /^help/i) {
PrintHelp();
} elsif ($command =~ /^deposit/i) {
(undef, $arg1, $arg2) = split (/\s+/, $command);
CalcBalance($conf{balance},$arg1);
AddTransaction($conf{transaction},$arg2);
} elsif ($command =~ /^withdraw/i) {
(undef, $arg1, $arg2) = split (/\s+/, $command);
$arg1 = -$arg1;
CalcBalance($conf{balance},$arg1);
AddTransaction($conf{transaction},$arg2);
} elsif ($command =~ /^rt/i) {
print "RT is not yet supported.";
} elsif ($command =~ /^exit/i || $command =~ /^quit/i || $command
+=~ /^e/i || $command =~ /^q/i) {
exit 0;
} else { #must be undefined command, try again.
print "\nBad Command\n";
next;
}
}
###Subs###
sub PrintHelp {
print "This program calculates your money balance, please run with the
+ -c flag to configure.\n";
print "Flags:\n";
print " -c -- Configure\n";
print " -h -- This file\n";
print "Default Files:\n";
print " rt.log -- Reocuring transaction configuration\n";
print " balance.log -- Backlog of user's balance\n";
print " transaction.log -- Backlog of transaction descript
+ions\n";
print " Configuration File: simplemoney.conf\n";
print "Commands:\n";
print " help -- show this file.\n";
print " deposit <arg1> <arg2> -- deposit <arg1> dollars with
+<arg2> description in the log.\n";
print " withdraw <arg1> <arg2> -- same as above, but withdraw
+.\n";
print " rt -- not yet supported.\n";
print " exit, quit, e, or q -- quit the program.\n";
+
print "Note: running deposit or withdraw with no arguments will do not
+hing, running with only a value will print 'this description is inten
+tionaly left blank' in balance.log\n"
}
sub CheckArg {
my $rargs = (shift @_);
my $arg;
foreach ($rargs) {
if ($_ =~ /^-h/) {
PrintHelp();
exit 0;
}
elsif ($_ =~ /^-c/) {
Configure();
}
else { #must be an undefined option
die"Undefined Option: Please run the program with the -h flag
+for help\n";
}
}
}
sub Configure {
my ($rt, $balance, $transaction);
print "Welcome, please follow the instructions to configure this p
+rogram (this will overwrite your current configuration)\n";
print "Reoccuring transaction file (rt.log by default)?";
chomp ($rt = <STDIN>);
$rt = "rt.log" unless ($rt); #assumes default if user gave
+no input.
open (WRT, ">$rt");
close (WRT);
print "Balance file (balance.log by default)";
chomp ($balance = <STDIN>);
$balance = "balance.log" unless ($balance); #assumes default if us
+er gave no input.
open (WBALANCE, ">$balance") || die "could not open balance.log $
+!";
print WBALANCE "0\n";
close (WBALANCE) || die "could not close balance.log $!";
print "Transaction file (transaction.log by default)";
chomp ($transaction = <STDIN>);
$transaction = "transaction.log" unless ($transaction); #assumes d
+efault if user gave no input.
open (WTRANSACTION, ">$transaction") || die "could not open trans
+action.log $!";
print WTRANSACTION "start\n";
close (WTRANSACTION) || die "could not close transaction.log $!";
#write everything into configuration file
open (WCONF, ">simplemoney.conf") || die "could not open simplemo
+eny.conf $!";
print WCONF "$rt\n$balance\n$transaction";
close (WCONF) || die "could not close simplemoney.conf $!";
}
sub CalcBalance {
my ($balance, @log);
my $location = shift(@_);
open (RBALANCE, $location) || die "Can't open $location $!";
@log = <RBALANCE>;
close (RBALANCE) || die "Can't close $location $!";
$balance = $log[-1];
if ($_[0]) { # we check to see if there is another
# argument (the amount to change) an
+d
# it is not 0.
my $toChange = shift(@_); # we only declare this if another
# argument exists to make the test
# above work.
$balance += $toChange;
open (WBALANCE, ">>$location") || die "Can't open $location $!";
print WBALANCE "$balance\n";
close (WBALANCE) || die "Can't close $location $!";
}
return $balance;
}
sub AddTransaction {
my ($location) = shift(@_);
my ($description) = shift(@_) || "This description is intentionaly
+ left blank";
open (WTRANSACTION, ">>$location") || die "Can't open $location $!
+";
print WTRANSACTION "$description\n";
close (WTRANSACTION) || die "Can't close $location $!";
}
In reply to SimpleMoney
by Fingo
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.