Needing help with my homework assignment in Java. A store owner keeps a record o
ID: 3905653 • Letter: N
Question
Needing help with my homework assignment in Java.
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 BalanceTransactions.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 PExplanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
NOTE: Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.
Input file: transactions.txt
========
10001 56.67 P
10002 23.67 R
10003 99.50 R
10004 10.95 P
BalanceTransactions.java
======
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class BalanceTransactions {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String filename;
double initialBalance, endingBalance;
double currentBalance;
Scanner infile = null;
do
{
System.out.print("Enter input filename: ");
filename = keyboard.nextLine();
try {
infile = new Scanner(new File(filename));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}while(infile == null);
System.out.print("Enter initial balance: ");
initialBalance = keyboard.nextDouble();
System.out.print("Enter ending balance: ");
endingBalance = keyboard.nextDouble();
currentBalance = initialBalance;
while(infile.hasNextInt())
{
infile.nextInt(); //read and discard invoice number
double amount = infile.nextDouble();
String type = infile.next();
if(type.equals("P"))
currentBalance = currentBalance - amount;
else if(type.equals("R"))
currentBalance = currentBalance + amount;
}
infile.close();
System.out.printf("Balance according to ledger file transactions is %.2f ", currentBalance);
if(currentBalance != endingBalance)
{
System.out.printf("Ledger balance of %.2f does not match user specified ending balance of %.2f ",
currentBalance, endingBalance);
}
else
{
System.out.printf("Ledger balance matches user specified ending balance of %.2f ", endingBalance);
}
}
}
output
=====
Enter input filename: transactions.txt
Enter initial balance: 200
Enter ending balance: 150
Balance according to ledger file transactions is 255.55
Ledger balance of 255.55 does not match user specified ending balance of 150.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.