Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A store owner keeps a record of daily transactions in a text file. Each line con

ID: 3602886 • Letter: A

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 the letter P if the amount was paid or R if the amount was received. Items on each line are separated by spaces. Write 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 end 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. Call your program Ledger.java. Use a transactions.txt file with the following lines: 10001 56.67 P 10002 23.67 R 10003 99.50 R 10004 10.95 P

Explanation / Answer

Hi,

PFB complete program to achieve this in java:

Ledger.java

import java.io.BufferedReader;

import java.io.FileReader;

import java.util.Scanner;

public class Ledger {

@SuppressWarnings("resource")

public static void main(String[] args) {

// TODO Auto-generated method stub

double cash_start = 0.0;

double cash_end = 0.0;

double total = 0.0;

String ledger = " ";

Scanner sc = new Scanner(System.in);

System.out.println("Welcome to Shop Ledger");

System.out.println("Enter cash at start of day : ");

cash_start = sc.nextDouble();

System.out.println("Enter expected cash at end of day : ");

cash_end = sc.nextDouble();

System.out.println("Enter ledger file name : ");

ledger = sc.next();

total = processFile(ledger+".txt",cash_start);

//System.out.println(total);

if(total == cash_end)

System.out.println("You have achieved expected cash value at end of the day!");

else if(total < cash_end)

System.out.println("You have NOT achieved expected cash value at end of the day!");

else

System.out.println("You exceeded expected cash value at end of the day!");

}

public static double processFile(String file,double starting)

{

String line,c,letter;

String[] tokens;

try

{

@SuppressWarnings("resource")

BufferedReader reader = new BufferedReader(new FileReader(file));

while((line = reader.readLine()) != null)

{

tokens = null;

tokens = line.split(" ");

c = tokens[1];

letter = tokens[2];

if(letter.equals("P"))

{

starting -= Double.parseDouble(c);

}

else if(letter.equals("R"))

{

starting += Double.parseDouble(c);

}

}

}

catch(Exception e)

{

System.err.format("Exception occurred trying to read '%s'.", file);

e.printStackTrace();

}

return starting;

}

}

OUTPUT:

Welcome to Shop Ledger
Enter cash at start of day :
100
Enter expected cash at end of day :
200
Enter ledger file name :
transactions
You have NOT achieved expected cash value at end of the day!

Welcome to Shop Ledger
Enter cash at start of day :
100
Enter expected cash at end of day :
120
Enter ledger file name :
transactions
You exceeded expected cash value at end of the day!

Hope this helps :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote