Checking Account Balance in Java Application

The following Java Program shows how to Check Account Balance. This program calculates a customer’s checking account balance at the end of the month. . I will be using the JCreator IDE in developing the program. To start in this tutorial, first open the JCreator IDE, click new and paste the following code.
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class CheckingAccountBalance
  5.  
  6. {
  7. static final double MINIMUM_BALANCE = 1000.00;
  8. static final double SERVICE_CHARGE = 25.00;
  9.  
  10. public static void main (String[] args)
  11. {
  12. int acctNumber;
  13. double beginningBalance;
  14. double accountBalance;
  15.  
  16. double amountDeposited = 0.0;
  17. int numberOfDeposits = 0;
  18.  
  19. double amountWithdrawn = 0.0;
  20. int numberOfWithdrawals = 0;
  21.  
  22. double interestPaid = 0.0;
  23.  
  24. char transactionCode;
  25. double transactionAmount;
  26.  
  27. boolean isServiceCharged = false;
  28.  
  29. Scanner inFile = new Scanner (new FileReader ("money.txt"));
  30.  
  31. PrintWriter outFile = new PrintWriter("money.out");
  32.  
  33. acctNumber = inFile.nextInt();
  34. beginningBalance = inFile.nextDouble();
  35.  
  36. accountBalance = beginningBalance;
  37.  
  38. while (inFile.hasNext())
  39. {
  40. transactionCode = inFile.next().charAt(0);
  41. transactionAmount = inFile.nextDouble();
  42.  
  43. switch (transactionCode)
  44. {
  45. case 'D':
  46. case 'd':
  47.  
  48. accountBalance = accountBalance + transactionAmount;
  49. amountDeposited = amountDeposited + transactionAmount;
  50.  
  51. numberOfDeposits++;
  52. break;
  53.  
  54. case 'I':
  55. case 'i':
  56.  
  57. accountBalance = accountBalance + transactionAmount;
  58. interestPaid = interestPaid + transactionAmount;
  59.  
  60. break;
  61.  
  62. case 'W':
  63. case 'w':
  64.  
  65. accountBalance = accountBalance - transactionAmount;
  66. amountWithdrawn = amountWithdrawn + transactionAmount;
  67.  
  68. numberOfWithdrawals++;
  69.  
  70. if ((accountBalance < MINIMUM_BALANCE) && (!isServiceCharged))
  71. {
  72. accountBalance = accountBalance - SERVICE_CHARGE;
  73. isServiceCharged = true;
  74. }
  75. break;
  76.  
  77. default:
  78. System.out.println("Invalid transaction code: "
  79. + transactionCode + " "
  80. + transactionAmount);
  81.  
  82. }
  83.  
  84. }
  85.  
  86. outFile.printf("Account Number: %d%n", acctNumber);
  87. outFile.printf("Beginning Balance: $%.2f %n", beginningBalance);
  88. outFile.printf("Ending Balance: $%.2f %n", accountBalance);
  89. outFile.println();
  90. outFile.printf("Interest Paid: $%.2f %n", interestPaid);
  91. outFile.printf("Amount Deposited: $%.2f %n", amountDeposited);
  92. outFile.printf("Number of Deposits: $%.2f %n", numberOfDeposits);
  93. outFile.println();
  94. outFile.printf("Amount Withdraw: $%.2f %n", amountWithdrawn);
  95. outFile.printf("Number of Withdrawals: %d%n", numberOfWithdrawals);
  96.  
  97. outFile.println();
  98.  
  99. if (isServiceCharged)
  100. outFile.printf("Service Charge: $%.2f %n", SERVICE_CHARGE);
  101.  
  102. outFile.close();
  103.  
  104.  
  105.  
  106.  
  107. }
  108. }
Sample Run: The algorithm of the program is: The first line of data shows the account number, followed by the account balance at the beginning of the month. Thereafter, each line has two entries: the transaction code and the transaction amount. The transaction code ‘W’ or ‘w’ means withdrawal, ‘D’ or ‘d’ means deposit, and ‘I’ or ‘I’ means interests paid by the bank. The program updates the balance after each transaction. If at any time during the month the balance goes $1000.00, a $25.00 service fee is charged for the month. The program prints the following information: account number, balance at the beginning of the month, balance at the end of the month, interest paid by the bank, total amount of deposit, number of deposits, total amount of withdrawal, number of withdrawals, and service charge if any.

Comments

Add new comment