12 Replies - 6054 Views - Last Post: 06 August 2010 - 07:00 AM Rate Topic: -----

#1 Vincent53  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-May 10

Account class ( debit method logic )

Posted 05 August 2010 - 12:23 PM

Hello Everyone,
I designed this account class and I do not get any error flags but I am not sure of my logic in the debit method part of the code. What I want to happen when I design the test class is for it to return " a negative balance is not allowed" if the _balance - amount < -1. Am I correct or wrong it just does not look right to me.
   public class Account
 {   
    private double _balance; // instance variable that stores the balance
     private double initialBalance;

    // constructor  
    public Account( double initialBalance )
    {
       // validate that initialBalance is greater than 0.0; 
       // if it is not, balance is initialized to the default value 0.0
       if ( initialBalance > 0.0 )
       {
         _balance = initialBalance; 
       } 
     } // end Account constructor

    // credit (add) an amount to the account
    public void credit( double amount )
    {      
       _balance = _balance + amount; // add amount to balance 
     } // end method credit
  
    // debit ( subtract) an amount from the account
   public void debit ( double amount )
    {
      _balance = _balance - amount;

      //
          if ( initialBalance - amount > _balance  )
          
              _balance = _balance - amount;
      
         
          if (_balance - amount < _balance) 
              
              _balance = -1;
   }
          
       // return the account balance
   public double getBalance()
    {
    return _balance; // gives the value of balance to the calling method
   } // end method getBalance

 } // end class Account



Edited by macosxnerd101: Fixed code tags. Please, :code:.

Is This A Good Question/Topic? 0
  • +

Replies To: Account class ( debit method logic )

#2 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Account class ( debit method logic )

Posted 05 August 2010 - 12:44 PM

I don't understand your conditions in your if statements under debit() method. You do realize that initialBalance - amount is equal to _balance, right? And as for the second if statement, _balance will definitely be assigned to -1, unless the amount is negative? @.@
Was This Post Helpful? 0
  • +
  • -

#3 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Account class ( debit method logic )

Posted 05 August 2010 - 12:52 PM

I'm sorry, your logic doesn't make any sense.

Let's pretend _balance was 1000 to begin with, and we passed amount in as 500, and also initialBalance is 1000.

So right off the bat, _balance becomes 500.

First if-statement: nothing happens because it's equal, not greater.

Next if-staetment: _balance will be set to -1 every single time, unless of course amount is negative.

If you subtract a positive value from a positive value, then the resulting value is going to be less than the one being subtracted from. This if-statement will prove to be true every time.


Hope this helps!

This post has been edited by eZACKe: 05 August 2010 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#4 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Account class ( debit method logic )

Posted 05 August 2010 - 01:05 PM

You have something wrong. It tourns out that initialBalance is private and I don't see any set method. So how can you initialize that variable ?.. initialBalance is going to be 0.0 always.

you should have two variables with names debit and credit. That would make sense for me.
Was This Post Helpful? 0
  • +
  • -

#5 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Account class ( debit method logic )

Posted 05 August 2010 - 01:21 PM

View Postguidojavier, on 05 August 2010 - 07:05 PM, said:

You have something wrong. It tourns out that initialBalance is private and I don't see any set method. So how can you initialize that variable ?.. initialBalance is going to be 0.0 always.

you should have two variables with names debit and credit. That would make sense for me.


Members in a class that are set as private variables are accessible by methods of their own class.
Was This Post Helpful? 0
  • +
  • -

#6 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Account class ( debit method logic )

Posted 05 August 2010 - 06:23 PM

View PostCuzzie, on 05 August 2010 - 04:21 PM, said:

Members in a class that are set as private variables are accessible by methods of their own class.


Very true, and though this probably doesn't have anything to do with the project at hand, it could be beneficial to have getter and setter methods for these private variables.

Why? Well it makes the code more reusable, and since a bank account program is usually a good program to expand upon and display OOP techniques, getters and setters could be very useful in the future if the OP is asked to expand on this program.

Just a thought :bigsmile:

This post has been edited by eZACKe: 05 August 2010 - 06:24 PM

Was This Post Helpful? 0
  • +
  • -

#7 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Account class ( debit method logic )

Posted 05 August 2010 - 06:54 PM

View PostCuzzie, on 05 August 2010 - 12:21 PM, said:

View Postguidojavier, on 05 August 2010 - 07:05 PM, said:

You have something wrong. It tourns out that initialBalance is private and I don't see any set method. So how can you initialize that variable ?.. initialBalance is going to be 0.0 always.

you should have two variables with names debit and credit. That would make sense for me.


Members in a class that are set as private variables are accessible by methods of their own class.



Yes you are right but where are you are initializing the initialBalance variable ?. What for is that variable ?. because it always is going to be zero. Well at least in the class there is not any method which initalize that variable with some value.

Maybe if the constructor was like this it would make sense for me:

 // constructor 
	   public Account( double initialBalance )
	   {
	      // validate that initialBalance is greater than 0.0;
	      // if it is not, balance is initialized to the default value 0.0
	      if ( initialBalance > 0.0 )
	      {
	        _balance = initialBalance;
                this.initialBalance = initialBalance;
	      }
	    } // end Account constructor


Was This Post Helpful? 0
  • +
  • -

#8 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Account class ( debit method logic )

Posted 05 August 2010 - 07:42 PM

I cleaned up your code a bit. The following works, but I've purposely left out somethings that is up to you to implement.
  • Make sure a valid number is entered (can't deposit negative numbers).
  • Make sure you withdraw only as much as you have.


package restaurant;

public class Account {

    private double balance;

    public Account(double initialBalance) {
        if (initialBalance > 0.0) {
            balance = initialBalance;
        } else {
            balance = 0.0;
        }
    }

    public void Deposit(double ammount) {
        balance += ammount;
    }

    public void Withdraw(double ammount) {
        balance -= ammount;
    }

    public double getBalance() {
        return balance;
    }
}


We can do it for you, but if you stutter here you'll have massive problems later on in your course.
Was This Post Helpful? 1
  • +
  • -

#9 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Account class ( debit method logic )

Posted 05 August 2010 - 09:48 PM

Best part of 's post? He changed the variable _balance to balance.

In my opinion, that is more than just a personal preference. I actually believe the use of underscores before variable names is discouraged.
Was This Post Helpful? 0
  • +
  • -

#10 Vincent53  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-May 10

Re: Account class ( debit method logic )

Posted 05 August 2010 - 10:01 PM

Hello Everyone,
Well I got this far in designing an account class and test but now I am completely stumped I am trying to insert a debit test in the Account test. I do not know what else to do.
// Account class with a constructor to
// initialize instance variable balance.

public class Account
{
   private double _balance; // instance variable that stores the balance

   // constructor
   public Account( double initialBalance )
   {
      // validate that initialBalance is greater than 0.0;
      // if it is not, balance is initialized to the default value 0.0
      if ( initialBalance > 0.0 )
      {
         _balance = initialBalance;
      }
   } // end Account constructor

   // credit (add) an amount to the account
   public void credit( double amount )
   {
      _balance = _balance + amount; // add amount to balance
   } // end method credit
public void debit (double amount )
{
    _balance = _balance - amount;
}
   // return the account balance
   public double getBalance()
   {
      return _balance; // gives the value of balance to the calling method
   } // end method getBalance

} // end class Account

// Inputting and outputting floating-point numbers with Account objects.
import java.util.Scanner;

public class AccountTest
{
   // main method begins execution of Java application  
    public static void main( String args[] ) 
    {
        // variable declarations
        double depositAmount;                     
        double debitAmount = 0;

        Scanner input = new Scanner( System.in );     // create Scanner object
        Account myAccount1 = new Account( 100.00 );   // create Account object
        Account myAccount2 = new Account( -9.00 );    // create Account object
 
        // display initial balance of each object
        System.out.printf( "account1 balance: $%.2f\n", myAccount1.getBalance() );
        System.out.printf( "account2 balance: $%.2f\n\n", myAccount2.getBalance() );

        // prompt and get deposit amount for account1 and deposit amount
        System.out.print( "Enter deposit amount for account1: " ); 
        depositAmount = input.nextDouble(); 
        System.out.printf( "\nadding %.2f to account1 balance\n\n", depositAmount );
        myAccount1.credit( depositAmount ); 
        System.out.print ( " Enter debit amount for account1 " );

        // display balances
        System.out.printf( "account1 balance: $%.2f\n", myAccount1.getBalance() );
        System.out.printf( "account2 balance: $%.2f\n\n", myAccount2.getBalance() );

        // prompt and get deposit amount for account2 and deposit amount
        System.out.print( "Enter deposit amount for account2: " ); 
        depositAmount = input.nextDouble(); 
        System.out.printf( "\nadding %.2f to account2 balance\n\n", depositAmount );
        myAccount2.credit( depositAmount );

        System.out.print ( debitAmount);
        debitAmount = input.nextDouble ();

        // display balances
        System.out.printf( "account1 balance: $%.2f\n", myAccount1.getBalance() );
        System.out.printf( "account2 balance: $%.2f\n", myAccount2.getBalance() );

    } // end main

} // end class AccountTest



Edited by macosxnerd101: Fixed code tags. Please, :code:.
Was This Post Helpful? 0
  • +
  • -

#11 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Account class ( debit method logic )

Posted 05 August 2010 - 10:52 PM

I don't really get what you mean. Are you saying that you don't know how to call debit() method in AccountTest? It's the same as calling the credit() method.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10469
  • View blog
  • Posts: 38,803
  • Joined: 27-December 08

Re: Account class ( debit method logic )

Posted 06 August 2010 - 06:32 AM

Duplicate topics merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#13 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Account class ( debit method logic )

Posted 06 August 2010 - 07:00 AM

Please remove all of the redundant comments, I can't make heads or tails of your code.

Things like Account bob = new Account(2.1); //Create new account object. are completely unnecesary.

What isn't working? After reading your code a bit it seems deposit should work fine. Explain in details what you want to happen, and what's actually happening.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1