Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA: 3. AUDITING TRANSACTIONS Next, we’ll implement a method to calculate and r

ID: 3746330 • Letter: J

Question

JAVA: 3. AUDITING TRANSACTIONS

Next, we’ll implement a method to calculate and return the total account balance based on a collection of provided transaction groups. Here is the required signature for this method (to be implemented within AuditableBanking.java), along with the signature of a corresponding test method (to be implemented within AuditableBankingTests.java).

1

2

3

4

public static int calculateCurrentBalance(int[][] allTransactions, int allTransactionsCount) {

  // TODO: Implement this method

  return -1;

}

1

2

3

4

public static boolean testCalculateCurrentBalance() {

  // TODO: Implement this method

  return false;

}

Another form of auditing that we would like to perform on collections of transaction groups is to count the number of overdrafts. An overdraft is when a withdraw is made that results in a negative balance. To detect overdrafts, you’ll need to process collections of transaction groups in order of increasing indexes starting at zero. Within each group of transactions, you should processes transactions in order of increasing indexes too. Here is the signature of the method that you must implement, along with some test code to add to your AuditableBankingTests class. These tests should help you verify your own understanding of the required method’s behavior, and should also help you begin to test your implementation of it.

1

2

3

4

public static int calculateNumberOfOverdrafts(int[][] allTransactions, int allTransactionsCount) {

  // TODO: Implement this method

  return -1;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

public static boolean testCalculateNumberOfOverdrafts() {

  boolean foundProblem = false;

  int[][] transactions = new int[][] {

    {1,10,-20,+30,-20,-20}, // +2 overdrafts (ending balance:  -20)

    {0,1,1,1,0,0,1,1,1,1},  // +2 overdrafts (ending balance:  -15)

    {1,115},                // +0 overdrafts (ending balance: +100)

    {2,3,1,0,1},            // +1 overdrafts (ending balance: -100)

  };

  

  // test with a single transaction of the Integer Amount encoding

  int transactionCount = 1;    

  int overdrafts = AuditableBanking.calculateNumberOfOverdrafts(transactions,transactionCount);

  if(overdrafts != 2) {

    System.out.println("FAILURE: calculateNumberOfOverdrafts did not return 2 when transactionCount = 1, and transactions contained: "+Arrays.deepToString(transactions));

    foundProblem = true;

  } else

    System.out.println("PASSED TEST 1/2 of TestCalculateNumberOfOverdrafts!!!");

  // test with four transactions: including one of each encoding

  transactionCount = 4;

  overdrafts = AuditableBanking.calculateNumberOfOverdrafts(transactions,transactionCount);

  if(overdrafts != 5) {

    System.out.println("FAILURE: calculateNumberOfOverdrafts did not return 5 when transactionCount = 4, and transactions contained: "+Arrays.deepToString(transactions));

    foundProblem = true;

  } else

    System.out.println("PASSED TESTS 2/2 of TestCalculateNumberOfOverdrafts!!!");

  

  return !foundProblem;

}

After you get these two methods working revisit your definition of the processCommand method. Update this method to calculate and print out the balance when the first non-white-space character within the command string is a B (upper or lower case), and to calculate and print out the number of overdrafts when the first non-white-space character within the command string is an O (upper or lower case). The following methods from the Java8 API may be helpful in updating the processCommand method in this way:

string.charAt(index) – returns the character at the specified zero-based index from within this string.

string.toUpperCase() – returns a new copy of the string this method is called on, but with an upper case version of each alphabetic character.

4. THE DRIVER APPLICATION

The last step of this assignment is to implement a nice text-based interface to access and “drive” the program. Organize this functionality into whatever custom static methods you see fit, but ensure that running the main method within your AuditableBanking class results in an interaction logs comparable to the sample shown below. All of your variables should be local to some static method (no class or instance fields are allowed), unless they are constants marked with the keyword final. Pay close attention to the details within the example log below, to ensure that your program’s welcome message, thank you message, command prompts, current balance, and overdraft numbers are all printed in the same manner. You do not need to worry about erroneous input from the user, because all of our grading tests will focus on properly encoded commands, as described within this specification. The user should be able to submit at least 100 transaction groups. Note that the user’s input below is shown in red, and that the rest of the text below was printed out by the program.

1

2

3

4

public static int calculateCurrentBalance(int[][] allTransactions, int allTransactionsCount) {

  // TODO: Implement this method

  return -1;

}

Explanation / Answer

A] Auditable Banking.java

import java.util.Scanner;
import java.util.StringTokenizer;

public class AuditableBanking {
  
   static int[][] allTransactions;
   static int allTransactionsCount;
  
   public static int calculateCurrentBalance(int[][] allTransactions, int allTransactionsCount) {
       int balance = 0;

        // CALCULATION OF CURRENT BALANCE OCCURS HERE
       for(int i = 0; i<allTransactionsCount; i++) {
           for(int j = 0; j<allTransactions[i].length; j++)  
               balance += allTransactions[i][j];         // Balance being calculated across transaction groups from 1 to allTransactionsCount, by summing up all the entries in every transaction group from index 0 with increasing index
       }
      
       return balance;  
   }
  
   public static int calculateNumberOfOverdrafts(int[][] allTransactions, int allTransactionsCount) {
       int numberOfOverdrafts = 0;
       int balance = 0;
       for(int i = 0; i<allTransactionsCount; i++) {
           for(int j = 0; j<allTransactions[i].length; j++) {      
               balance += allTransactions[i][j];

               // CALCULATION OF NUMBER OF OVERDRAFTS OCCURS HERE
               if(allTransactions[i][j]<0 && balance<0)   // if withdrawal is made and balance turns out to be negative, overdraft occurs
                   numberOfOverdrafts++;
           }
       }
      
       return numberOfOverdrafts;
   }
  
   public static boolean processCommand(String str) {
       char ch = str.charAt(0);
       switch(ch) {
       case 'B':       // calculate current balance
           System.out.println(calculateCurrentBalance(allTransactions, allTransactionsCount));
           break;
          
       case 'O':       // calculate number of overdrafts
           System.out.println(calculateNumberOfOverdrafts(allTransactions, allTransactionsCount));
           break;
          
       case 'Q':       // quit program
           return false;
          
           default:
               StringTokenizer st = new StringTokenizer(str, " ");       // Converts string of transactions to tokens
               int[] arr = new int[st.countTokens()];
               int i = 0;
              
               while(st.hasMoreTokens()) {                               // Loop to convert string of transaction to int array
                   arr[i] = Integer.parseInt(st.nextToken());
                   i++;
               }
              
               allTransactions[allTransactionsCount] = arr;       // add current transaction to allTransactions
               allTransactionsCount++;                              
              
       }
      
       return true;
   }
  
   public static void main(String[] args) {      
       Scanner sc = new Scanner(System.in);
       String inp;  
       boolean flag = true;
       while(flag) {
           inp = sc.nextLine().toUpperCase();
           flag = processCommand(inp);
       }
      
       System.out.println("Thank you for using the app!");
   }
  
}

B] AuditableBankingTests.java

import java.util.Arrays;

public class AuditableBankingTests {
   public static boolean testCalculateCurrentBalance() {
       boolean foundProblem = false;
       int[][] transactions = {
               {1,10,-20,30,-20,-20},
               {0,1,1,1,0,0,1,1,1,1},
               {1,115},
               {2,3,1,0,1}
       };
      
       int transactionCount = 1;
       int balance = AuditableBanking.calculateCurrentBalance(transactions, transactionCount);
      
       if(balance!=-19) {
           System.out.println("FAILURE: calculateCurrentBalance did not return -19 when transactionCount=1 and transactions contained: "+Arrays.deepToString(transactions));
           foundProblem = true;
       }
       else
           System.out.println("PASSED TEST 1/2 of TestCalculateCurrentBalance!!!");
      
       transactionCount = 4;
       balance = AuditableBanking.calculateCurrentBalance(transactions, transactionCount);
      
       if(balance!=111) {
           System.out.println("FAILURE: calculateCurrentBalance did not return 111 when transactionCount=4 and transactions contained: "+Arrays.deepToString(transactions));
           foundProblem = true;
       }
       else
           System.out.println("PASSED TEST 2/2 of TestCalculateCurrentBalance!!!");
      
       return foundProblem;
   }
  
   public static boolean testCalculateNumberOfOverdrafts() {
       boolean foundProblem = false;
       int[][] transactions = {
               {1,10,-20,30,-20,-20},
               {0,1,1,1,0,0,1,1,1,-1},
               {1,115},
               {2,3,1,0,1}
       };
      
       int transactionCount = 1;
       int overdrafts = AuditableBanking.calculateNumberOfOverdrafts(transactions, transactionCount);
      
       if(overdrafts!=2) {
           System.out.println("FAILURE: calculateNumberOfOverdrafts did not return 2 when transactionCount=1 and transactions contained: "+Arrays.deepToString(transactions));
           foundProblem = true;
       }
       else
           System.out.println("PASSED TEST 1/2 of TestCalculateNumberOfOverdrafts!!!");
      
       transactionCount = 4;
       overdrafts = AuditableBanking.calculateNumberOfOverdrafts(transactions, transactionCount);
      
       if(overdrafts!=3) {
           System.out.println("FAILURE: calculateNumberOfOverdrafts did not return 3 when transactionCount=4 and transactions contained: "+Arrays.deepToString(transactions));
           foundProblem = true;
       }
       else
           System.out.println("PASSED TEST 2/2 of TestCalculateCurrentBalance!!!");
      
       return foundProblem;
   }
}

I was unclear as to how exactly the balance and number of overdrafts was being calculated using different transaction groups. So you might find an error only because the calculation of balance and number of overdrafts is slightly different. But this can easily be substituted in the parts in the code which calculate the balance and number of overdrafts, that I have marked, with any logic you see fit.


Hope this addresses your question. Please do upvote :)