# How to program a debit in Java?



## Rhino24 (Feb 8, 2008)

I am trying to provide a method called debit that withdraws money from an account. I have to ensure that the debit does not exceed amount; if so it should print debit amount exceeded account balance. Can somebody start me off please? Thanks!


----------



## Chicon (Jul 29, 2004)

Hi Rhino24,

Welcome to TSG ! 

Here's a small example :

```
[SIZE=2]
public class Account {
    private double balance = 0;
    
    public void deposit(double amount) {
        balance += amount;
    }
    public void debit(double amount) {
        if (amount > balance) {
            System.out.println("Warning : " + amount + " exceeds " + balance);
        } else {
            balance -= amount;
        }
    }
    public void showBalance() {
        System.out.println("Balance = " + balance);
    }
}

public class Demo {
    public static void main(String[] args) {
        Account account = new Account();
        account.deposit(15000.0);
        account.showBalance();
        account.debit(10000.0);
        account.showBalance();
        account.debit(6000.0);
    }
}
[/SIZE]
```


----------



## Rhino24 (Feb 8, 2008)

Thanks A lot for getting me on the right track!!


----------

