IM TRYING TO UNDERSTAND THIS, COMMENTS ARE GREATLY APPRECIATED THANK YOU VERY MU
ID: 3848580 • Letter: I
Question
IM TRYING TO UNDERSTAND THIS, COMMENTS ARE GREATLY APPRECIATED THANK YOU VERY MUCH 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. Follow all directions in the COP3337 Class Rules for Submitting Programs. Please use the file > export > to zip option in NetBeans. The zip file should be named FirstNameLastNameA5.zip.Explanation / Answer
Java Code:
Instructions:
1) Create Coda.java file and paste given code in it
2) Change the path of the input file in the code manually according to where your input file is located!
Code.java
package code;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class Code {
public static void main(String[] args)
{
try{
File file = new File("C:\Users\Akash\Documents\NetBeansProjects\code\src\code\input.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
System.out.println("Enter balance at the start of the day!");
Scanner scanner = new Scanner(System.in);
int balance;
balance = scanner.nextInt();
System.out.println("Enter balance at the end of the day!");
int endbalance;
endbalance = scanner.nextInt();
while(true)
{
String line;
line = bufferedReader.readLine();
if(line == null)
{
break;
}
String[] parts = line.split(" ");
String ss = parts[1];
String vv = parts[2];
if(vv.equals("R"))
{
balance = balance + Integer.parseInt(ss);
}
else
{
balance = balance - Integer.parseInt(ss);
}
}
System.out.println("Actuall balance According to input file = " + balance );
if(balance != endbalance)
{
System.out.println("Actuall balance According to input file = " + balance + " is not matching with balance at day end = " + endbalance );
}
else
{
System.out.println("Actuall balance According to input file = " + balance + " is matching with balance at day end = " + endbalance);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
input.txt
1 500 R
2 1000 R
3 1500 R
4 1000 P
5 1000 P
Sample Output:
Enter balance at the start of the day!
2000
Enter balance at the end of the day!
3000
Actuall balance According to input file = 3000
Actuall balance According to input file = 3000 is matching with balance at day end = 3000