简体   繁体   中英

withdraw() method for a java currency exchange

I am having trouble with this section of a project. The basic idea is to have a program as a user with a withdraw amount and then update the total balance. I was trying to do it using a switch statement. I dont understand how to do this with while keeping the 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;

Withdrawal amount should be converted to the account currency first, similar to how you have converted it in the deposit.

You should then check whether there would be sufficient balance; if not, return false.

Lastly, update the balance & return true.

I'll give you a cleaner example.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM