This blog is under construction

Tuesday 9 July 2013

C program for bank transactions

Write a C program for bank transactions.


  #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>
  #include <time.h>

  struct transaction {
        char time[256];
        int credit, debit, balance;
  };

  /* time calculation to store in database */
  void calcTime(char *str) {
        time_t ts;
        ts = time(NULL);
        strcpy(str, ctime(&ts));
        str[strlen(str) - 1] = '\0';
        return;
  }

  int main() {
        struct transaction obj[100];
        int count = 0, amount, total = 0, i, ch;
        char account[100];

        /* get the account number */
        printf("Enter your Account Number:");
        fgets(account, 100, stdin);
        account[strlen(account) - 1] = '\0';

        /* account number verification */
        if (strcmp(account, "110011") !=0) {
                printf("%s does not exists!!\n", account);
                return 0;
        }

        memset(obj, 0, sizeof(obj));
        while (1) {
                printf("1. Deposit\n2. Withdraw\n3. Account Summary\n4. Exit\n");
                printf("Enter your choice:");
                scanf("%d", &ch);
                switch (ch) {
                        case 1:
                                /* Deposit operation */
                                printf("Enter the amount you want to deposit:");
                                scanf("%d", &amount);
                                obj[count].credit = amount;
                                total = total + amount;
                                obj[count].balance = total;
                                calcTime(obj[count].time);
                                count++;
                                break;

                        case 2:
                                /* Withdraw operation  */
                                printf("Enter the amount you want to withdraw:");
                                scanf("%d", &amount);
                                if (total - amount < 0) {
                                        printf("Underbalance!! Can't withdraw\n");
                                        break;
                                }
                                obj[count].debit = amount;
                                total = total - amount;
                                obj[count].balance = total;
                                calcTime(obj[count].time);
                                count++;

                                break;

                        case 3:
                                /* prints account summary */
                                printf("Transaction Time            Deposit    "
                                                                "WithDraw   Balance\n");
                                for (i = 0; i < count; i++) {
                                        printf("%s %10d%10d%10d\n", obj[i].time,
                                                obj[i].credit, obj[i].debit, obj[i].balance);
                                }
                                break;

                        case 4:
                                exit(0);

                        default:
                                printf("You have entered wrong option!!\n");
                                break;
                }

        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your Account Number:110011
  1. Deposit
  2. Withdraw
  3. Account Summary
  4. Exit
  Enter your choice:1
  Enter the amount you want to deposit:1000
  1. Deposit
  2. Withdraw
  3. Account Summary
  4. Exit
  Enter your choice:1
  Enter the amount you want to deposit:2500
  1. Deposit
  2. Withdraw
  3. Account Summary
  4. Exit
  Enter your choice:1
  Enter the amount you want to deposit:5400 
  1. Deposit
  2. Withdraw
  3. Account Summary
  4. Exit
  Enter your choice:2
  Enter the amount you want to withdraw:2345
  1. Deposit
  2. Withdraw
  3. Account Summary
  4. Exit
  Enter your choice:2
  Enter the amount you want to withdraw:1000
  1. Deposit
  2. Withdraw
  3. Account Summary
  4. Exit
  Enter your choice:3

  Transaction Time            Deposit    WithDraw   Balance
  Tue Jul  9 13:07:26 2013       1000         0      1000
  Tue Jul  9 13:07:31 2013       2500         0      3500
  Tue Jul  9 13:07:37 2013       5400         0      8900
  Tue Jul  9 13:07:40 2013          0      2345      6555
  Tue Jul  9 13:07:43 2013          0      1000      5555

  1. Deposit
  2. Withdraw
  3. Account Summary
  4. Exit
  Enter your choice:4



No comments:

Post a Comment