4 Replies - 4840 Views - Last Post: 18 May 2011 - 08:21 AM Rate Topic: -----

#1 Aburton376  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 17-May 11

Banking and Savings Account Program

Posted 18 May 2011 - 12:13 AM

HI,

I am doing a Banking Account and Savings Account program and I keep getting a repeated answer when I run the program. It prints out a number five times and I don't know why Its Says withdraw and A number five times every time I change the number it is just the same. I am trying to print out different numbers of withdrawals. Like what I put in the test program. But something is just not right.
Here is my Code:
package banking;

public class BankAccount {
	private static final int AnnualInterestRate = 0;



	public static void main(String[] args) {
	}
		
	private double balance = 70.0;
	private double deposits = 0;
	private double withdrawals = 0;
	private double interestRate ;
	private double serviceCharge = 1;
	private double MonthlyServiceCharge=0;

	

	public BankAccount(double balance, double interestRate) {
          this.setBalance(balance);
          this.setInterestRate(interestRate);
          this.setDeposits(1);
	}


	public double deposit(double depositAmount){
		balance += depositAmount;
		return balance;
	}
	public double setBalance(double balance){
		return balance;
		
	}
	public double getBalance(){
		return balance;
	}
	public double getWithdrawals(){
		
		
		return withdrawals;
	}

	public double withdraw(double withdrawAmount){
		 if ( withdrawAmount > balance )
			 return -1;
		 else balance -= withdrawAmount;
		 
		 System.out.println("BankAccount Withdraw");
		 return withdrawAmount;
	} 

	private double calcInterest(){
	double	MonthlyInterestRate = (AnnualInterestRate / 12);
	double	MonthlyInterest = balance * MonthlyInterestRate;
		balance = balance + MonthlyInterest;
		     return MonthlyInterest; 
	}
	protected void monthlyProcess(){
		balance -= serviceCharge;
		calcInterest();
		withdrawals =setDeposits((int)(serviceCharge =0));
	}
	
	public void setMonthlyServiceCharge(double  MonthlyServiceCharge){
		MonthlyServiceCharge= 0;
	}
	public double getMonthlyServiceCharge() {
		return MonthlyServiceCharge;
	}
	

	public int setDeposits(int deposits) {
		this.deposits = deposits;
		return deposits;
	}


	public double getDeposits() {
		return deposits;
	}


	public void setInterestRate(double interestRate) {
		this.interestRate = interestRate;
	}


	public double getInterestRate() {
		return interestRate;
	}
}


package banking;

public class BankAccountTest {
public static void main(String[] args) {
		
		SavingsAccount a = new SavingsAccount(5000,500);
		
		
		a.deposit(0);
		System.out.println(a.getBalance());
		a.withdraw(5);
		a.withdraw(10);
		a.withdraw(10);
		a.withdraw(10);
		a.withdraw(5);
		a.withdraw(2);
		a.monthlyProcess();
	}

}


package banking;
public class SavingsAccount extends BankAccount{
	public double balance, amount,initialBalance;	
	private boolean active;
	private int deposit;

	SavingsAccount(double balance, double interestRate) {
		super(balance, interestRate);
		if(balance < 25.0 ){
			setActive(true);
			System.out.println("Your account is Active");
		}

		else{
			setActive(false);
			System.out.println(" Your account is InActive.");
		}}

	public double deposit(double depositAmount) {
		balance+=depositAmount;
		deposit++;
		return balance;
	}
	public double withdraw(double withrawAmount) {
		double amount = 65.0;
		if (isActive()&& withrawAmount >= this.getBalance()){ 
			amount = super.withdraw (withrawAmount);
			if (getBalance()< 25.0)
				active = false;
		}
		System.out.println("The amount is: "+ amount );
		return amount;
	}
	protected void monthlyProcess() {
		if(this.getDeposits()> 4 )
			this.withdraw(getDeposits()-4);
		super.monthlyProcess();
		System.out.println(" The Total deposits is :"+ getDeposits());
	}	
	
	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean isActive() {
		return active;
	}

	public void setDeposit(int deposit) {
		this.deposit = deposit;
	}

	public double getDeposits() {
		return deposit;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Banking and Savings Account Program

#2 SegFaulty  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 35
  • Joined: 11-October 10

Re: Banking and Savings Account Program

Posted 18 May 2011 - 12:19 AM

I'm not sure if this will fix your problem but you have an empty main method in BankAccount. You don't need that main method there, and could be causing problems.
Was This Post Helpful? 0
  • +
  • -

#3 Aburton376  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 17-May 11

Re: Banking and Savings Account Program

Posted 18 May 2011 - 12:38 AM

View PostSegFaulty, on 18 May 2011 - 12:19 AM, said:

I'm not sure if this will fix your problem but you have an empty main method in BankAccount. You don't need that main method there, and could be causing problems.


I got rid of the empty main and it still didn't work it printed out:
Your account is InActive.
70.0
The amount is: 65.0
The amount is: 65.0
The amount is: 65.0
The amount is: 65.0
The amount is: 65.0
The amount is: 65.0
The Total deposits is :1.
I'm trying to make the account active if its $25 or more but thats not working. If its less than $25 its supposed to be inActive. I'm trying to calculate monthly interest and Process and It just prints out whats above.
Was This Post Helpful? 0
  • +
  • -

#4 tux.aka.pingu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 11-May 11

Re: Banking and Savings Account Program

Posted 18 May 2011 - 04:32 AM

I don't know if thats it but, I think so.
Initially the account is "inActive" so you cannot withdraw money. To the amount is always assigned "65" and not changed (because of inactivity) and you then make a "System.out", therefore each time you try to withdraw money the same number is shown. Maybe you should change the "if" in your SavingsAccount.

from

if(balance < 25) active = true;



to

if(balance >= 25) active = true;



Hope I could help. greetz
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7739
  • View blog
  • Posts: 13,075
  • Joined: 19-March 11

Re: Banking and Savings Account Program

Posted 18 May 2011 - 08:21 AM

	public double withdraw(double withrawAmount) {
		double amount = 65.0;
		if (isActive()&& withrawAmount >= this.getBalance()){ 
			amount = super.withdraw (withrawAmount);
			if (getBalance()< 25.0)
				active = false;
		}
		System.out.println("The amount is: "+ amount );
		return amount;
	}



Start here:
		System.out.println("The amount is: "+ amount );

Every time this line executes the result is "The amount is 65". So that means that this line:
amount = super.withdraw (withrawAmount);

is not executing.

The means that this condition:

if (isActive()&& withrawAmount >= this.getBalance()){ 


is false. That is, it is not the case that both account is active and balance is sufficient.

Your test code clearly suggests that balance should be sufficient, so you want to make sure that getBalance is correct. (it is, but you should verify anyway)
Process of elimination leads us to isActive, which correctly returns false because... oops. Account is incorrectly set to inactive in the constructor.

tux's answer is correct, but you need to know how to find that answer for yourself, just in case tux is busy one day and you need to do your own debugging. Generally, it's good to start at the line where the error becomes obvious and work your way back to the problem, as I did here.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1