From 0751b55f611b889f63dfc2bc2705e7537703cb43 Mon Sep 17 00:00:00 2001 From: gonzo Date: Fri, 19 May 2023 22:17:58 +0200 Subject: added loan struct to abstract over loan types --- Makefile | 10 +++++----- loans.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ loans.h | 19 +++++++++++++++++++ 3 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 loans.c diff --git a/Makefile b/Makefile index d3a30d0..14f15ab 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,14 @@ UNAME := $$( uname -s ) .if ${UNAME} == "Linux" -CC=clang --std=gnu99 +CC=clang --std=gnu11 .else -CC=clang --std=c99 +CC=clang --std=c11 .endif AR=ar ruvcs -CFLAGS=-Wall -Wextra -Werror -Weverything -Wpadded -pedantic -LIBS=-lncurses -lm +CFLAGS=-Wall -Wextra -Werror -Weverything -pedantic +LIBS=-lform -lncurses -lm BLDFLAGS=-g RELFLAGS=-O2 @@ -18,7 +18,7 @@ HEAD= OBJ=${DEPS:.c=.o} LIB=las.a -LDEPS=bullet_loan.c straight_line_loan.c mortgage_loan.c +LDEPS=bullet_loan.c straight_line_loan.c mortgage_loan.c loans.c LHEAD=contract.h loans.h LOBJ=${LDEPS:.c=.o} diff --git a/loans.c b/loans.c new file mode 100644 index 0000000..f812aaf --- /dev/null +++ b/loans.c @@ -0,0 +1,48 @@ +#include "loans.h" +#include "stdio.h" + +loan loan_init(loan_type type, int n, int d, float r, float P) { + loan ret; + ret.type = type; + + switch (type) { + case BULLET: + ret.c = bullet_init(n, d, r, P); + break; + case STRAIGHTLINE: + ret.c = sl_init(n, d, r, P); + break; + case MORTGAGE: + ret.c = mort_init(n, d, r, P); + break; + } + + return ret; +} + +float loan_update(loan l) { + switch (l.type) { + case BULLET: + return bullet_update(l.c); + case STRAIGHTLINE: + return sl_update(l.c); + case MORTGAGE: + return mort_update(l.c); + } + perror("Unkown Loan type supplied!"); + exit(EXIT_FAILURE); +} + +void loan_free(loan l) { + switch (l.type) { + case BULLET: + bullet_free(l.c); + break; + case STRAIGHTLINE: + sl_free(l.c); + break; + case MORTGAGE: + mort_free(l.c); + break; + } +} diff --git a/loans.h b/loans.h index fd276ae..88bbb41 100644 --- a/loans.h +++ b/loans.h @@ -3,6 +3,20 @@ #include #include "contract.h" +typedef enum loan_type { + BULLET, + STRAIGHTLINE, + MORTGAGE, +} loan_type; + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpadded" +typedef struct loan { + loan_type type; + contract *c; +} loan; +#pragma clang diagnostic pop + /**** BULLET LOAN ****/ contract *bullet_init(int n, int d, float r, float P); float bullet_update(contract *); @@ -17,3 +31,8 @@ void sl_free(contract *); contract *mort_init(int n, int d, float r, float P); float mort_update(contract *); void mort_free(contract *); + +/**** GENERAL LOAN STUFF ****/ +loan loan_init(loan_type type, int n, int d, float r, float P); +float loan_update(loan); +void loan_free(loan); -- cgit v1.2.3