From 3267a8d6f1c7226715c6697ddf5bcb3b6c4be7d0 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sat, 29 Sep 2012 17:10:48 +0200 Subject: [PATCH] implement depositing when the user cannot purchase an item --- include/credit_manager.h | 1 + src/credit_manager.c | 20 +++++++++++--------- src/kasse.c | 12 ++++++++---- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/include/credit_manager.h b/include/credit_manager.h index 60eea81..c5dfb99 100644 --- a/include/credit_manager.h +++ b/include/credit_manager.h @@ -1,5 +1,6 @@ #ifndef CREDIT_MANAGER_H_ #define CREDIT_MANAGER_H_ struct credits_t *find_credit(char *name); +void deposit_credit(); void credit_manager(); #endif diff --git a/src/credit_manager.c b/src/credit_manager.c index ac6fb0a..277df54 100644 --- a/src/credit_manager.c +++ b/src/credit_manager.c @@ -52,20 +52,22 @@ struct credits_t *find_credit(char *name){ } /* - * when depositing money with this and returning to the main menu, the program - * will crash with a message like the following: + * Deposits credit for a user. Called in the credit manager (with input == + * NULL) or interactively when the user does not have enough money for his + * intended purchase (with input == nickname). * */ -static void deposit_credit() { +void deposit_credit(char *input) { char *time = get_time(); - char *input; struct credits_t *credit; unsigned int deposit; - cprintf("\r\nName:\r\n"); - if ((input = get_input()) == NULL || *input == '\0') - return; // no name given - + if (input == NULL) { + cprintf("\r\nName:\r\n"); + if ((input = get_input()) == NULL || *input == '\0') + return; // no name given + } + if ((credit = find_credit(input)) == NULL) return; // cannot find named credit @@ -163,7 +165,7 @@ void credit_manager(){ current_credits_page--; break; case 'p': - deposit_credit(); break; + deposit_credit(NULL); break; case 'g': cprintf("Filter eingeben:\r\n"); filter = get_input(); diff --git a/src/kasse.c b/src/kasse.c index 5c0ddb9..40bfbf5 100644 --- a/src/kasse.c +++ b/src/kasse.c @@ -151,10 +151,14 @@ static signed int buy(char *name, unsigned int price) { * to NULL if no such credit could be found */ credit = find_credit(nickname); if (credit != NULL) { - if ((signed int)credit->credit < ((signed int)price * einheiten)) { - cprintf("Sorry, %s hat nicht genug Geld :-(\r\n", nickname); - get_input(); - return 0; + while ((signed int)credit->credit < ((signed int)price * einheiten)) { + cprintf("%s hat nicht genug Geld. e) einzahlen a) abbruch \r\n", nickname); + c = cgetc(); + if (c == 'e') { + deposit_credit(nickname); + } else { + return 0; + } } /* substract money */ credit->credit -= (price * einheiten); -- 2.39.2