2016-12-07 64 views
0

我在項目的這一部分遇到問題。基本的想法是將程序作爲具有提款金額的用戶,然後更新總餘額。我試圖用switch語句來做到這一點。我不知道如何使用,同時保持具體的數據類型布爾用於java貨幣兌換的withdraw()方法

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; 
+0

「while keep dataType boolean」which'dataType'?在你的數據類型的問題中沒有提到?編輯你的問題,目前還不清楚你問什麼。 –

+0

我的意思是這個方法必須返回一個布爾值。 – Andronik

+0

這是我如何做我的depost()方法,但即時提款退出。 – Andronik

回答

0

取款金額應首先轉換爲賬戶貨幣,類似你是如何在存款轉換它做到這一點。

您應該檢查是否有足夠的餘額;如果不是,則返回false。

最後,更新餘額&返回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; 
} 
+0

方法的第一行:您正在檢查未定義的'amount'值。您改用'nominatedAmount'代替。 –

+0

它必須保持公共靜態布爾撤回(double amount,int currencyType){ – Andronik

+0

謝謝伊恩。而@Andronik - 是的,但我更願意向你展示良好的做法,而不是爲你做準確的功課。學習。 –