Download amort.cc
/*Copyright (C) 2022 Brian R. Smith
* Contact: briansmith2865@gmail.com
**The source code in this file is free software: you can
**redistribute it and/or modify it under the terms of the GNU
**General Public License (GNU GPL) as published by the Free Software
**foundation, either version 3 of the License, or (at your option)
**any later version. The code is distributed WITHOUT ANY WARRANTY;
**without even the implied warranty of MERCHANTABILITY or FITNESS
**FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
**
**As additional permission under GNU GPL version 3 section 7, you
**may distribute non-source (e.g., minimized or compacted) forms of
**that code without the copy of the GNU GPL normally required by
**section 4, provided you include this license notice and a URL
**through which recipients can access the Corresponding Source.
**
** @licend The above is the entire license notice
**for the source code in this file.
*/
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#if defined(__WIN32__)
#include<windows.h>
#define CLS "cls"
#define SLEEP Sleep(1000)
#elif defined(__linux__)
#include<unistd.h>
#define CLS "clear"
#define SLEEP sleep(1)
#else
#error UNSUPPORTED PLATFORM
#endif
using namespace std;
static inline void displaytable(float debt, float amort, float interestrate);
string theCopywrite = "**This program (amort) is free software: you can\n"
"**redistribute it and/or modify it under the terms of the GNU\n"
"**General Public License (GNU GPL) as published by the Free Software\n"
"**foundation, either version 3 of the License, or (at your option)\n"
"**any later version. This program is distributed WITHOUT ANY WARRANTY;\n"
"**without even the implied warranty of MERCHANTABILITY or FITNESS\n"
"**FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.\n"
"**The source code as well as a copy of the GNU GPL can be obtained at\n"
"**\tbriansmath.ddns.net/Code/amort/Contents.html\n";
string help = "\nEXAMPLE OF USE. Suppose you wish to get a table for the following situation:\n"
"initial debt -- 125000;\n"
"payments each period towards the principal -- 12500.\n"
"interest rate during each period -- 5\%\n\n"
"Then you should enter (order doesn't matter): \n\n"
"debt 125000\nprpy 12500\nintr 5\n\n"
"After the table has been displayed you can change any of your entries by entering\n"
"single lines. For instance\n\n"
"intr 6\n";
string info = "PROGRAM 'AMORT'. Enter 'copy' to see copywrite information. Enter 'help' for an example of use.";
int main () {
float debt = 0;
float amort = 0;
float interestrate = 0;
string buffer;
string keyword;
float value = 0;
cout << info << "\n\n";
bool firsttime = true;
while(true) {
cout << ">> ";
getline(cin, buffer);
istringstream is(buffer);
is >> keyword; is >> value;
if(keyword == "exit")
break;
else if(keyword == "copy") {
cout << theCopywrite << "\n\n" << "Press enter to contiune.";
getline(cin,buffer);
system(CLS);
cout << info << "\n\n";
}
else if(keyword == "help") {
cout << help << "\n\n" << "Press enter to continue.";
getline(cin, buffer);
system(CLS);
cout << info << "\n\n";
}
else if(keyword == "debt") {
if (value <= 0) {
cout << "Debt should be positive." << endl;
if(!firsttime)
SLEEP;
}
else
debt = value;
}
else if (keyword == "prpy") {
if(value <= 0) {
cout << "Payment towards principle should be positive." << endl;
if(!firsttime)
SLEEP;
}
else
amort = value;
}
else if (keyword == "intr")
if(value <= 0) {
cout << "Interest rate should be positive." << endl;
if(!firsttime)
SLEEP;
}
else
interestrate = value;
else {
cout << "Invalid command" << endl;
if(!firsttime)
SLEEP;
}
if(debt > 0 && amort > 0 && interestrate > 0) {
system(CLS);
cout << info << "\n\n";
displaytable(debt, amort, interestrate);
firsttime = false;
}
value = 0;
}
cout << "GOODBYE" << "\n\n";
return 0;
}
static inline void displaytable(float debt, float amort, float interestrate){
//VARIABLER
int period = 1;
float interest;
float due;
float tot_amort = 0;
float tot_paym = 0;
float tot_interest = 0;
//SHOW CURRENT INTEREST RATE.
cout << "INTEREST RATE: " << setprecision(2)
<< fixed << interestrate << "%\n"
<< "---------------------------------------------------------------"
<< endl;
//DISPLAY COLUMN TITLE.
cout << "PERIOD |"
<< left << setw(12) << " DEBT" << '|'
<< left << setw(15) << " PRINC. PAYMENT" << '|'
<< left << setw(12) << " INTEREST " << '|'
<< left << setw(12) << " AMOUNT DUE" << endl;
cout << setprecision(2) << fixed;
//CALCULATE AND DISPLAY TABLE.
do {
if(debt < amort)
amort = debt;
interest = debt * interestrate/100;
due = interest + amort;
tot_amort += amort;
tot_paym += due;
tot_interest += interest;
cout << right << setw(7) << period << '|'
<< right << setw(12) << debt << '|'
<< right << setw(15) << amort << '|'
<< right << setw(12) << interest << '|'
<< right << setw(12) << due << endl;
debt -= amort;
++period;
} while(debt > 0);
//SHOW TOTAL PAYMENTS TOWARDS PRINCIPAL, INTEREST AND ALLTOGETHER.
cout << "---------------------------------------------------------------\n"
<< left << setw(21) << "TOTAL:"
<< right << setw(15) << tot_amort << '|'
<< right << setw(12) << tot_interest << '|'
<< right << setw(12) << tot_paym << endl;
cout << "\n \n";
}