From: Arjen Baart Date: Sun, 5 May 2019 15:17:03 +0000 (+0200) Subject: Added loan and interest calculations X-Git-Url: http://www.andromeda.nl/gitweb/?p=account.git;a=commitdiff_plain;h=11d958ce53aec17c0cd1c8bd5daa8aad7371a344 Added loan and interest calculations --- diff --git a/loan.cpp b/loan.cpp new file mode 100644 index 0000000..3508570 --- /dev/null +++ b/loan.cpp @@ -0,0 +1,187 @@ + +#include "account.h" + +#include +#include +#include +#include + +// 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 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::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 \n" + " --from : Calculate the report from date\n" + " -t \n" + " --to : Calculate the report until 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; + +} diff --git a/makefile b/makefile index 09ce41c..cdb8d2b 100644 --- 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