Question: 3. AUDITING TRANSACTIONS Next, well implement a method to calculate and return the total account balance based on a collection of provided transaction groups. Here
3. AUDITING TRANSACTIONS
Next, well 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, youll 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 methods 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.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
