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

Binary Amount Transactions: When the first number (at index zero) within a trans

ID: 3746751 • Letter: B

Question

Binary Amount Transactions: When the first number (at index zero) within a transaction group array is zero, the rest of the numbers in that array represent transactions encoded as Binary Amount Transactions. Each binary amount transaction is either a zero or a one. Ones represent deposits of $1, and zeros represent withdraws of $1. Any other numbers found in such transaction arrays represent errors, but we will not test how your code responds to such errors, so you can ignore them.

Integer Amount Transactions: When the first number (at index zero) within a transaction group array is one, the rest of the numbers in that array represent transactions encoded as Integer Amount Transactions. Integer amount transactions can be either positive or negative integers. Positive integers represent deposits of the specified integer amount, and negative integers represent withdraws of the specified amount. Any zeros found within such transactions represent errors, but we will not test how your code responds to such errors, so you can ignore them.

Quick Withdraw Transactions: When the first number (at index zero) within a transaction group is two, the rest of the numbers in that array represent transactions encoded as Quick Withdraw Transactions. The non-negative integer numbers within quick withdraw transaction arrays represent the numbers of withdraws, rather than the amounts of withdraws (as was the case with the previous encoding). The amounts for quick withdraws are fixed at $20, $40, $80, and $100. And the number of withdraws of each amount is stored at indexes one, two, three, and four respectively. This means that the size of every quick withdraw array should be five. Arrays of other sizes, and negative withdraw amounts both represent errors, but we will not test how your code responds to such errors, so you can ignore them.

For the purpose of this program, the user will enter each group of transactions as a string of integers separated by spaces, which conforms to one of these three encoding standards. For example, they might enter this string containing a binary amount transaction:

Define a new method called processCommand that takes such a string as command/input, and correctly adds a new transaction group to the provided set of transactions groups. When the first character within a command does not correspond to a valid transaction encoding or the allTransactions array starts out full, this method should not make any changes to allTransactions. Note in the required method signature below, that this method returns an int (as is required to keep track of the potentially growing size of our oversize array):

1

2

3

4

public static int processCommand(String command, int[][] allTransactions, int allTransactionsCount) {

  // TODO: Implement this method

  return -1;

}

The following methods from the Java8 API may be helpful in implementing this processCommand method:

Integer.parseInt(string) – returns an integer encoding of the number represented by the string input.

string.trim() – returns a new copy of the string this method is called on, but without any leading or trailing whitespace.

string.split(” “) – breaks the string this method is called on into parts around each space, and returns all of those parts as a single array of strings.

Since it is currently difficult to tell whether this method is working, we are going to write a test method within AuditableBankingTests to help us verify this. Add a method to this class with the following signature:

1

2

3

4

public static boolean testProcessCommand() {

  // TODO: Implement this method

  return false;

}

This test method should test BankingTests.processCommand by passing in some hard-coded arguments (a command like “0 0 1 1 0 1 1” into an oversize array containing two prior transaction groups) and checking whether the return value and contents of allTransactions match the specified behavior (like allTransactions[2][2] == 1). Try checking for at least two very specific defects within in this test, and keep this test code as simple as possible to avoid the likelihood of confounding bugs within your tests. This method should print out feedback about any defects that it finds. In addition to this feedback for human eyes, this method should return false when any defects are found and true when none are found. Create a main method within your BankingTests class to run this test (and additional tests that you add in the future).

1

2

3

4

public static int processCommand(String command, int[][] allTransactions, int allTransactionsCount) {

  // TODO: Implement this method

  return -1;

}

Explanation / Answer

Hello, I have answered the previous version of this question. So I’m extending the old program to have new functionalities. Please find the code below and let me know if you have any issues. Thanks.

// AuditableBankingTests.java

import java.util.Arrays;

public class AuditableBankingTests {

      /**

      * adds a transaction group to an array of transaction groups

      *

      * @param newTransactions

      *            - is the new transaction group to be added (perfect size)

      * @param allTransactions

      *            -is the collection that newtransaction is being added to

      * @param allTransactionsCount

      *            - count of transaction groups within allTransactions (before

      *            adding newtransactions )

      * @return count of transaction groups within allTransactions (after adding

      *         newtransactions )

      */

      public static int submitTransactions(int[] newTransactions,

                  int[][] allTransactions, int allTransactionsCount) {

            // checking if allTransactions is full or not

            if (allTransactionsCount < allTransactions.length) {

                  // not full, adding to the array

                  allTransactions[allTransactionsCount] = newTransactions;

                  // incrementing count of transaction groups

                  allTransactionsCount++;

            }

            // returning the new allTransactionsCount

            return allTransactionsCount;

      }

      /**

      * method to process a String command containing encoding type and new

      * transaction values, add to the allTransactions array if it is not full

      * and the encoding is valid

      *

      * @Note that this method check two things only, the new transaction is

      *       added to the allTransactions array only if the array is not full

      *       and the encoding is of type 0, 1 or 2. It does not check if the

      *       transaction values are valid

      * @return updated allTransactionsCount after adding new transaction

      */

      public static int processCommand(String command, int[][] allTransactions,

                  int allTransactionsCount) {

            // checking if there is space in allTransactions array

            if (allTransactionsCount < allTransactions.length) {

                  // splitting the String by whitespace

                  String splittedValues[] = command.split(" ");

                  // extracting the encoding type, assuming it is always an integer

                  int encoding = Integer.parseInt(splittedValues[0].trim());

                  // checking if encoding is valid (0, 1 or 2)

                  if (encoding >= 0 && encoding <= 2) {

                        // creating new array

                        int newTransaction[] = new int[splittedValues.length];

                        // adding all elements in the command to the new array after

                        // converting to integer

                        for (int i = 0; i < newTransaction.length; i++) {

                              newTransaction[i] = Integer.parseInt(splittedValues[i]

                                          .trim());

                        }

                        // adding to allTransactions array

                        allTransactions[allTransactionsCount] = newTransaction;

                        allTransactionsCount++;

                  }

            }

            return allTransactionsCount;

      }

      public static boolean testProcessCommand() {

            // defining an over sized integer array

            int allTrans[][] = new int[5][];

            // defining some perfect sized integer arrays

            int trans1[] = { 1, 20, 30, 40, 50 };

            int trans2[] = { 2, 22, 33, 44, 55 };

            // adding these two transactions before testing the processCommand

            // method

            allTrans[0] = trans1;

            allTrans[1] = trans2;

            int count = 2; // initial count

            // now testing the processCommand method

            // adding a valid binary transaction

            count = processCommand("0 0 1 1 0 1 1", allTrans, count);

            if (count != 3) {

                  // test failed

                  return false;

            }

            // adding a valid integer transaction

            count = processCommand("1 55 66 -22 8 9 11", allTrans, count);

            if (count != 4) {

                  // test failed

                  return false;

            }

            // adding a transaction of unknown encoding

            count = processCommand("3 5 2 5 10", allTrans, count);

            if (count != 4) { // there should not be any change in size

                  // test failed

                  return false;

            }

            // adding a valid quick withdrawal transaction

            count = processCommand("2 5 2 5 10", allTrans, count);

            if (count != 5) {

                  // test failed

                  return false;

            }

            // now the array must be full, even adding a valid transaction make no

            // difference

            count = processCommand("0 1 1 1 1 0", allTrans, count);

            if (count != 5) {

                  // test failed

                  return false;

            }

            // all tests passed

            return true;

      }

      public static void main(String[] args) {

            if (testProcessCommand()) {

                  System.out.println("processCommand() is working fine!");

            } else {

                  System.out.println("processCommand() is not working fine!");

            }

      }

}

/*OUTPUT*/

processCommand() is working fine!