writing junit tests for my classes
I would like to write junit tests for my java classes(FileData and ReadFile).

The objective is to read contents from a text file and doing some basic operations and calculations.

code of my classes are as below :(pasting the whole code of both the classes as it would give some idea in helping me out)


    import java.io.IOException;
    public class FileData {
    
        public static void main(String[] args)throws IOException{
            // provide the path of the text file
            String file_name = "C:\\Users\\Public\\Bhrama\\atmTest.txt";
            String atmMoney;
            String accountNumber;
            String atmPin;
            String enteredPin;
            String requestedAmount;
            String availableAmount;
            String overDraft;
            int atmPinInt;
            int enteredPinInt;
            int availableAmountInt;
            int overDraftInt;
            String decidingChar;
            int requestedAmountInt;
            int balanceAmount;
    
            try{
    
                ReadFile file = new ReadFile(file_name);
                String[] aryLines = file.openFile();
    
                int i;
                // displaying the contents of the text file
                for (i=0; i<aryLines.length; i++) {
                    System.out.println(aryLines[i]);
    
                }
                System.out.println("-------------------------");
                int numberOfLines = file.readLines();
                int j;
                for (j=0; j<numberOfLines; j++) {
    
                    // for the first line of the text file - atm money
                    if (j == 0) {
                        atmMoney = aryLines[j];
                        atmPinInt = Integer.parseInt(atmMoney);
                        if (atmPinInt <= 0 ){
                            System.out.println("ATM_ERR");
                        }
                    }
                    // for empty lines
                    if (aryLines[j].length() == 0) {
                        //not doing anything
                    }
                    // for the account number, atm pin and entered pin lines handling (hard coded though its not good practice)
                    if (aryLines[j].length() == 18) {
    
                        int a = aryLines[j].indexOf(" ");
                        accountNumber = aryLines[j].substring(0, a);
                        int b = aryLines[j].indexOf(" ", a);
                        int c = aryLines[j].indexOf(" ", b+1);
                        atmPin = aryLines[j].substring(b+1, c);
                        enteredPin = aryLines[j].substring(c+1);
                        atmPinInt = Integer.parseInt(atmPin);
                        enteredPinInt = Integer.parseInt(enteredPin);
                        if (atmPinInt != enteredPinInt){
                            System.out.println("ACCOUNT_ERR");
                        }
                        //System.out.println("correct pin entered");
                        int d = aryLines[j+1].indexOf(" ");
                        availableAmount = aryLines[j+1].substring(0, d);
                        availableAmountInt = Integer.parseInt(availableAmount);
    
                        int e = aryLines[j+1].indexOf(" ", d);
                        overDraft = aryLines[j+1].substring(e+1);
                        overDraftInt = Integer.parseInt(overDraft);
    
                        decidingChar = aryLines[j+2].substring(0, 1);
    
    
                        // logic for Balance enquiry or Withdrawal
                        if (decidingChar.equalsIgnoreCase("B")){
                            file.availableAmount(availableAmountInt);
                        }
                        if (decidingChar.equalsIgnoreCase("W")){
                            file.balanceAmount(aryLines,j+2,availableAmountInt);
                        }
                        // 4th line of the user details(if exists)
                        if (aryLines[j+3].length() != 0){
                            decidingChar = aryLines[j+3].substring(0, 1);
    
                             if (decidingChar.equalsIgnoreCase("B")){
                                  file.availableAmount(availableAmountInt);
                             }
    
                            if (decidingChar.equalsIgnoreCase("W")){
                                file.balanceAmount(aryLines,j+3,availableAmountInt);
                            }
    
                        }
    
    
                    }
                }
    
                
            }
            catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }


    import java.io.IOException;
    import java.io.FileReader;
    import java.io.BufferedReader;
    
    public class ReadFile {
    
        private String path;
        // text file is assigned to path
        public ReadFile(String file_path) {
            path = file_path;
        }
    
        public String[] openFile() throws IOException {
    
            FileReader fr = new FileReader(path);
            BufferedReader textReader = new BufferedReader(fr);
            // to check the number of lines of the text file
            int numberOfLines = readLines();
    
            String[ ] textData = new String[numberOfLines];
            int i;
            // reading the text file line by line
            for (i=0; i < numberOfLines; i++) {
            textData[ i ] = textReader.readLine();
    
        }
            textReader.close( );
            return textData;
        }
    
        int readLines() throws IOException {
    
            FileReader file_to_read = new FileReader(path);
            BufferedReader bf = new BufferedReader(file_to_read);
    
            String aLine;
            int numberOfLines = 0;
    
            while ((aLine = bf.readLine()) != null ){
                numberOfLines++;
            }
            bf.close();
    
            return numberOfLines;
        }
    
        void availableAmount(int availableAmount){
            System.out.println(availableAmount);
    
        }
    
        void balanceAmount(String[] arrayLines, int i, int availableAmount){
            int balanceAmount;
            // using the requested amount, calculate the balance amount
            int f = arrayLines[i].indexOf(" ");
            String requestedAmount = arrayLines[i].substring(f+1);
            int requestedAmountInt = Integer.parseInt(requestedAmount);
            balanceAmount = availableAmount-requestedAmountInt;
            if (balanceAmount < 0){
                System.out.println("FUNDS_ERR");
            }
            else{
            System.out.println(balanceAmount);
            }
    
        }
    }


writing junit tests for my classes
===

I would like to write junit tests for my java classes(FileData and ReadFile).

The objective is to read contents from a text file and doing some basic operations and calculations.

code of my classes are as below :(pasting the whole code of both the classes as it would give some idea in helping me out)


    import java.io.IOException;
    public class FileData {
    
        public static void main(String[] args)throws IOException{
            // provide the path of the text file
            String file_name = "C:\\Users\\Public\\Bhrama\\atmTest.txt";
            String atmMoney;
            String accountNumber;
            String atmPin;
            String enteredPin;
            String requestedAmount;
            String availableAmount;
            String overDraft;
            int atmPinInt;
            int enteredPinInt;
            int availableAmountInt;
            int overDraftInt;
            String decidingChar;
            int requestedAmountInt;
            int balanceAmount;
    
            try{
    
                ReadFile file = new ReadFile(file_name);
                String[] aryLines = file.openFile();
    
                int i;
                // displaying the contents of the text file
                for (i=0; i<aryLines.length; i++) {
                    System.out.println(aryLines[i]);
    
                }
                System.out.println("-------------------------");
                int numberOfLines = file.readLines();
                int j;
                for (j=0; j<numberOfLines; j++) {
    
                    // for the first line of the text file - atm money
                    if (j == 0) {
                        atmMoney = aryLines[j];
                        atmPinInt = Integer.parseInt(atmMoney);
                        if (atmPinInt <= 0 ){
                            System.out.println("ATM_ERR");
                        }
                    }
                    // for empty lines
                    if (aryLines[j].length() == 0) {
                        //not doing anything
                    }
                    // for the account number, atm pin and entered pin lines handling (hard coded though its not good practice)
                    if (aryLines[j].length() == 18) {
    
                        int a = aryLines[j].indexOf(" ");
                        accountNumber = aryLines[j].substring(0, a);
                        int b = aryLines[j].indexOf(" ", a);
                        int c = aryLines[j].indexOf(" ", b+1);
                        atmPin = aryLines[j].substring(b+1, c);
                        enteredPin = aryLines[j].substring(c+1);
                        atmPinInt = Integer.parseInt(atmPin);
                        enteredPinInt = Integer.parseInt(enteredPin);
                        if (atmPinInt != enteredPinInt){
                            System.out.println("ACCOUNT_ERR");
                        }
                        //System.out.println("correct pin entered");
                        int d = aryLines[j+1].indexOf(" ");
                        availableAmount = aryLines[j+1].substring(0, d);
                        availableAmountInt = Integer.parseInt(availableAmount);
    
                        int e = aryLines[j+1].indexOf(" ", d);
                        overDraft = aryLines[j+1].substring(e+1);
                        overDraftInt = Integer.parseInt(overDraft);
    
                        decidingChar = aryLines[j+2].substring(0, 1);
    
    
                        // logic for Balance enquiry or Withdrawal
                        if (decidingChar.equalsIgnoreCase("B")){
                            file.availableAmount(availableAmountInt);
                        }
                        if (decidingChar.equalsIgnoreCase("W")){
                            file.balanceAmount(aryLines,j+2,availableAmountInt);
                        }
                        // 4th line of the user details(if exists)
                        if (aryLines[j+3].length() != 0){
                            decidingChar = aryLines[j+3].substring(0, 1);
    
                             if (decidingChar.equalsIgnoreCase("B")){
                                  file.availableAmount(availableAmountInt);
                             }
    
                            if (decidingChar.equalsIgnoreCase("W")){
                                file.balanceAmount(aryLines,j+3,availableAmountInt);
                            }
    
                        }
    
    
                    }
                }
    
                
            }
            catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }


    import java.io.IOException;
    import java.io.FileReader;
    import java.io.BufferedReader;
    
    public class ReadFile {
    
        private String path;
        // text file is assigned to path
        public ReadFile(String file_path) {
            path = file_path;
        }
    
        public String[] openFile() throws IOException {
    
            FileReader fr = new FileReader(path);
            BufferedReader textReader = new BufferedReader(fr);
            // to check the number of lines of the text file
            int numberOfLines = readLines();
    
            String[ ] textData = new String[numberOfLines];
            int i;
            // reading the text file line by line
            for (i=0; i < numberOfLines; i++) {
            textData[ i ] = textReader.readLine();
    
        }
            textReader.close( );
            return textData;
        }
    
        int readLines() throws IOException {
    
            FileReader file_to_read = new FileReader(path);
            BufferedReader bf = new BufferedReader(file_to_read);
    
            String aLine;
            int numberOfLines = 0;
    
            while ((aLine = bf.readLine()) != null ){
                numberOfLines++;
            }
            bf.close();
    
            return numberOfLines;
        }
    
        void availableAmount(int availableAmount){
            System.out.println(availableAmount);
    
        }
    
        void balanceAmount(String[] arrayLines, int i, int availableAmount){
            int balanceAmount;
            // using the requested amount, calculate the balance amount
            int f = arrayLines[i].indexOf(" ");
            String requestedAmount = arrayLines[i].substring(f+1);
            int requestedAmountInt = Integer.parseInt(requestedAmount);
            balanceAmount = availableAmount-requestedAmountInt;
            if (balanceAmount < 0){
                System.out.println("FUNDS_ERR");
            }
            else{
            System.out.println(balanceAmount);
            }
    
        }
    }

