Design a SavingsAccount class that stores a savings account’s interest rate and balance. The class constructor should accept the amount of the saving’s account starting balance and the annual interest rate. The class should also have (in addition to accessors and mutators) methods for subtracting the amount of a withdrawal, adding the amount of a deposit, and adding the amount of monthly interest to the balance. (The monthly interest rate is the annual interest rate divided by 12.)
1. Create a UML class diagram for class SavingsAccount.
2. Implement the class in Java.
3. Create a JUnit test case to test the class methods.
4. Write a client program that reads a month’s deposit and withdrawal transactions from a text file transactions.txt and uses the SavingsAccount class methods to add deposits, subtract withdrawals, add monthly interest, and compute and display the ending balance and total interest earned. The input file consists of the starting balance of the month and the annual interest rate on the first line, followed by a sequence of deposit and withdrawal amounts separated by tabs. Deposits are represented with positive numbers and withdrawals with negative numbers. Assume overdrafts are rejected with no penalty.
I think I have most of the coding done but when I test my code it is not giving desired output. My code is ----
package savingaccount;
import java.util.*;
import java.io.*;
public class Savingaccount {
// private String owner;
private double startingbalance;
private double currentbalance;
private double interestrate;
private double lastinterest;
public Savingaccount(double balance, double apr){ //constructor
balance = startingbalance;
apr = interestrate;
}
//Savingaccount suhaag = new Savingaccount(0.0, 20000); //constuctor is called by new object suhaag
public double getstartingBalance(){
return startingbalance;
}
public double getcurrentBalance(){ //get current balance
return currentbalance;
}
public double getinterestrate(){ //gets interest
return interestrate;
}
public double getlastinterestrate(){
return lastinterest;
}
public double setinterest(){ //sets interest
return interestrate;
}
public void withdrawal(double w){
currentbalance = currentbalance - w;
}
public void deposit(double d){
currentbalance = currentbalance + d;
}
public void addInterest(){
double monthlyinterestrate = interestrate/12;
lastinterest = monthlyinterestrate * currentbalance;
currentbalance = currentbalance + lastinterest;
}
public static void main(String[]args)
throws FileNotFoundException
{
double beginningbalance;
double accountbalance;
double deposit;
double withdraw;
double interestpaid;
//Create input file objects
Scanner inFile = new Scanner(new FileReader("transactions.txt"));
beginningbalance = inFile.nextDouble();
System.out.print("File opened \n");
Scanner keyboard = new Scanner(System.in);
System.out.print("What is the annual interest rate?");
interestpaid = keyboard.nextDouble();
//Savingaccounts object
deposit = inFile.nextDouble();
withdraw = inFile.nextDouble();
accountbalance = deposit - withdraw;
Savingaccount sunny = new Savingaccount(beginningbalance, interestpaid);
sunny.getstartingBalance(); //balance before adding interest
sunny.addInterest(); //add interest
sunny.getlastinterestrate(); //get last interest rate
System.out.println("Methods working");
System.out.println("Starting Balance " + sunny.getstartingBalance());
System.out.println("Account balance $" + sunny.getcurrentBalance());
System.out.println("Total interest $" + sunny.getlastinterestrate());
System.out.println("Interest rate " + sunny.getinterestrate());
}
}
Thanks...
This post has been edited by GregBrannon: 15 March 2013 - 12:06 PM
Reason for edit:: Added code tags - do what you did to bold your code but select the [CODE] button instead.

New Topic/Question
Reply


MultiQuote



|