-4

I am improving my skill in C# right now I am stuck on the SIMPLE MATH formula, I am working on a very basic banking system that can do basic banking functionality like deposit cash, withdraw and show balance. The problem where I stuck is simple math formula I want to deduct a 5% commission on the withdrawal amount, I know this line of code does not do the trick.

BankAccount.Comision c = comis => 2 * comis;

Here is my code:

 class BankDetails : IBankDetails
{
    ReturnedVal rv = new ReturnedVal();
    List<BankAccount> _accounts;

    public BankDetails()
    {
        _accounts = new List<BankAccount>();
    }

    public List<BankAccount> Account
    {
        get { return _accounts; }
    }


    public void CreateAccount(string name)
    {
        BankAccount account = new BankAccount();
        CalculateIBAN calculateIban = new CalculateIBAN();
        

        try
        {
            if (!String.IsNullOrEmpty(name))
            {
                account.Name = name;
                account.IBAN = calculateIban.IBAN();
                _accounts.Add(account);
                Console.WriteLine("Account created - Name: {0}, IBAN: {1}", account.Name, account.IBAN);
            }
            else
            {
                Console.WriteLine("Account name is null or empty.");
            }
        }
        catch (NullReferenceException ne)
        {
            Console.WriteLine(ne.StackTrace);
        }
    }

    public float Deposit()
    {
        string iban = rv.EnterIban();
        BankAccount account = rv.GetAccountByName(iban, _accounts);

        while (account == null)
        {
            Console.WriteLine("Account doesn't exist");
            iban = rv.EnterIban();
            account = rv.GetAccountByName(iban, _accounts);
        }

        float sum = rv.AmountToDeposit();
        while (sum <= 0)
        {
            Console.WriteLine("Amount cannot be less or equal than 0.");
            sum = 0;
            sum = rv.AmountToDeposit();
        }

        account.Sum += sum;
        Console.WriteLine("Added {0} to account {1}", sum, iban);

        return account.Sum;
    }

    public float Withdraw()
    {
        BankDetails details = new BankDetails();
        BankAccount.Comision c = comis => 2 * comis;

        string iban = rv.EnterIban();
        BankAccount account = rv.GetAccountByName(iban, _accounts);

        while (account == null)
        {
            Console.WriteLine("Account doesn't exist");
            iban = rv.EnterIban();
            account = rv.GetAccountByName(iban, _accounts);
        }

        float sum = rv.AmountToDeposit();
        while (sum <= 0)
        {
            Console.WriteLine("Amount cannot be less or equal than 0.");
            sum = 0;
            sum = rv.AmountToDeposit();
        }

        account.Sum -= sum;

        Console.Write("Withdrawn {0} from account {1}.", sum, iban);
        Console.WriteLine("Comision {0}", Math.Round(c(account.Sum)));
        account.Sum -= c(account.Sum);
        Console.WriteLine("Remaining: {0}", Math.Round(account.Sum));

        return account.Sum;
    }

    public float Balance()
    {
        string iban = rv.EnterIban();
        BankAccount account = rv.GetAccountByName(iban, _accounts);

        Console.WriteLine("IBAN: {0} has {1} left", iban, account.Sum);

        return account.Sum;
    }
}

here is my class where I declared delegate and properties

public class BankAccount
{
    public string Name { get; set; }
    public string IBAN { get; set; }
    public float Sum { get; set; }

    public delegate float Comision(float comis);

}
  • There's too much noise in your code as it is currently. Can you remove irrelevant parts and show us a minimal code that still reproduce your issue? Also I couldn't find anything in your code that reflects the 5% you're talking about in your question – Rafalon Feb 11 '21 at 09:16
  • Are you asking for a formula to deduct 5% from the value? Something like `x - x * 0.05`? – Sinatr Feb 11 '21 at 09:17
  • I think I would not want to do business with your bank if I loose 5% of all the money I deposit there. Better keep it under my pillow :) – Natalia Muray Feb 11 '21 at 09:19
  • 1
    Unrelated: [Why not use Double or Float to represent currency?](https://stackoverflow.com/q/3730019/982149) – Fildor Feb 11 '21 at 09:34
  • Also unrelated: note that you do not check whether the account has the required liquidity. Nothing would stop a user from withdrawing 1.000.000.000 Currency? – Fildor Feb 11 '21 at 09:40
  • @Fildor thanks for the useful tip as I said in the question I am new and still learning, i got your point now the question is how do I overcome the issues that you raise? –  Feb 11 '21 at 09:46
  • @NataliaMuray :) –  Feb 11 '21 at 09:46
  • Which one? "Using float" or checking sufficient funds? – Fildor Feb 11 '21 at 09:47
  • @Fildor both of them sir –  Feb 11 '21 at 09:49
  • For "float": as first step your could use `decimal` instead. But I'd rather suggest to look for something similar to the "Money" (or "Currency"? don't remember exactly) class in Java. – Fildor Feb 11 '21 at 09:49
  • For checking funds: Just check what the current funds on the account are. Maybe you also have features like ... don't know exactly what it's called in english ... a "Dispostionskredit" - a small (rather expensive) loan, where you can overdraw a little bit but have to pay extra fees... so you may want to warn the customer about extra fees and get affirmation or maybe you just reject withdraw for insufficient funds. – Fildor Feb 11 '21 at 09:52

2 Answers2

1

To get 5% of amount you must calculate (5 * amount)/100, which is the same as amount * 0.05f. In your case BankAccount.Comision c = amount => amount*0.05f;

niki.kante
  • 77
  • 3
  • your answer is correct too but I have to choose one and Natalia answered my question first –  Feb 11 '21 at 09:50
0

float fivePercent = amount * 0.05f;

Natalia Muray
  • 252
  • 1
  • 6