Results 1 to 6 of 6
  1. #1
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default How to display "Insufficient amount"

    Hello I am woriking on a bank account program, it accept balance amount, deposit amount and wihtraw amount, as a result of this display account balance. I want to add a feature on my withraw method, if withraw amount is greater that balance display "Insufficient amount" message. Below is my withraw method;
    Java Code:
    //Withraw method
        public double withraw()
        {  
        	//Check if we can do withraw
        	if (balance >= withraw)
        	{
        		balance = balance - withraw;
        		return withraw;
        	}
        	else
        		return 0.0;
        }
    I don't know where to place
    Java Code:
    System.out.println("Insufficient amount");
    I tried before and after return 0.0; however I did not see message when I tried to withraw amount bigger than balance .
    If you request all codes I can post it.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Placing the print statement after the return statement will do nothing, and in fact should result in a compiler error (I think, unless it's just a warning, either way, it won't help).

    We don't need to see all of your code, but it would help to see what you tried with the print statements. How are you calling this method? What are you doing with the return value?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hacikho View Post
    Hello I am woriking on a bank account program, it accept balance amount, deposit amount and wihtraw amount, as a result of this display account balance. I want to add a feature on my withraw method, if withraw amount is greater that balance display "Insufficient amount" message. Below is my withraw method;
    Java Code:
    //Withraw method
        public double withraw()
        {  
        	//Check if we can do withraw
        	if (balance >= withraw)
        	{
        		balance = balance - withraw;
        		return withraw;
        	}
        	else
        		return 0.0;
        }
    I don't know where to place
    Java Code:
    System.out.println("Insufficient amount");
    I tried before and after return 0.0; however I did not see message when I tried to withraw amount bigger than balance .
    If you request all codes I can post it.
    A bank account doesn't print anything; it just has to keep an amount of money; if I were you I'd implement a simple method, say, isSufficient(double amount) that returns a boolean value indicating that the account has enough money to withdraw 'amount' money. Something else that wants to print something can call this method and print something if necessary; this is called 'separation of concerns and/or responsibilities'.

    kind regards,

    Jos

  4. #4
    hacikho is offline Member
    Join Date
    Sep 2009
    Posts
    35
    Rep Power
    0

    Default

    is this what you mean?
    Java Code:
    //Withraw method
        public double withraw()
        {  
        	//Check if we can do withraw
        	if (balance >= withraw)
        	{
        		balance = balance - withraw;
        		return withraw;
        	}
        	else
        		//Can not do withraw
        		return  isSufficient();
        }
        
        public String isSufficient()
        {
        	System.out.println(" InSufficient amount");
        }

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hacikho View Post
    is this what you mean?
    Java Code:
    //Withraw method
        public double withraw()
        {  
        	//Check if we can do withraw
        	if (balance >= withraw)
        	{
        		balance = balance - withraw;
        		return withraw;
        	}
        	else
        		//Can not do withraw
        		return  isSufficient();
        }
        
        public String isSufficient()
        {
        	System.out.println(" InSufficient amount");
        }
    No that is not what I mean; I mean this:

    Java Code:
    public boolean isSufficient(double amount) {
       return balance >= amount;
    }
    ... and if something else wants to print something it should use this little method on an Account.

    kind regards,

    Jos

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Quote Originally Posted by hacikho View Post
    is this what you mean?
    No. Firstly, that doesn't compile: you can't return a String from a method that returns a double. And you can't not return a String from a method that is supposed to.

    Secondly, it doesn't separate the bank features from the display features.

    I think you should take a step back. Where were you putting the print statement originally that it didn't work?

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 05:21 PM
  2. "java -version" doesn't display proper value.
    By goldest in forum New To Java
    Replies: 8
    Last Post: 11-06-2009, 10:30 AM
  3. Replies: 0
    Last Post: 04-20-2009, 12:08 PM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •