In: Computer Science
I am using Java and what do you mean samples?
Creation of Program Application (Development Task 2)
This program should use a class file you create called: SavingsAccount.
This class file should store the following information: Annual Interest Rate (this can be hardcoded) Starting Balance (use a constructor to accept this information)
Create method to add all Deposits from Deposits.txt file.
Create method to subtract all Withdrawals from Withdrawals.txt file.
Create method to calculate the interest rate which is the annual rate divided by 12, added to the balance (if positive after all records have been read and calculated).
1. The program application should have the following inputs:
Deposists.txt
Withdrawls.txt
Starting Balance 2. The program application should have the following outputs:
Ending Balance
Total Amount of Deposits
Total Amount of Withdrawals
Total interest Earned
import java.io.*;
import java.util.*;
class SavingsAccount {
double rate;
double balance;
SavingsAccount(double balance) {
this.rate = 10.0;
this.balance = balance;
}
double addDeposits() throws FileNotFoundException {
FileReader deposit = new FileReader("./Deposits.txt");
Scanner scan = new Scanner(deposit);
double deposits = 0.0;
while(scan.hasNext()) {
double dep = Double.parseDouble(scan.next().toString());
this.balance += dep;
deposits += dep;
}
return deposits;
}
double subtractWithdrawals() throws FileNotFoundException {
FileReader withdraw = new FileReader("./Withdrawals.txt");
Scanner scan = new Scanner(withdraw);
double withdrawals = 0.0;
while(scan.hasNext()) {
double with = Double.parseDouble(scan.next().toString());
this.balance -= with;
withdrawals += with;
}
return withdrawals;
}
double addInterest() throws Exception {
double monthlyRate = rate / 12.0;
if (this.balance < 0) {
throw new Exception("balance cannot be negative for interest");
}
double interest = (this.balance * monthlyRate) / 100.0;
this.balance += interest;
return interest;
}
public static void main(String args[]) throws Exception {
// creating an object of this class
SavingsAccount save = new SavingsAccount(10000.0);
// calling methods
double deposits = save.addDeposits();
double withdrawals = save.subtractWithdrawals();
double interest = save.addInterest();
System.out.println("Ending Balance = " + save.getBalance());
System.out.println("Total Amount of Deposits = " + deposits);
System.out.println("Total Amount of Withdrawals = " + withdrawals);
System.out.println("Total interest Earned = " + interest);
}
double getBalance() {
return this.balance;
}
void setBalance(double balance) {
this.balance = balance;
}
}