USING JAVAECLIPSE A store owner keeps a record of daily cash
USING JAVA(ECLIPSE)
A store owner keeps a record of daily cash 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 it was received. Items are separated by spaces. Write a program that prompts the store owner for the amount of cash at the beginning and end of the day, and the name of the file. Your program should check whether the actual amount of cash at the end of the day equals the expected value. You should use Catch exceptions technique when you open the file i.e. (try and catch). Assume that the file contains records of one working day.Solution
Hi, Please find my program.
Please let me know in case of any issue.
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
public class Transaction {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(\"Enter begining amount: \");
double begining = sc.nextDouble();
System.out.print(\"Enter end amount: \");
double end = sc.nextDouble();
System.out.print(\"Enter input file name: \");
String file = sc.next();
try{
// opening input fiel
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
double totalPaid = 0;
double totalReceive = 0;
String line;
// reading each line
while((line = br.readLine()) != null){
// spliting line by space
String[] str = line.split(\"\\\\s+\");
// str[0]: contains invoice number
// str[1] : contains amount
// str[2] : contains P/R
if(\"p\".equalsIgnoreCase(str[2])){
totalPaid = totalPaid + Double.parseDouble(str[1].trim());
}
else if(\"r\".equalsIgnoreCase(str[2]))
totalReceive = totalReceive + Double.parseDouble(str[1].trim());
}
// closing file
br.close();
fr.close();
double totalAvailableAtEnd = begining + totalReceive - totalPaid;
if(totalAvailableAtEnd == end)
System.out.println(\"Transaction is correct\");
else
System.out.println(\"Transcation is not correct\");
}catch (Exception e) {
System.out.println(\"Error reading/opening in input file\");
}
}
}


