Java
The numbers in the Deposits.txt file are the amounts of deposits that were made to a savings account during the month, and the numbers in the Withdrawals.txt file are the amounts of withdrawals that were made during the month. Write a program that creates an instance of the SavingsAccount class . The starting balance for the object is 500.00. The annual interest rate should be 4.75%. The program should read the values from the Deposits.txt file and use the object’s method to add them to the account balance. The program should read the values from the Withdrawals.txt file and use the object’s method to subtract them from the account balance. The program should call the class method to calculate the monthly interest, and then display the ending balance and the total interest earned.
You can download and test your program with the input files provided: Deposits.txt and Withdrawals.txt.
You should submit two class files: Main.java (the main program) and SavingsAccount.java.
View required output
Test Case 1
Files in the same directory |
---|
Deposits.txt
100.00 Withdrawals.txt 29.88 |
The starting balance was: $500.00n The ending balance of the account is: $662.78n The total amount of deposits was: $340.47n The total amount of withdrawals was: $180.30n The total interest earned was: $2.61n
Expert Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
//declarin the two files
private static final String deposit = “C:\deposit.txt”;
private static final String withdrawl = “C:\withdrawl.txt”;
public static void main(String[] args) {
double dsum,wsum,interest,savings,fbal,rate,tr; //varialb declaration
dsum=0.0;
tr=0.0;
rate=4.75;
wsum=0.0;
savings=500.00;
fbal=0.0;
interest=0.0;
BufferedReader br = null;
FileReader fr = null;
BufferedReader br1 = null;
FileReader fr1 = null;
try {
fr = new FileReader(deposit);
br = new BufferedReader(fr);
fr1 = new FileReader(withdrawl);
br1 = new BufferedReader(fr1);
String s,s1;
double n,n1;
br = new BufferedReader(new FileReader(deposit));
br1 = new BufferedReader(new FileReader(withdrawl));
while ((s = br.readLine()) != null) //taking input from the deposit file
{
n=Double.parseDouble(s);
dsum=dsum+n;
}
while ((s1 = br1.readLine()) != null) //taking input from thw withdrawl file
{
n1=Double.parseDouble(s1);
wsum=wsum+n1;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
fbal=fbal+savings+dsum-wsum; //calculating the total balance
tr=((fbal*rate)/12); //calculating the interest
interest=(tr/100);
fbal=fbal+interest;
System.out.println(“The starting balance was:”+savings); //displaying the result
System.out.println(“The ending balance of the account is:”+fbal);
System.out.println(“The total amount of deposits was:”+dsum);
System.out.println(“The total amount of withdrawals was:”+wsum);
System.out.println(“The total interest earned was:”+interest);
}
}