Sei sulla pagina 1di 3

11

#include <stdio.h> #define FALSE 0 #define TRUE (!FALSE) #define TYPE_AMOUNT 2 #define ACCOUNT_AMOUNT 5 typedef struct accountType_t { char type; double maxWithdrawPerDay; double minBalance; } AccountType; typedef struct bankAccount_t { char owner[50]; char identifier[10]; double balance; double withdrawPerDay; AccountType* typePtr; } BankAccount; int isPositiveBalance(BankAccount *bnkac) /* This function checks the account's balance: Returns "TRUE" if it's positive (inclding 0), or "FALSE" otherwise. */ { if (bnkac->balance >= 0) return TRUE; return FALSE; } int applyTransaction(BankAccount *bnkac, double amount) /* This function adds "amount" to the account's balance if the following conditions are met: 1.) "amount" is not more than the withdrawl limit. 2.) The added amount does not bring the balance below the minimal threshold. If both conditions are met, the function returns "TRUE" and executes the transaction. Otherwise, it returns "FALSE". */ { if ((bnkac->typePtr->maxWithdrawPerDay) <= (amount + bnkac->withdrawPerDay)) if ((bnkac->balance + amount) >= (bnkac->typePtr->minBalance)) { bnkac->balance += amount; if (amount < 0) bnkac->withdrawPerDay += amount; return TRUE; } return FALSE; }

AccountType * getAccountType(AccountType types[], int numTypes, char type) /* This function goes through all the account types (There are "numTypes" amount of types), and returns a pointer towards the appropriate type. If the type does not exist, this function returns a "NULL" address. */ { int i=0; for (i=0; i < numTypes; i++) if (type == types[i].type) return &types[i]; return NULL; } void printAccount(BankAccount *bnkac) /* This function receives an account's details, and prints them. */ { printf("Account: %s ", bnkac->identifier); printf("Owner: %s ", bnkac->owner); printf("Balance: %g ", bnkac->balance); printf("Type: %c\n\n", bnkac->typePtr->type); return; } int main() //************************************ Main Program ************************************ { AccountType actype[TYPE_AMOUNT]= { {'S', -500, -500}, {'B', -1000, -5000} }; BankAccount bnkac[ACCOUNT_AMOUNT]= { {"David", "1111", 0.0, 0.0, NULL}, {"Rotem", "2222", 0.0, 0.0, NULL}, {"Tomer", "3333", 0.0, 0.0, NULL}, {"Ronit", "4444", 0.0, 0.0, NULL}, {"Guy", "5555", 0.0, 0.0, NULL} }; int i=0; double amount=0.0; //Last two accounts are "business", all else are "standard". //Assignment did not define *HOW* the user hard-codes the different types otherwise... //So hopefully this way of manual assignment is correct enough for the assignment. for (i=0; i < ACCOUNT_AMOUNT; i++) if (i < ACCOUNT_AMOUNT-2) bnkac[i].typePtr= getAccountType(actype, TYPE_AMOUNT, 'S'); else bnkac[i].typePtr= getAccountType(actype, TYPE_AMOUNT, 'B'); //Starting the actual program after the above initialization: printf("The initial status of the accounts are:\n"); for (i=0; i < ACCOUNT_AMOUNT; i++) printAccount(&bnkac[i]); printf("Enter your transaction (Index followed by amount):\n"); scanf("%d%lf", &i, &amount); while ((i >= 0) && (i < ACCOUNT_AMOUNT)) { if (applyTransaction(&bnkac[i], amount) == FALSE) printf("The transaction is invalid!\n"); printf("Enter your transaction (Index followed by amount):\n"); scanf("%d%lf", &i, &amount); }

printf("The negative balances are:\n\n"); for (i=0; i < ACCOUNT_AMOUNT; i++) if (isPositiveBalance(&bnkac[i]) == FALSE) printAccount(&bnkac[i]); return 0; }

Potrebbero piacerti anche