Added loan and interest calculations
authorArjen Baart <arjen@andromeda.nl>
Sun, 5 May 2019 15:17:03 +0000 (17:17 +0200)
committerArjen Baart <arjen@andromeda.nl>
Sun, 5 May 2019 15:17:03 +0000 (17:17 +0200)
loan.cpp [new file with mode: 0644]
makefile

diff --git a/loan.cpp b/loan.cpp
new file mode 100644 (file)
index 0000000..3508570
--- /dev/null
+++ b/loan.cpp
@@ -0,0 +1,187 @@
+
+#include "account.h"
+
+#include <fstream>
+#include <iomanip>
+#include <list>
+#include <getopt.h>
+
+//  Not used but referred to from account.cpp
+
+Booking   b[4];
+Mutation  m[20];
+
+int n_m = 0, n_b = 0;
+
+/*
+ *  A Loan is list of transactions. each transaction is a date and a balance.
+ *  The balance states the amount added to de loan (Debit) or payed back (Credit).
+ */
+
+//----------------------------transaction---------------------
+
+class transaction
+{
+public:
+
+   date     booked;
+   balance  mutation;
+
+   friend std::istream & operator>>(std::istream &, transaction &);
+   friend std::ostream & operator<<(std::ostream &, transaction );
+};
+
+std::istream &operator>>(std::istream &s, transaction &t)
+{
+   amount debit, credit;
+
+   s >> t.booked;
+   s >> debit >> credit;
+   t.mutation = balance(debit, credit);
+
+   return s;
+}
+
+std::ostream & operator<<(std::ostream &s, transaction t)
+{
+   s << t.booked << " " << t.mutation;
+
+   return s;
+}
+
+//----------------------------loan----------------------------
+
+class loan
+{
+   std::list<transaction>   records;
+
+public:
+
+   void read(char *filename);
+   void report();
+};
+
+void loan::read(char *filename)
+{
+   std::ifstream  in(filename);
+
+   while (in)
+   {
+      transaction tr;
+      in >> tr;
+
+      if (in)
+      {
+         records.push_back(tr);
+      }
+   }
+}
+
+void loan::report()
+{
+   std::list<transaction>::const_iterator  ri;
+   transaction   tr, prev_tr;
+   balance       saldo, prev_saldo;
+
+   for (ri = records.begin(); ri != records.end(); ri++)
+   {
+      tr = *ri;
+      std::cout << tr;
+
+      saldo += tr.mutation;
+      saldo = ~saldo;
+      std::cout << "    " << saldo;
+
+      if (ri != records.begin())
+      {
+         int  days_of_interest = tr.booked - prev_tr.booked;
+         amount  loaned_low,   loaned_high;
+         amount  interest_low, interest_high;
+
+         loaned_low = prev_saldo.debit();
+         if (loaned_low > 2500000)
+         {
+            loaned_high = loaned_low - 2500000;
+            loaned_low  = 2500000;
+         }
+         interest_low  = (loaned_low  * days_of_interest *  9 / 365 + 50) / 100;
+         interest_high = (loaned_high * days_of_interest * 12 / 365 + 50) / 100;
+
+         std::cout << std::setw(6) << std::right << days_of_interest;
+         std::cout << " " << interest_low << " " << interest_high << " " << interest_low + interest_high;
+      }
+      std::cout << "\n";
+      prev_tr = tr;
+      prev_saldo = saldo;
+   }
+}
+
+//----------------------------main----------------------------
+
+static char  helptext[] = 
+     "Usage: loan [options] loanfile\n"
+     "\n"
+     "Options:\n"
+     "  -f <start>\n"
+     "  --from <start>   :  Calculate the report from <start> date\n"
+     "  -t <end>\n"
+     "  --to <end>       :  Calculate the report until <end> date\n"
+     "  --help           :  Print this helpful message\n"
+     "  -V\n"
+     "  --version        :  Print the version number\n";
+
+static struct option long_options[] =
+{
+   { "help",       no_argument,        0,   'h' },
+   { "from",       required_argument,  0,   'f' },
+   { "to",         required_argument,  0,   't' },
+   { "version",    no_argument,        0,   'V' },
+   { 0, 0, 0, 0 }
+};
+
+int main(int argc, char * argv[])
+{
+
+   int      c;
+   int      option_index = 0;
+
+   String from_option, to_option;
+
+   while ((c = getopt_long(argc, argv, "hf:t:V", long_options, &option_index)) != -1)
+   {
+      switch (c)
+      {
+      case 'h':
+         std::cout << helptext;
+         exit(0);
+         break;
+
+      case 'V':
+         std::cout << "account version 1.4\n";
+         exit(0);
+         break;
+
+      case 'f':
+         from_option = String(optarg);
+         break;
+
+      case 't':
+         to_option = String(optarg);
+         break;
+      }
+   }
+
+   if (optind == argc)
+   {
+      std::cerr << "Usage: loan [options] loan_file\n";
+      exit(1);
+   }
+
+   loan loan_account;
+
+   loan_account.read(argv[optind]);
+   loan_account.report();
+
+   return 0;
+
+}
index 09ce41c..cdb8d2b 100644 (file)
--- a/makefile
+++ b/makefile
@@ -6,7 +6,7 @@ SRCS = account_main.cpp account.cpp postscript.cpp
 
 LIBS = -lAXE -L/usr/X11R6/lib -lX11 -lps
 
-all      : account bank
+all      : account bank loan
 
 account  : $(OBJS)
        g++ $(OBJS) -o account $(LIBS)
@@ -14,14 +14,17 @@ account  : $(OBJS)
 bank     : bank.o
        g++ bank.o -o bank $(LIBS)
 
+loan     : loan.o account.o postscript.o
+       g++ loan.o account.o postscript.o -o loan $(LIBS)
+
 account_main.o : account.h
 
 account.o : account.h postscript.h
 
 postscript.o : postscript.h
 
-install  : account bank
-       cp account bank /usr/local/bin
+install  : account bank loan
+       cp account bank loan /usr/local/bin
        mkdir -p /usr/local/xslt
        cp balans.xsl profit.xsl saldibalans.xsl cashflow.xsl /usr/local/xslt
        mkdir -p /usr/local/share/afm
@@ -30,6 +33,7 @@ install  : account bank
 clean    :
        rm -f account core
        rm -f bank bank.o
+       rm -f loan loan.o
        rm -f $(OBJS)
        rm -f saldi.xml saldibalans.ps grootboek.ps