Search code examples
c#classoopsortedsetbank

C# bank system SortedSet


I tried to make a bank system that save the accounts in a sorted set by there balance (on desnding order).

when i printing the accounts its (using the for each in the main) dose not print in desanding order, its printing in inseration oreder (a1 a2 a3....) way?

allso, is it ok to add a instance to tha sorted set in the consractur? is there better way to that?

class Bank : IComparable<Bank>
{
    private static int numOfAccounts = 0;
    private readonly int _id;
    private double _balance;
    private static SortedSet<Bank> _banks { get; } = 
        new SortedSet<Bank>(new BankComperatorByBalance() );

    public Bank()
    {
        _id = numOfAccounts;
        numOfAccounts++;
        _banks.Add(this);
    }

    
    public override bool Equals(object? obj)
    {
        if (obj == null || !(obj is Bank other))
        {
            return false;
        }
      
        return this._id == other._id;
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
    public Bank(double balance) : this()
    {
        this._balance = balance;
    }

    public void deposit(double balance)
    {
        _banks.Remove(this);
        this._balance += balance;
        _banks.Add(this);
    }
    public bool withdrawal(double balance)
    {
        if(this._balance>= balance) { 
            this._balance -= balance;
            return true;
        }
        return false;
    }
    public bool transfer(Bank reciver,double balance)
    {
        if (this._balance - balance >= 0)
        {
            this.deposit(balance);
            reciver.deposit(balance);
            return true;
        }
        return false;
    }

    public string toString()
    {
        return "id: " + this._id + "\nBalance: " + this._balance;
    }

    public static SortedSet<Bank> GetBanks()
    {
        return _banks; 
    }

    public static void Main(string[] args)
    {
        foreach (var b in GetBanks().Reverse())
        {
            Console.WriteLine(b.toString());
        }
    }

    public int CompareTo(Bank? other)
    {
       if(other == null) return -1;
       if(other._balance == this._balance) return _id.CompareTo(other._id);
       return _balance.CompareTo(other._balance);
    }
}

class BankComperatorByBalance : Comparer<Bank>
{
    public override int Compare(Bank? x, Bank? y)
    {
        if(x == null) return 1;
        return x.CompareTo(y);
    }
}

Solution

  • How i understand u want to print items in descending order, then you need to change your GetBanks() method from this:

    public static SortedSet<Bank> GetBanks()
    {
        return _banks; 
    }
    

    to this:

    public static IOrderedEnumerable<Bank> GetBanks()
    {
        // here we ordering banks by balance:
        return _banks.OrderByDescending(b => b._balance);
    }
    

    and finally call like this:

    foreach (var b in GetBanks())
    {
        Console.WriteLine(b.toString());
    }