Bankomat Algorithm

    0

    0

    YusifHasanov

    The following code defines a class Bankomat which keeps track of the sum of money that has been deposited in it. The class also has a start method which allows the user to enter the amount of money they want to withdraw.

    The class also has a Calculate method which takes in an input of an integer that represents the amount of money deposited, and an input of an integer that represents the number of bills in the denominations of 100, 50, 20, 10, and 5. These two inputs are then used to generate a string that contains the sum of money and the denominations of the money.

    The Calculate method also keeps a flag active which tells the method whether or not the amount entered is valid. If the flag is false, then the method returns the response "Not enough bills". If the flag is true, the method uses the sum of money and the denominations of the money to generate the response string.

    namespace Initital.Classes;
    
    public class Bankomat
    {
      public int ones { get; set; }
      public int fives { get; set; }
      public int tens { get; set; }
      public int twenties { get; set; }
      public int fifties { get; set; }
      public int hundreds { get; set; }
      public int twoHundreds { get; set; }
    
      public Bankomat(int ones, int fives, int tens, int twenties, int fifties, int hundreds, int twoHundreds)
      {
        this.ones = ones;
        this.fives = fives;
        this.tens = tens;
        this.twenties = twenties;
        this.fifties = fifties;
        this.hundreds = hundreds;
        this.twoHundreds = twoHundreds;
      }
    
      public void Start()
      {
        var sum = ones + fives * 5 + tens * 10 + twenties * 20 + fifties * 50 + hundreds * 100 + twoHundreds * 200;
        while (true)
          try
          {
            Console.WriteLine($"Enter the sum you want to withdraw (max {sum}): ");
            Console.Write("Enter amount: ");
            Console.WriteLine("Enter 0 for exit");
            var amount = int.Parse(Console.ReadLine());
            if (amount == 0)
            {
              Console.WriteLine("Goodbye!");
              break;
            }
    
            if (amount > sum || amount < 0)
              Console.WriteLine($"Enter between 1 and {sum}");
            else
              Console.WriteLine(Calculate(ones, fives, tens, twenties, fifties, hundreds, twoHundreds, amount));
          }
          catch (Exception)
          {
            Console.WriteLine("Enter valid amount");
          }
      }
    
      private static string Calculate(int ones, int fives, int tens, int twenties, int fifties, int hundreds,
        int twoHundreds, int amount)
      {
        var isTrue = true;
        var result = new List<int>();
        var response = "";
        while (amount > 0)
    
        {
          if (amount >= 200 && twoHundreds > 0)
          {
            result.Add(200);
            amount -= 200;
            twoHundreds--;
          }
          else if (amount >= 100 && hundreds > 0)
          {
            result.Add(100);
            hundreds -= 1;
            amount -= 100;
          }
          else if (amount >= 50 && fifties > 0)
          {
            result.Add(50);
            fifties -= 1;
            amount -= 50;
          }
          else if (amount >= 20 && twenties > 0)
          {
            result.Add(20);
            twenties -= 1;
            amount -= 20;
          }
          else if (amount >= 10 && tens > 0)
          {
            result.Add(10);
            tens -= 1;
            amount -= 10;
          }
          else if (amount >= 5 && fives > 0)
          {
            result.Add(5);
            fives -= 1;
            amount -= 5;
          }
          else if (amount >= 1 && ones > 0)
          {
            result.Add(1);
            ones -= 1;
            amount -= 1;
          }
          else
          {
            isTrue = false;
            response = "Not enough bills";
            break;
          }
    
          if (amount == 0) break;
        }
    
        if (!isTrue) return response;
        foreach (var item in result)
          response += item + " ";
    
        return response;
      }
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.