A Tip for Calculating Account Balance

Question

The following transaction file is a CSV file that contains financial transactions, where each line is either a debit or a credit transaction on a specific account number.

 

00001500205568600,D,1520.15

00001500205568600,D,12500.00

00001500205568600,C,44.00

00001300220978215,C,59800.13

00001300220978215,C,80000.00

00001300220978215,C,15850.23

00001300220978215,D,85.60

00002200540006410,D,595550.03

00002200540006410,C,1200.00

00002200540006410,D,3250.00

00001300220978215,C,12.55

00009650025500020,C,290050.00

00009650025500020,D,96.00

 

Each line has the following format: <account number>,<D or C (D for debit and C for credit)>,<amount>. The file contains transactions for multiple account numbers, but more than one for each account number. I need to write a Java program that reads the transactions.txt file line by line and calculate the final balance of each account number. Assume that each account starts with 0 balance; and then the amount of each debit (D) transaction is subtracted from the balance and the amount of each credit (C) transaction is added to the balance. The program should print each account number with its final balance.

I have this code and I need help in making transaction and print them.

 

public class Test1 {

 

  public static void main(String[] args) {

 

      test1 obj = new test1();

    obj.run();

 

  }

 

  public void run() {

 

    String csvFile = "transactions.txt";

    BufferedReader br = null;

    String line = "";

    String cvsSplitBy = ",";

 

 

 

    try {

 

        br = new BufferedReader(new FileReader(csvFile));

        while ((line = br.readLine()) != null) {

 

                // use comma as separator

            String[] country = line.split(cvsSplitBy);

 

 

            System.out.println("Account Number " + country[0]

                                 + ", Account Balance=" + country[2] + "]");

 

        }

 

    } catch (FileNotFoundException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        if (br != null) {

            try {

                br.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

 

    System.out.println("Done");

  }

 

}

 

Answer

When value of the 2nd column is D, change the value in the 3rd column into the negative, then group data and perform sums. Without further computing target, we can handle it in SPL (Structured Process Language). The code is simple and easy to understand:

A

1

=file("d:\\source.csv").import@c()

2

=A1.run(if(#2=="D",#3=-#3))

3

=A2.groups(#1;sum(#3))

A1: Import source.csv.

 undefined

A2: For D values in the 2nd column, change the corresponding values in the 3rd column to a negative.

 undefined

A3: Group the 1st column and the perform sum over the 3rd column.

 undefined

An SPL script is integration-friendly. See How to Call an SPL Script in Java to learn how to invoke an SPL script from a Java application.