用于Java货币交换的withdraw()方法

时间:2016-12-07 00:49:38

标签: java methods

我在项目的这一部分遇到了麻烦。基本思路是将程序作为具有提款金额的用户,然后更新总余额。我试图使用switch语句来完成它。在保持dataType boolean

的同时,我不明白如何做到这一点
public static boolean withdraw(double amount, int currencyType) {

    double withdrawAmount = 0; 
    switch (currencyType){
    case 1: withdrawAmount = balance - amount;
    }

    updateBalance(getBalance() + convertCurrency(amount, currencyType, true));

    System.out.print(withdrawAmount);

    return false;

    //TODO: implementation here
}

//my deposit method//

public static boolean deposit(double amount, int currencyType) {    
    if(amount <= 0){

        return false;
    }

    String currency = "";
    switch (currencyType){
    case 1: currency = "U.S. Dollars"; break;
    case 2: currency = "Euros"; break;
    case 3: currency = "British Pounds"; break;
    case 4: currency = "Indian Rupees"; break;
    case 5: currency = "Australian Dollars"; break;
    case 6: currency = "Canadian Dollars"; break;
    case 7: currency = "Singapore Dollars"; break;
    case 8: currency = "Swiss Francs"; break;
    case 9: currency = "Malaysian Ringgits"; break;
    case 10: currency = "Japanese Yen"; break;
    case 11: currency = "Chinese Yuan Renminbi"; break;     
    default: return false;
    }

    updateBalance(getBalance() + convertCurrency(amount, currencyType, true));

    System.out.println("You successfully deposited " + amount + " " + currency);
    System.out.print("\n\n");

    return true;

1 个答案:

答案 0 :(得分:0)

提款金额应首先转换为账户货币,类似于您在存款中转换的方式。

然后你应该检查是否有足够的余额;如果没有,则返回false。

最后,更新余额&amp;返回true。

我会给你一个更清洁的例子。

public static boolean withdraw (double nominatedAmount, Currency nominatedCurrency) {    
    if (nominatedAmount > 0) {
        return false;
    }

    // convert to Account Currency.
    double convertedAmount = convertCurrency( nominatedAmount, nominatedCurrency, this.accountCurrency);

    // withdraw;
    //     -- checking for Sufficient Funds first.
    double outcomeBalance = getBalance() - convertedAmount;
    if (outcomeBalance  < 0) {
        return false;  // insufficient funds.
    }
    setBalance( outcomeBalance);
    log.info("withdrawal successful: acct={}, balance={}", this.accountNumber, outcomeBalance);

    // done;  success.
    return true;
}