6 Replies - 442 Views - Last Post: 14 May 2014 - 04:18 AM Rate Topic: -----

#1 neewb  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Active/Inactive Savings Account

Posted 11 May 2014 - 12:07 PM

This program asks the user for their starting balance, number of deposits, and number of withdrawals for a month

Now, if the number of withdrawals goes over 4, it has to include a service charge of 1.00 for every withdrawal that was more than 4 (This works fine so far)

The problem I am having is this:

The subclass part of this program is supposed to determine whether an account is below 25. If it is, it should make the account inactive, until the account is 25 or more. The subclass will have a method that will override the super's setWithdrawal mehtod to make sure that no withdrawals will be made until that balance is 25 or more.


Then the setDeposit method in the subclass will also override the super's setDeposit method and make sure that the account is not inactive before any deposits are made. If the account was determined to be inactive at that time and the deposits that were added make it jump to 25 or more, than the account should be changed to active and then be allowed to call the super's method to set the deposit.


So...the issue I am having is the subclass trying to determine whether the balance is under 25 before it calls the super's version of that method where it will allow the user to take money out and when making deposits where it will determine if the account was inactive first before make it active while the deposits raise to 25 or above.

I hope the above makes sense...here is my super class:


// This is an abstract class that will be used for its child class BankAccount
public abstract class BankAccount 
{
	private double balance;                   	// To hold the account balance for the month
	private int numDeposit;                  	// To hold the number of deposits for the month 
	protected int numWithdrawal;             	// To hold the number of withdrawals for the month
	private double annualIR;                 	// To hold the annual interest rate for the month
	protected double monthlyServCharge;     	// To hold any service charge for the month
	private double monthlyInterest;
	
	// Constructor
	public BankAccount(double bal, double AIR)
	{
		balance = bal;                          // Set the balance
		annualIR = AIR;                         // Set the annual interest rate
	}
	
	public void setDeposit(double dep)
	{
		balance += dep;                         // Add the deposit to the balance										
		numDeposit +=1;                         // Increment the deposit
	}
	
	public void setWithdrawal(double with)
	{
		balance -= with;                        // Subtract the withdrawal from the balance                     					
		numWithdrawal +=1;                      // Increment the withdrawal
	}
	
	public void setCalcInterest()
	{
		double monthlyIR;                     	// Holds the monthly interest rate
		             						 	// Holds the monthly interest
		monthlyIR = annualIR / 12;            
		monthlyInterest = balance * monthlyIR;
		balance = balance + monthlyInterest;
	}
	
	public void setMonthlyProcess(double charge)
	{
		balance -= monthlyServCharge;           // Subtract the monthly service charge from the balance
		
		setCalcInterest();                      // Call the setCalcInterest method to set the interest
	}
	
	public void setServiceCharge(double charges)
	{
		monthlyServCharge = charges;
	}
	
	
	public double getBalance()
	{
		return balance;
	}
	
	public int getNumDeposit()
	{
		return numDeposit;
	}
	
	public int getNumWithdrawal()
	{
		return numWithdrawal;
	}
	
	public double getInterest()
	{
		return monthlyInterest;
	}
	
	public double getMonthlyServiceCharge()
	{
		return monthlyServCharge;
	}	
}





And here is the sub/main


import javax.swing.JOptionPane;

public class Savings_Account extends BankAccount 
{
	boolean status;
	
	public Savings_Account(double bal, double AIR)
	{
		super(bal, AIR);
	}
	
	public void setWithdrawal(double with)
	{
		if(super.getBalance() < 25)
		{	
			status = true;
			JOptionPane.showMessageDialog(null, "Your request can not be completed.\n\nYour account has fallen below $25, and is inactive.");
			
		}
		
		else
		{
			status = false;
			super.setWithdrawal(with);
			setMonthlyProcess();
		}
	}
	
	public void setDeposit(double dep)
	{
		if(super.getBalance() < 25)
		{	
			status = true;
			JOptionPane.showMessageDialog(null,  "Your account is still inactive");
		}	
			
		else
		{
			status = false;
			//JOptionPane.showMessageDialog(null,"Your account is now active");
			super.setDeposit(dep);
		}
	}
	
	public void setMonthlyProcess()
	{
		double charge =0;
		
		if(numWithdrawal > 4)
		{	
			charge = numWithdrawal -4;	
			super.setServiceCharge(charge);
		}
		
		else if(super.getBalance() < 25)
		{	
			status = true;
			JOptionPane.showMessageDialog(null, "Your account is still inactive");
		}
		
		else
		{	
			status = false;
			super.setServiceCharge(charge);
		}	
	}
	
	public static void main(String[] args)
	{
		double startingBalance;
		double annualIntRate;
		double deposit = 0;
		double withdraw = 0;
		
		String string = JOptionPane.showInputDialog("Please enter your starting balance ");
		startingBalance = Double.parseDouble(string);
		
		String stringInput = JOptionPane.showInputDialog("Please enter your annual interest rate ");
		annualIntRate = Double.parseDouble(stringInput);
		
		Savings_Account account = new Savings_Account(startingBalance, annualIntRate);
		
		account.setCalcInterest();
	
		String str = JOptionPane.showInputDialog("How many deposits did you make this month ");
		double numDep = Double.parseDouble(str);
		
		for(int index = 0; index < numDep; index ++)
		{
			String input = JOptionPane.showInputDialog("Please enter the amount of deposit " + (index + 1));
		    deposit = Double.parseDouble(input);
			account.setDeposit(deposit);
		}
		
		String s = JOptionPane.showInputDialog("How many withdrawals did you make this month ");
		double numWith = Double.parseDouble(s);
		
		for(int index = 0; index < numWith; index ++)
		{
			String enter = JOptionPane.showInputDialog("Please enter the amount of withdrawal " + (index + 1));
			withdraw = Double.parseDouble(enter);
			account.setWithdrawal(withdraw);
		}
		
		JOptionPane.showMessageDialog(null, "Your total balance for the month is $" + account.getBalance() + "\n" +
				"Your total amount of deposits for the month was " + account.getNumDeposit() + "\nYour total amount of withdrawals was " +
				account.getNumWithdrawal() + "\nYour total interest was " + account.getInterest()  +"\nYour monthly service charge was " +
				account.getMonthlyServiceCharge());
		
		System.exit(0);	
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Active/Inactive Savings Account

#2 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 159
  • View blog
  • Posts: 623
  • Joined: 24-April 10

Re: Active/Inactive Savings Account

Posted 13 May 2014 - 02:37 AM

I'm not sure I fully understand your problem. But I do have this comment to make; In this code in the subclass:
029	    public void setDeposit(double dep)
030	    {
031	        if(super.getBalance() < 25)
032	        {  
033	            status = true;
034	            JOptionPane.showMessageDialog(null,  "Your account is still inactive");
035	        }  
036	             
037	        else
038	        {
039	            status = false;
040	            //JOptionPane.showMessageDialog(null,"Your account is now active");
041	            super.setDeposit(dep);
042	        }
043	    }

if the user has less than 25 in their account, you are preventing them from being able to make a deposit, so how will their account ever get over 25/active again?
Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: Active/Inactive Savings Account

Posted 13 May 2014 - 08:27 AM

I'm not clear on your actual problem. Your code seems ok.

One problem I do see is that you're storing data, rather than doing math on request. e.g.

public class Savings_Account extends BankAccount {
    // this makes things too complicated
    // boolean status;
    // also, "status" is ambiguous

    public Savings_Account(double bal, double AIR) {
        super(bal, AIR);
    }

    // now you can tell the user
    // and make the problem clear
    public boolean isActive() {
        return getBalance() >= 25.00;
        // note, no need for super: it's your getBalance, too.
    }

    public void setWithdrawal(double amount) {
        if (isActive()) {
            // now you need the super
            // to explicitly call the parent method
            super.setWithdrawal(amount);
            // why, exactly, do this?
            // setMonthlyProcess();
        } else {
            // do your really want UI interaction here?
            // either throw an exception or just ignore it
            // your UI should deal with the fall out
        }
    }


    public double getMonthlyServiceCharge() {
        double amount = super.getMonthlyServiceCharge();
        // do extra math here for Savings
        return amount;
    }



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#4 neewb  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Re: Active/Inactive Savings Account

Posted 13 May 2014 - 01:57 PM

View Postmike73, on 13 May 2014 - 02:37 AM, said:

I'm not sure I fully understand your problem. But I do have this comment to make; In this code in the subclass:
029	    public void setDeposit(double dep)
030	    {
031	        if(super.getBalance() < 25)
032	        {  
033	            status = true;
034	            JOptionPane.showMessageDialog(null,  "Your account is still inactive");
035	        }  
036	             
037	        else
038	        {
039	            status = false;
040	            //JOptionPane.showMessageDialog(null,"Your account is now active");
041	            super.setDeposit(dep);
042	        }
043	    }

if the user has less than 25 in their account, you are preventing them from being able to make a deposit, so how will their account ever get over 25/active again?


The directions for this program said something about making sure that if the account is inactive (under 25), that it needs to check with the witdrawal method first, to clarify that then once you start making deposits it will be able to tell you that the account is now active.

I probably messed up the coding on that, but here is the actual directions relating to what I attempted:

deposit: check active status, if inactive and the deposit brings balance above $25, the account becomes active again. deposit is then made by calling the superclass version of method.

Maybe I should have just called the withdrawal method inside the deposit method to check?
Was This Post Helpful? 0
  • +
  • -

#5 neewb  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 81
  • Joined: 16-October 12

Re: Active/Inactive Savings Account

Posted 13 May 2014 - 02:03 PM

View Postbaavgai, on 13 May 2014 - 08:27 AM, said:

I'm not clear on your actual problem. Your code seems ok.

One problem I do see is that you're storing data, rather than doing math on request. e.g.

public class Savings_Account extends BankAccount {
    // this makes things too complicated
    // boolean status;
    // also, "status" is ambiguous

    public Savings_Account(double bal, double AIR) {
        super(bal, AIR);
    }

    // now you can tell the user
    // and make the problem clear
    public boolean isActive() {
        return getBalance() >= 25.00;
        // note, no need for super: it's your getBalance, too.
    }

    public void setWithdrawal(double amount) {
        if (isActive()) {
            // now you need the super
            // to explicitly call the parent method
            super.setWithdrawal(amount);
            // why, exactly, do this?
            // setMonthlyProcess();
        } else {
            // do your really want UI interaction here?
            // either throw an exception or just ignore it
            // your UI should deal with the fall out
        }
    }


    public double getMonthlyServiceCharge() {
        double amount = super.getMonthlyServiceCharge();
        // do extra math here for Savings
        return amount;
    }



Hope this helps.



Thank you for the above information, I will see what I can come up with based on what you gave me.

 // why, exactly, do this?
            // setMonthlyProcess();


The reason I do this is because it will give me the monthly service charge if any....If I take that out, The service charge will be zero. It needs to called there because it has already figured out how many withdrawals you have made and that will determine whether or not a service charge will be required (over 4).
Was This Post Helpful? 0
  • +
  • -

#6 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 159
  • View blog
  • Posts: 623
  • Joined: 24-April 10

Re: Active/Inactive Savings Account

Posted 13 May 2014 - 11:51 PM

I think you need something like:
public void setDeposit(double dep)
{
	// user should be allowed to make a deposit regardless if the account is active or not
	setDeposit(dep); // don't need super, since it is public we can treat it as our own
	// Now we check if it should be active again
	if(!active && getBalance() > 25)
	{  
		status = true; // make it active again (a better variable name should be used, such as isActive or just active)
		JOptionPane.showMessageDialog(null,  "Your account is still inactive");
	}  
	// we don't need the else. The withdrawal should check if balance is less than 25 and then
	// set the account to inactive. setDeposit can be in charge are reactivating the account.
}

This post has been edited by mike73: 13 May 2014 - 11:55 PM

Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: Active/Inactive Savings Account

Posted 14 May 2014 - 04:18 AM

View Postmike73, on 14 May 2014 - 02:51 AM, said:

I think you need something like:
public void setDeposit(double dep)
{
	// user should be allowed to make a deposit regardless if the account is active or not
	setDeposit(dep); // don't need super, since it is public we can treat it as our own
...



Look at that again, real closely.

You MUST have super there. Otherwise, you've just created a recursive infinite loop. When you get something like "stack overflow" when you try to run that, that's what it means.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1