In: Computer Science
(Java)// Slightly different problem from the others that use the same input files, it has been adapted from a practice problem//
// Also if you can keep the code as simple as possible, try to avoid any advanced techniques, so that I can easily understand what is happening in case I decide to use this for future reference (which most likely I am) that'd be super appreciated//
Deposit and Withdrawal Files:
Use Notepad or another text editor to create a text file named Deposits.txt. The file should contain the following numbers, one per line:
///////////// Deposits.txt /////////////
100.00
125.00
78.92
37.55
/////////////////////////////////////////////
Next, create a text file named Withdrawals.txt. The file should contain the following numbers, one per line:
///////////// Withdrawals.txt /////////////
29.88
110.00
27.52
50.00
12.90
///////////////////////////////////////////////////
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. The starting balance on the account is $500.00. The program should read the values from the Deposits.txt file and add them to the starting balance of the account. The program should read the values from the Withdrawals.txt file and subtract them from the account balance. Display the new balance after each deposit and withdrawal so that you can see the change to the account balance in the output, and display the total interest earned (The interest is 10%). // This is an adapted version of a practice problem, so if the interest part doesn't make sense in this scenairo please feel free to omit it.//
Program Screenshot :
Program code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
public class ReadFiles {
private static DecimalFormat df = new
DecimalFormat("0.00");
public static void main(String[] args) throws
IOException {
// Read text file
Deposits.txt
BufferedReader deposit = new
BufferedReader(new FileReader("Deposits.txt"));
// Read text file
Withdrawals.txt
BufferedReader withDraw = new
BufferedReader(new FileReader("Withdrawals.txt"));
// The starting balance
on the account is 500.00
double accoutBalence =
500.00;
double withdrwalBalance =
0.0;
// First line of
Deposits.txt
String dep =
deposit.readLine();
// First line of
Withdrawals.txt
String with = withDraw.readLine();
// Read both lines till reaches to null
while(dep != null || with != null) {
try {
// read the values from the
Deposits.txt file and add them to the starting balance of the
account.
accoutBalence = accoutBalence +
Double.parseDouble(dep);
// Display the new balance after
each deposit
System.out.println("After each deposit : " +
df.format(accoutBalence));
// read the values from the Withdrawals.txt file and
subtract them from the account balance
accoutBalence = accoutBalence -
Double.parseDouble(with);
// Display the new balance after each deposit and
withdrawal
System.out.println("After each withdrawl : " +
df.format(accoutBalence));
withdrwalBalance = withdrwalBalance +
Double.parseDouble(with);
// Read next lines in the text files
dep = deposit.readLine();
with = withDraw.readLine();
}catch(NullPointerException e) {}
}
deposit.close();
withDraw.close();
}
}
Sample
output: