The directions for my assignment were:
Build a program that uses 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 the programming challenge 10. 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 substract 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.
import java.io.*;
import java.util.Scanner;
public class DepositWithdraw
{
public static void main(String[] args)throws IOException
{
double intRate ;
double amount=0.00;
double bal=500.00;
Scanner keyboard = new Scanner(System.in);
System.out.print("What is the annual interest rate?");
intRate= keyboard.nextDouble();
SavingAccount SavingAccount1= new SavingAccount(bal,intRate);
File file = new File ("Deposits.txt");
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
double num1 = inputFile.nextDouble();
amount += num1;
}
SavingAccount1.deposit(amount);
inputFile.close();
File file2 = new File("Withdrawals.txt");
Scanner inputFile2 = new Scanner(file2);
while (inputFile2.hasNext())
{
double num2 = inputFile2.nextDouble();
amount-= num2;
}
inputFile2.close();
SavingAccount1.getBalance();
SavingAccount1.addInterest();
SavingAccount1.getLastInterest();
System.out.println("Account balance $" + SavingAccount1.getBalance());
System.out.println("Total interest earned $" + SavingAccount1.getLastInterest());
}
}
public class SavingAccount
{
private double balance;
private double interestRate;
private double lastInterest;
public SavingAccount( double bal,double intRate)
{
balance = bal;
interestRate = intRate;
lastInterest = 0.0;
}
public void withdraw(double amount)
{
balance -= amount;
}
public void deposit(double amount)
{
balance+=amount;
}
public void addInterest()
{
double monthlyInterestRate = interestRate / 12;
lastInterest = monthlyInterestRate * balance;
balance += lastInterest;
}
public double getBalance()
{
return balance;
}
public double getIntrestrate()
{
return interestRate;
}
public double getLastInterest()
{
return lastInterest;
}
}
Attached File(s)
-
Withdrawals.txt (27bytes)
Number of downloads: 37 -
Deposits.txt (28bytes)
Number of downloads: 26

New Topic/Question
Reply



MultiQuote





|