In: Computer Science
Please do the following in JAVA.
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: 100.00 124.00 78.92 37.55 Next, create a text file named Withdrawals.txt. The file should contain the following numbers, one per line: 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. Write a program that creates an instance of the SavingsAccount class that you wrote in Programming Challenge 12. The starting balance for the object is 500.00. 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( Use 10% as the interest rate ), and then display the ending balance and the total interest earned.
Note :
We have to paste the input file in the project folder so that our program can able to detect.
If we didnt placed the input file in the project folder we will get FileNotFoundException.
We have to paste the input file in the
project folder so that our program can able to
detect.
Could you plz go through this code and let me know if u
need any changes in this.Thank You
=================================
// Deposits.txt (Input file)
100.00
125.00
78.92
37.55
===================================
// Withdrawls.txt (Input file)
29.88
110.00
27.52
50.00
12.90
===================================
// SavingsAccount.java
public class SavingsAccount {
private double interestrate;
private double balance;
/**
* @param balance
*/
public SavingsAccount(double balance) {
this.balance = balance;
this.interestrate = 0.10;
}
/**
* @return the balance
*/
public double getBalance() {
return balance;
}
public void withdraw(double amt) {
if (balance - amt >= 0) {
balance -=
amt;
}
}
public void deposit(double amt) {
if (amt > 0) {
this.balance +=
amt;
}
}
public double calculateMonthlyInterest() {
double interestAmt = (interestrate)
* balance;
this.balance += interestAmt;
return interestAmt;
}
}
========================================
========================================
// Test.java
import java.io.File;
import
java.io.FileNotFoundException;
import java.util.Scanner;
public class Test {
public static void main(String[] args)
throws FileNotFoundException {
Scanner sc =
null;
double amt;
SavingsAccount sa = new
SavingsAccount(500);
sc = new Scanner(new
File("Deposits.txt"));
while (sc.hasNext())
{
amt = sc.nextDouble();
sa.deposit(amt);
}
sc.close();
sc = new Scanner(new File("Withdrawls.txt"));
while (sc.hasNext())
{
amt = sc.nextDouble();
sa.withdraw(amt);
}
sc.close();
System.out.println("Monthly Interest is :$"+
sa.calculateMonthlyInterest());
System.out.printf("Ending balance is :$%.2f" ,
sa.getBalance());
}
}
========================================
========================================
Output:
============================== Thank You