summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgonzo <gonzo@toniatuh.com>2023-06-10 00:22:33 +0200
committergonzo <gonzo@toniatuh.com>2023-06-10 00:22:33 +0200
commitb2ab7c3665c0a9eee6e8f208f1974253ce37eec9 (patch)
treec2c62aa5577ddd610e2c61eb265f08cf6622e18d
parentf643587923125479281e4ba23d9bef01190b1bb3 (diff)
downloadloan_calc-b2ab7c3665c0a9eee6e8f208f1974253ce37eec9.tar.gz
user may now select loan_type
-rw-r--r--loans.h10
-rw-r--r--main.c15
-rw-r--r--read_functions.c12
3 files changed, 28 insertions, 9 deletions
diff --git a/loans.h b/loans.h
index aed283e..4e3a3df 100644
--- a/loans.h
+++ b/loans.h
@@ -1,12 +1,13 @@
#pragma once
#include <stdlib.h>
#include <math.h>
+#include <string.h>
#include "contract.h"
typedef enum loan_type {
- BULLET,
- STRAIGHTLINE,
- MORTGAGE,
+ BULLET = 0,
+ STRAIGHTLINE = 1,
+ MORTGAGE = 2,
} loan_type;
#pragma clang diagnostic push
@@ -36,3 +37,6 @@ void mort_free(contract *);
loan loan_init(loan_type type, int n, int d, double r, double P);
double loan_update(loan);
void loan_free(loan);
+
+/**** read loan type ****/
+loan_type read_loan_type(char **contract_subtypes, char *lt);
diff --git a/main.c b/main.c
index 992225c..5a73850 100644
--- a/main.c
+++ b/main.c
@@ -10,7 +10,7 @@ static WINDOW *pad;
_Noreturn static void finish(int sig);
void display_custom_form(FORM *);
void print_contract(WINDOW *w, int x, int y, contract *c);
-void run_sim(FORM *);
+void run_sim(FORM *, char **contract_subtypes);
int main() {
int ch, i;
@@ -29,7 +29,7 @@ int main() {
FIELD *fields[8];
FORM *form;
char *contract_types[] = { "loan", "savings", NULL };
- char *contract_subtypes[] = { "bullet", "sla", "mort", NULL };
+ char *contract_subtypes[] = { "bullet", "sla", "mortgage", NULL };
/* initialize fields */
fields[0] = new_field(1, 11, 4, 22, 0, 0); /* contract type */
@@ -91,7 +91,7 @@ int main() {
formactive = FALSE;
curs_set(0);
box(stdscr, 0, 0);
- run_sim(form);
+ run_sim(form, contract_subtypes);
padscroll = 0;
refresh();
prefresh(pad, padscroll, 0, 4, 1, HEIGHT-2, WIDTH-1);
@@ -159,12 +159,14 @@ void print_contract(WINDOW* w, int x, int y, contract *c) {
(double)c->D, (double)c->A);
}
-void run_sim(FORM *form) {
+void run_sim(FORM *form, char **contract_subtypes) {
/* loan_type type; */
int n, d, yp;
double r, p;
+ loan_type lt;
FIELD **fields = form_fields(form);
+ lt = read_loan_type(contract_subtypes, field_buffer(fields[1], 0));
n = atoi(field_buffer(fields[2], 0));
yp = atoi(field_buffer(fields[3], 0));
d = atoi(field_buffer(fields[4], 0));
@@ -180,7 +182,7 @@ void run_sim(FORM *form) {
/* setup pad */
pad = newpad(n+1, WIDTH-2);
- mvwprintw(stdscr, 1, 2, "Reinbursement Table");
+ mvwprintw(stdscr, 1, 2, "Reinbursement Table for %s", contract_subtypes[lt]);
mvwprintw(stdscr, 3, 3,
"%4s/%-4s | %12s | %10s | %10s | %10s\n",
"cur", "tot",
@@ -190,7 +192,8 @@ void run_sim(FORM *form) {
"annuity");
/* run simulation */
- loan l = loan_init(MORTGAGE, n, d, r, p);
+ printf("%d\n", (int)lt);
+ loan l = loan_init(lt, n, d, r, p);
for (int i = 0; i <= n; i++) {
print_contract(pad, i, 2, l.c);
loan_update(l);
diff --git a/read_functions.c b/read_functions.c
new file mode 100644
index 0000000..d60b19e
--- /dev/null
+++ b/read_functions.c
@@ -0,0 +1,12 @@
+#include "loans.h"
+
+loan_type read_loan_type(char **contract_subtypes, char *lt) {
+ int i = 0;
+ for (i = 0; lt[i]; i++)
+ if (lt[i] == ' ') lt[i] = '\0';
+
+ for (i = 0; contract_subtypes[i]; i++)
+ if (!strcmp(contract_subtypes[i], lt))
+ return (loan_type)i;
+ return MORTGAGE;
+}