Using java to achieve a short-answer question bank transfer?

Category: Java EE
 
woshidajiangyoud
2012-03-07 09:59:41

Sponsored Links
Using Java to achieve a simple bank transfer problem

Sponsored Links

jeef2004
2012-03-07 10:07:54
for example

class Account {
    String name;
    double balance;

    public Account(String name) {
        this.name = name;
    }
    public Account(String name, double balance) {
        this.name = name;
        this.balance = balance;
    }
    public String getName() {return name;}

    public double getBalnace() {return balance;}

    public synchronized double take(double money) { //
        if (balance >= money) {
            balance -= money;
            return money;
        }
        money = balance;
        balance = 0;
        return money;
    }

    public synchronized void save(double money) { //
        if (money < 0) {
            return;
        }
        balance += money;        
    }
}

public class ATM {
    public static void main(String[] args) {
        Account a1 = new Account("a1", 5000);
        Account a2 = new Account("a2", 3000);

        transfer(a1, a2, 3000);
        transfer(a2, a1, 10000);
    }

    public static boolean transfer(Account from, Acctount to, double money) {
        if (moeny > from.getBalance()) {
            System.out.printf("Account %s has no enough money to transfer.\n", from.getName());
            return false;
        }
        to.save(from.take(money));
        System.out.printf(Account %s transfers money[%.2f] to account %s.\n", from.getName(), money, to.getName());
        return true;
    }
}
DUANQIBING168168
2012-03-07 10:14:49
thread about. . .
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.