Skip to content

Commit

Permalink
Allow negative balances and account generation with empty balance (#47)
Browse files Browse the repository at this point in the history
Fixes #46
  • Loading branch information
lysogeny committed Nov 3, 2023
1 parent 2bb4f70 commit 1d8dd35
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct status_array_t {
struct credits_t {
char nickname[MAX_CREDIT_NAME_LENGTH + 1];
/* Guthaben (in Cent) */
unsigned int credit;
signed int credit;
};

struct credits_array_t {
Expand Down
3 changes: 2 additions & 1 deletion include/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ char *format_euro(char *s, int maxlen, int32_t cent);
void c128_perror(BYTE, char *);
extern BYTE _oserror;

#define KASSE_AUTHORS "phil_fry, sECuRE, sur5r, mxf"
#define KASSE_AUTHORS "phil_fry, sECuRE, sur5r, mxf, ada"

#define MENU_KEY(key, label) \
do { \
Expand Down Expand Up @@ -79,6 +79,7 @@ extern BYTE _oserror;
/* "-999,99€" */
#define EUR_FORMAT "%3ld,%02lu" EURSYM
#define EUR_FORMAT_MINLEN 8
#define EUR_FORMAT_NEG " -%ld,%02lu" EURSYM

/* because there is no macro expansion when stringifying, we need to use two
* levels of macros to stringify the value of a macro (for example
Expand Down
10 changes: 7 additions & 3 deletions src/credit_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static BYTE current_selection = 0xFF;

static void print_item(BYTE i) {
char buffer[EUR_FORMAT_MINLEN + 1];
const unsigned int credit = credits.credits[i].credit;
const signed int credit = credits.credits[i].credit;

if (format_euro(buffer, sizeof(buffer), credit) != buffer) {
cprintf("Error: Could not format credit %d\r\n", credit);
Expand Down Expand Up @@ -165,7 +165,7 @@ static void deposit_credit_idx(int8_t i) {
if ((deposit = cget_number(0)) == 0)
return;

credit->credit += deposit;
credit->credit += (signed int)deposit;

print_the_buffer();
cprintf("\r\nEinzahlung durchgef" uUML "hrt, dr" uUML "cke RETURN...\r\n");
Expand Down Expand Up @@ -206,8 +206,12 @@ static void new_credit(void) {
}

cprintf("\r\nGuthaben in Cents:\r\n");
if ((credit = cget_number(0)) == 0)
credit = cget_number(0);
if (credit < 0) {
cprintf("\r\nLege mit 0 an und verkaufe Waren oder Freitext\r\n");
cget_return();
return;
}
strncpy(credits.credits[credits.num_items].nickname, name,
MAX_CREDIT_NAME_LENGTH);
credits.credits[credits.num_items].credit = credit;
Expand Down
9 changes: 7 additions & 2 deletions src/general.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,13 @@ char *format_euro(char *s, int maxlen, int32_t cent) {
int32_t euros, cents;
euros = cent / 100;
cents = abs(cent % 100);
if (snprintf(s, maxlen, EUR_FORMAT, euros, cents) > maxlen)
return NULL;
if (cent < 0 && cent > -100) {
if (snprintf(s, maxlen, EUR_FORMAT_NEG, euros, cents) > maxlen)
return NULL;
} else {
if (snprintf(s, maxlen, EUR_FORMAT, euros, cents) > maxlen)
return NULL;
}
return s;
}

Expand Down
8 changes: 5 additions & 3 deletions src/kasse.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,14 @@ static signed int buy(char *name, int32_t price) {
credit->credit);
exit(1);
}
cprintf(
"\r\n%s hat nicht genug Geld (%s). e) einzahlen a) abbruch \r\n",
nickname, rest);
cprintf("\r\n%s hat nicht genug Geld (%s). e) einzahlen a) abbruch t) "
"trotzdem\r\n",
nickname, rest);
c = cgetc();
if (c == 'e') {
deposit_credit(nickname);
} else if (c == 't') {
break;
} else {
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/print_ascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdbool.h>
#include <c128.h>

//#include "vdc_patch_charset.h"
// #include "vdc_patch_charset.h"

int main(void) {
int i = 80;
Expand Down

0 comments on commit 1d8dd35

Please sign in to comment.