4 Replies - 379 Views - Last Post: 08 October 2011 - 01:27 PM Rate Topic: -----

#1 wndtjr2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 05-October 11

bank account class method math error this codes doesn't run correc

Posted 08 October 2011 - 12:58 PM

I think calculation of intrest and total balance is incorrect.
I don't know what to do next.

import java.text.DecimalFormat;

import javax.swing.JOptionPane;


public class SavingAccount {

	public static void main(String[] args) {
		
		String intRate, stBalance, psMonth, input;
		double interRate, startBalance, depositIn, monthWith;
		int passMonth;
		
		DecimalFormat dollar = new DecimalFormat("##0.00");
		
		intRate = JOptionPane.showInputDialog("Enter the annual interest rate.");
		interRate = Double.parseDouble(intRate);
		
		stBalance = JOptionPane.showInputDialog("Enter the starting balance.");
		startBalance = Double.parseDouble(stBalance);
		
		SAccount sAccount = new SAccount(startBalance, interRate);
		
		psMonth = JOptionPane.showInputDialog("Enter the number of months that have passed since the account was established.");
		passMonth = Integer.parseInt(psMonth);
		
		for(int i = passMonth; i < 12; i++){
			
			input = JOptionPane.showInputDialog("Input the amount of money you want to deposit.");
			depositIn = Double.parseDouble(input);
			sAccount.setDeposit(depositIn);
			
			input = JOptionPane.showInputDialog("Input the amount of money you want to withdraw.");
			monthWith = Double.parseDouble(input);
			sAccount.setWithdrawal(monthWith);
			sAccount.setTotalInt(interRate);
		}JOptionPane.showMessageDialog(null, "total ending balance : " + dollar.format(sAccount.getBalance()) +
				"\ntotal amount of deposit : " + dollar.format(sAccount.getDeposit()) + 
				"\ntotal amount of withdral : " + dollar.format(sAccount.getwithdral()) +
				"\ntotal interest you earned : " + dollar.format(sAccount.getTotalInt()));
		System.exit(0);
	}
}




public class SAccount {
	
	private double interestRate, interest;
	private double balance;
	private double deposit;
	private double withdral;
	
	
	public SAccount(double bal,double intRate){
		balance = bal;
		interestRate = intRate;
	}
	
	public void setDeposit(double dposit){
		deposit += dposit;
	}
	
	public void setWithdrawal(double withd){
		withdral -= withd;
	}
	
	public void setIntRate(double rate){
		interestRate = rate;
	}
	
	public void setBalance(double bal){
		balance = bal;
		balance = deposit - withdral;	
		balance += balance;
	}
	
	public void setTotalInt(double intRate){
		interestRate = intRate; 
		interest = (interestRate / 12) * balance;
	}
	
	public double getDeposit(){
		return deposit;
	}
	
	public double getwithdral(){
		return withdral;
	}
	
	public double getBalance(){
		return balance;
	}
	
	public double getTotalInt(){
		return interest;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: bank account class method math error this codes doesn't run correc

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: bank account class method math error this codes doesn't run correc

Posted 08 October 2011 - 01:05 PM

Why do you think it's incorrect? Please, give us a hint, a place to start looking.

This post has been edited by GregBrannon: 08 October 2011 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: bank account class method math error this codes doesn't run correc

Posted 08 October 2011 - 01:16 PM

Your problem is your setDeposit() and setWithdrawal() methods. If someone deposit or withdraw, the money is added or taken from balance and not other variables. So that causes the problem.

Also look at your setBalance() method, it is very mistaken, values are overwritten there and I dont see anywhere you call it in main().

In short your class has many redundancy and bad designed. First, I was expecting interest rate to be static and only entered in constructor maybe but not everywhere.... Look again at your class
Was This Post Helpful? 0
  • +
  • -

#4 wndtjr2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 05-October 11

Re: bank account class method math error this codes doesn't run correc

Posted 08 October 2011 - 01:16 PM

When i run it, I see the last panel which has total balance and total deposits and total withdrawal and total interest. But I see total balance which is supposed to be TOTAL but it shows only start balance and interest doesn't seem like correct cause even though i put interest rate 100% it shows like 1000%
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: bank account class method math error this codes doesn't run correc

Posted 08 October 2011 - 01:27 PM

That is because the only place where you assign the balance is in constructor and your setBalance() which you dont call it anywhere and is redundant and mistaken.
The idea of design for me is like:
Your setbalance method has no need there because balance has been initial and is updated by deposit or withdraw.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1