Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Suppressing proceeding statements after if statement fails

 
car michael
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone

I'm very new to programming and I'm having a bit of trouble with this code.



The program compiles just fine, but I want it to suppress the following statements after an if clause has failed (i.e. entering a negative number for the deposit amount), then initiate the scanner again so the user can input another value for the amount. At the moment, after the first if clause fails the line "Invalid amount. Try again." is printed, then proceeds to the next print line statement.

Any suggestions?
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you think that the credit and debit functions should come inside the if block?
In your code, you call the function then you check the condition which is meaningless.
 
Kiaamaa Liammes
Ranch Hand
Posts: 32
Eclipse IDE Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


try to separate the logic for debit and credit
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could make it more elegant by making it interactive.


 
Kiaamaa Liammes
Ranch Hand
Posts: 32
Eclipse IDE Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be better if you do the credit function after doing a check for the bal:




 
car michael
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kiaamaa,

I tried editing my code to look like yours but I got some serious errors when I ran the program. It compiled fine, but when I deposited $2000, it said my balance was $175. This is with a starting balance of $2175.

Also, the else statement does not execute when I enter zero or a negative number. I will post up what my AccountTest class looks like at the moment, as well as the Account class which defines the credit and debit methods.

Also, I should mention that nothing so far has done what I wanted it to do. I want to stop the next statement from printing after the user enters zero or a negative amount for debit, and then allow them to enter another number for debit. So basically, after they enter a negative number, the statement "Invalid amount. Try again." will appear and then the user can enter another number that does not execute the if clause.

So far, these suggestions have indeed made my program look more elegant, but the program still executes the next print line statement after a zero or negative number has been entered for the debit function.

EDIT:

Doh, sorry Kiaamaa. I got $175 for the withdrawal function. Got a little mixed up. Your code works just fine.

Account class:


AccountTest:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic