aspose file tools*
The moose likes Beginning Java and the fly likes need help with code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need help with code" Watch "need help with code" New topic
Author

need help with code

christopher pam
Greenhorn

Joined: Jan 12, 2005
Posts: 5
import java.util.Scanner;

public class Account
{
private double balance;
private double withdrawal;

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

if(withdrawal < 25000.00)
withdrawal = balance;
else
{
System.out.print("Debit amount exceeded account balance");
}
}
public void debit(double withdrawal)
{
balance = balance - withdrawal;
}

public void credit(double amount)
{
balance = balance + amount;
}

public double getBalance()
{
return balance;
}

public static void main(String args[])
{
Account account = new Account(25000.00);

Scanner input = new Scanner(System.in);
double depositAmount;
double withdrawalAmount;


System.out.printf("You've got $%.2f in your account\n",account.getBalance());

System.out.print("Enter deposit amount");
depositAmount = input.nextDouble();
System.out.printf("nAdding %.2f to the account concerned\n\n",depositAmount);
account.credit(depositAmount);
System.out.printf("You've got $%.2f in your account\n",account.getBalance());



System.out.print("Enter withdrawal amount");
withdrawalAmount = input.nextDouble();
System.out.printf("nDeducting $%.2f from the account concerned\n\n",withdrawalAmount);
account.debit(withdrawalAmount);
System.out.printf("You've got $%.2f in your account\n",account.getBalance());
}
}


the above code is supposed to give me ma account balance and prompt me to enter a deposit amount which it'll add up to the initial balance and afterwards prompt me to enter withdrawal amount,now this is where ma problem lies.im tryin to ensure that the debit amount does not exceed the account's balance,if it does the balance is to be left unchanged and printing a message indicating "Debit amount exceeds account balance".instead it keeps leaving the account with a negative balance.what do i do?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

Please use the code tags around your source code. there are those little "instand ubb code" buttons - one is labeld "code". by using this, and pasting your code between the tags, the formatting is preserved, making what you have MUCH easier to read.

Now, take a look at this code:


what is it doing?
[ March 19, 2007: Message edited by: Fred Rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help with code