Ask Question
10 April, 10:28

A store owner keeps a record of daily transactions in a text file. Each line contains three items: The invoice number, the cash amount, and the letter P if the amount was paid or R if the amount was received. Items on each line are separated by spaces. Wri te a program that prompts the store owner for the amount of cash at the beginning of the day and for the amount of cash at the end of the day, as well as the name of the ledger text file. Your program should calculate if the actual amount of cash at the en d of the day equals the expected value. If an error is found while reading the text file, the user should be allowed to choose another file

+4
Answers (1)
  1. 10 April, 12:03
    0
    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");

    }

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “A store owner keeps a record of daily transactions in a text file. Each line contains three items: The invoice number, the cash amount, and ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers