Ask Question
25 May, 09:15

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

+2
Answers (1)
  1. 25 May, 10:40
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers