5 Replies - 14958 Views - Last Post: 15 March 2013 - 06:06 PM Rate Topic: -----

#1 sunny27c   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-March 13

SavingsAccount class java

Posted 15 March 2013 - 12:03 PM

Hi there everyone.. First of all thank you for posting great articles and help on the site. I am new to java and eclipse and I am on a deadline to write a program as following---

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.


Is This A Good Question/Topic? 0
  • +

Replies To: SavingsAccount class java

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: SavingsAccount class java

Posted 15 March 2013 - 12:07 PM

Please describe how your actual output is different than desired. Also, there's a data file involved. Can you show us what that looks like? Either post a few lines or the whole thing, also in code tags.
Was This Post Helpful? 0
  • +
  • -

#3 sunny27c   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-March 13

Re: SavingsAccount class java

Posted 15 March 2013 - 01:09 PM

View PostGregBrannon, on 15 March 2013 - 12:07 PM, said:

Please describe how your actual output is different than desired. Also, there's a data file involved. Can you show us what that looks like? Either post a few lines or the whole thing, also in code tags.


Here is my input text file --

0.00 100.00 -10.00
90.00 100.00 -10.00
180.00 100.00 -10.00


I just changed the client program (still don't know what is it doing) and it gives me output as below --

File opened
What is the annual interest rate?10
Methods working
First deposit = $110.0
First withdrawal = $110.0
Total interset = $0.0


Really appreciate your prompt response...

View Postsunny27c, on 15 March 2013 - 01:08 PM, said:

View PostGregBrannon, on 15 March 2013 - 12:07 PM, said:

Please describe how your actual output is different than desired. Also, there's a data file involved. Can you show us what that looks like? Either post a few lines or the whole thing, also in code tags.


Here is my input text file --

0.00 100.00 -10.00
90.00 100.00 -10.00
180.00 100.00 -10.00


I just changed the client program (still don't know what is it doing) and it gives me output as below --

File opened
What is the annual interest rate?10
Methods working
First deposit = $110.0
First withdrawal = $110.0
Total interset = $0.0


Really appreciate your prompt response...

Here the changed client code...
public static void main(String[]args)
		throws FileNotFoundException
	{
	double beginningbalance;
	
	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();
		
	Savingaccount sunny = new Savingaccount(beginningbalance, interestpaid);
	
	sunny.deposit(inFile.nextDouble());
	sunny.addInterest();
	sunny.withdrawal(inFile.nextDouble());
	sunny.addInterest();
		
	System.out.println("Methods working");
	System.out.println("First deposit = $" + sunny.getBalance());
	System.out.println("First withdrawal = $" + sunny.getBalance());
	System.out.println("Total interset = $" + sunny.interestrate);
	
	System.exit(0);
	
	}
	}

This post has been edited by GregBrannon: 15 March 2013 - 01:29 PM
Reason for edit:: Added code tags, removed bold from code.

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: SavingsAccount class java

Posted 15 March 2013 - 01:27 PM

Please. If you can bold everything, you can put code in code tags. See the '[CODE]' tool button in the editor's tool area? Use that instead of the 'B' Bold tool to put code in its proper place.

Then, quit bolding everything, or anything for that matter.

This post has been edited by GregBrannon: 15 March 2013 - 01:29 PM

Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: SavingsAccount class java

Posted 15 March 2013 - 01:37 PM

This constructor:
	public Savingaccount(double balance, double apr){		//constructor
		balance = startingbalance;
		apr = interestrate;
	}

is "backwards." The intent of the assignment's requirements and most constructors is to assign the constructor's parameter values to the class' instance variables. Your constructor does the opposite. Understand that the value on the right of the assignment operator is assigned to the variable on the left. Yours:

balance = startingbalance;

What it should be:

startingbalance = balance;

And so on . . .
Was This Post Helpful? 1
  • +
  • -

#6 sunny27c   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-March 13

Re: SavingsAccount class java

Posted 15 March 2013 - 06:05 PM

View PostGregBrannon, on 15 March 2013 - 01:37 PM, said:

This constructor:
	public Savingaccount(double balance, double apr){		//constructor
		balance = startingbalance;
		apr = interestrate;
	}

is "backwards." The intent of the assignment's requirements and most constructors is to assign the constructor's parameter values to the class' instance variables. Your constructor does the opposite. Understand that the value on the right of the assignment operator is assigned to the variable on the left. Yours:

balance = startingbalance;

What it should be:

startingbalance = balance;

And so on . . .

Sorry about the code tag....I changed the constructor and although its not showing any error its not giving desired output...
public static void main(String[]args)
		throws FileNotFoundException
	{
	double beginningbalance;
	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();
		
	Savingaccount sunny = new Savingaccount(beginningbalance, interestpaid);
	
	sunny.deposit(beginningbalance);
	sunny.addInterest();
	sunny.withdrawal(inFile.nextDouble());
	sunny.addInterest();
		
	System.out.println("Methods working");
	System.out.println("InterestRate value = $" + sunny.getInterestRate());
	System.out.println("First deposit = $" + sunny.getBalance());
	System.out.println("First withdrawal = $" + sunny.getBalance());
	System.out.println("Total interset = $" + sunny.interestrate);
	
	System.exit(0);
	

Thanks....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1