**This is a Java Problem, needs to be done with Eclipse!** A store owner keeps a
ID: 3870936 • Letter: #
Question
**This is a Java Problem, needs to be done with 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 MUST MUST MUST include the following within your code, no exceptions!**
import java.io.*;
import java.util.*;
/**
* Code for P11.11. This program takes in a list of baby names and outputs a list of boys and
girls names.
* @author
*/
public class BalanceTransactions
{
public static void main(String[] args)
{
// The following lines ask the user to enter the file name and the balances at the beginning and the end
Scanner in = new Scanner(System.in);
System.out.print("Please enter the file name for input: ");
String filename = in.next();
System.out.print("Please enter the cash amount at the start: ");
double start = in.nextDouble();
System.out.print("Please enter the cash amount at the end: ");
double end = in.nextDouble();
// Your work starts here
}
}
**Sample Text File Below**
10001 56.67 P
10002 23.67 R
10003 99.50 R
10004 10.95 P
Thank you!
Explanation / Answer
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Code for P11.11. This program takes in a list of baby names and outputs a list of boys and
girls names.
* @author
*/
public class BalanceTransactions
{
public static void main(String[] args)
{
// The following lines ask the user to enter the file name and the balances at the beginning and the end
Scanner in =null;
double finalBal=0.0d;
try{
in=new Scanner(System.in);
System.out.print("Please enter the file name for input: ");
String filename = in.next();
System.out.print("Please enter the cash amount at the start: ");
double start = in.nextDouble();
finalBal=start;
System.out.print("Please enter the cash amount at the end: ");
double end = in.nextDouble();
List<String> list=getLinesFromFile(filename);
for (String line : list) {
String[] lineContent=line.split(" ");
if(lineContent[2].equals("P")){
finalBal-=Double.parseDouble(lineContent[1]);
}else if(lineContent[2].equals("R")){
finalBal+=Double.parseDouble(lineContent[1]);
}
}
if(finalBal==end){
System.out.println("actual amount of cash matched with the expected value");
}else{
System.out.println("actual amount of cash doesn't matched with the expected value");
System.out.println("Start amount::"+start);
System.out.println("End amount::"+end);
System.out.println("Final balance::"+finalBal);
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
if(in!=null)
in.close();
}
}
public static List<String> getLinesFromFile(String fileName) throws Exception{
File f =null;
FileReader fileReader=null;
BufferedReader bufferedReader=null;
List<String> list=new ArrayList<String>();
try{
f=new File(fileName);
if(f.exists()){
fileReader = new FileReader(f);
bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
list.add(line);
}
fileReader.close();
}
}catch(Exception ex){
throw ex;
}finally{
if(bufferedReader!=null){
bufferedReader.close();
}
if(fileReader!=null){
fileReader.close();
}
}
return list;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.