diff options
| author | gonzo <gonzo@toniatuh.com> | 2023-06-10 14:05:05 +0200 |
|---|---|---|
| committer | gonzo <gonzo@toniatuh.com> | 2023-06-10 14:05:05 +0200 |
| commit | 58c143df0cd45ca85c30ef928202b15785a3bef4 (patch) | |
| tree | 008edbd0437a0c1fdd4e248850b1feece2960426 | |
| parent | 2b2490fb438ba2a68929a3d3bea9817b3de36633 (diff) | |
| download | loan_calc-58c143df0cd45ca85c30ef928202b15785a3bef4.tar.gz | |
check that values exist before running sim
| -rw-r--r-- | main.c | 31 |
1 files changed, 17 insertions, 14 deletions
@@ -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 *, char **contract_subtypes); +int run_sim(FORM *, char **contract_subtypes); int main() { int ch, i; @@ -43,7 +43,7 @@ int main() { fields[4] = new_field(1, 11, 8, 22, 0, 0); /* delay duration */ set_field_type(fields[4], TYPE_INTEGER, 1, 0, INT_MAX); fields[5] = new_field(1, 11, 9, 22, 0, 0); /* starting amount */ - set_field_type(fields[5], TYPE_NUMERIC, 2, 0.0, 0.0); + set_field_type(fields[5], TYPE_NUMERIC, 2, 0.01, 0.0); fields[6] = new_field(1, 11, 10, 22, 0, 0); /* rate */ set_field_type(fields[6], TYPE_NUMERIC, 3, 0.0, 1.0); fields[7] = NULL; @@ -86,13 +86,21 @@ int main() { form_driver(form, REQ_DEL_PREV); break; case '\r': - if (E_OK == form_driver(form, REQ_VALIDATION)) { + if ((E_OK == form_driver(form, REQ_VALIDATION)) + && run_sim(form, contract_subtypes)) { unpost_form(form); formactive = FALSE; curs_set(0); box(stdscr, 0, 0); - run_sim(form, contract_subtypes); padscroll = 0; + mvwprintw(stdscr, 1, 2, "Reinbursement Table"); + mvwprintw(stdscr, 3, 3, + "%4s/%-4s | %12s | %10s | %10s | %10s\n", + "cur", "tot", + "principal", + "interest", + "part", + "annuity"); refresh(); prefresh(pad, padscroll, 0, 4, 1, HEIGHT-2, WIDTH-1); } @@ -159,7 +167,7 @@ void print_contract(WINDOW* w, int x, int y, contract *c) { (double)c->D, (double)c->A); } -void run_sim(FORM *form, char **contract_subtypes) { +int run_sim(FORM *form, char **contract_subtypes) { /* loan_type type; */ int n, d, yp; double r, p; @@ -173,6 +181,8 @@ void run_sim(FORM *form, char **contract_subtypes) { p = atof(field_buffer(fields[5], 0)); r = atof(field_buffer(fields[6], 0)); + if (!n || !yp || (p <= 0.0)) return 0; + //adapt wrt yearly payments if (yp > 1) { n = n*yp; @@ -182,21 +192,14 @@ void run_sim(FORM *form, char **contract_subtypes) { /* setup pad */ pad = newpad(n+1, WIDTH-2); - mvwprintw(stdscr, 1, 2, "Reinbursement Table for %s", contract_subtypes[lt]); - mvwprintw(stdscr, 3, 3, - "%4s/%-4s | %12s | %10s | %10s | %10s\n", - "cur", "tot", - "principal", - "interest", - "part", - "annuity"); /* run simulation */ - 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); } duration = n - HEIGHT + 6; + + return 1; } |
