Lab 8 Source Code

Either cut-and-paste this text for use in your lab, or

accounts.c

#include <stdio.h>
/* Header */
 
#define MAX_ACCTS 10
int accounts[MAX_ACCTS] = {20181, 20467, 20813, 21354, 21358, 21359, 22027, 22041};
double balances[MAX_ACCTS] = {0, 145.72, 10133.00, -17.44, 612.98, 1415.30, 313.21, 3131.32};

int findAccount(int accountID) {
        int index = -1;
        /* Use a linear search to find the accountID in the accounts array. */
        /* If found, return the index. */
        index = 1; /* This line is just for testing - remove it */
        /* If not found, return -1 */
        return(index);
}
 
void printAccountBal(int accountIndex) {
        /* Write this function. Use the account index
         * to find and print the account balance.
         * Use good human factor design in your output.
         * Use correct format for currency. */
}
 
void deposit(int accountIndex) {
        /* Write this function. Use the account index
         * to access the account balance.
         * Prompt for a deposit amount. The deposit amount
         * should be greater than 0.
         * Update the balance in the array. */
}
 
void withdraw(int accountIndex) {
        /* Write this function. Use the account index
         * to access the account balance.
         * Prompt for a withdrawal amount.
         * The withdrawal amount should be greater than 0.
         * Make sure the withdrawal amount is not more than the balance.
         * If the amount exceeds the balance,
         * provide an error message and ask for a new withdrawal amount.
         * Update the balance in the array. */
}
 
int main(void) {
        int choice;
        int accountID, accountIndex;
        int accountIDgood;
 
        printf("Bank account simulator\n");
 
        do {
                accountIDgood = 0;      /* False */
                do {
                        printf("\nEnter account ID: ");
                scanf("%d", &accountID);
                if (accountID < 20000 || accountID > 29999) {
                        printf("Account ID is not correct format.\n");
                } else {
                        accountIndex = findAccount(accountID);
                        if (accountIndex == -1) {
                                printf("Account ID %d is not valid.\n", accountID);
                        } else {
                                accountIDgood = 1;      /* True */
                        }
                }
                } while (!accountIDgood);
 
                printAccountBal(accountIndex);
 
                do {
                printf("Choose from the following menu:\n");
                printf("\t0 - Exit the program\n");
                printf("\t1 - Change accounts\n");
                printf("\t2 - Make deposit\n");
                printf("\t3 - Make withdrawal\n");
                printf("Enter your choice: ");
                scanf("%d", &choice);
                printf("\n");
                
                switch (choice) {
                        case 2:
                                deposit(accountIndex);
                                printAccountBal(accountIndex);
                                break;
                        case 3:
                                withdraw(accountIndex);
                                printAccountBal(accountIndex);
                                break;
                        default:
                                if (choice != 0 && choice != 1) {
                                        printf("Your choice (%d) is not valid.\n", choice);
                                }
                                break;
                }
                } while (choice != 0 && choice != 1);
        } while (choice != 0);
 
        return(0);
}