Answer:
Answer explained below
Explanation:
Java Program:
package ledger;
import java.util.*;
import java.io.*;
public class Ledger {
//Main method
public static void main(String[] args) throws FileNotFoundException {
String fileName;
try
{
//Reading file name
fileName = getFileName();
//Processing file
processFile(fileName);
}
catch(Exception ex)
{
//Reading file name
fileName = getFileName();
//Processing file
processFile(fileName);
}
}
//Reading file name
public static String getFileName()
{
String fileName;
Scanner sc = new Scanner(System.in);
//Prompting user for file name
System.out.print("\n\n Enter ledger text file name: ");
//Reading and storing file name
fileName = sc.nextLine();
return fileName;
}
//Method that process the file
public static void processFile(String fileName) throws FileNotFoundException
{
//Scanner class object for reading file
Scanner reader = new Scanner(new File(fileName));
double startingBalance, endingBalance, amount, balance;
String transactionType, invoiceNum;
//Scanner class object
Scanner sc = new Scanner(System.in);
//Reading starting balance
System.out.print("\n Enter Starting Balance: ");
startingBalance = sc.nextDouble();
//Reading ending balance
System.out.print("\n Enter Ending Balance: ");
endingBalance = sc.nextDouble();
//Initiall balance is starting balance
balance = startingBalance;
//Reading data
while(reader.hasNext())
{
//Reading invoice number
invoiceNum = reader.next();
//Reading amount
amount = reader.nextDouble();
//Reading transaction type
transactionType = reader.next();
//For Paid transactions
if(transactionType.equalsIgnoreCase("P"))
{
balance = balance - amount;
}
//For Received transactions
else
{
balance = balance + amount;
}
}
System.out.println("\n\n Ledger Balance: " + balance);
//Closing file
reader.close();
//Comparing ending balance
if(balance == endingBalance)
{
System.out.println("\n Actual amount matches with the expected value.... \n");
}
else
{
System.out.println("\n Sorry!!! Actual amount doesn't match with the expected value.... \n");
}
}
}