C#银行系统SortedSet

huangapple go评论73阅读模式
英文:

C# bank system SortedSet

问题

我尝试创建一个银行系统,它将账户按照余额(降序排列)保存在一个排序集中。

当我打印账户时(在主程序中使用foreach),它并没有按照降序打印,而是按照插入顺序(a1、a2、a3...)的方式打印?

另外,是否可以在构造函数中将实例添加到排序集中?有没有更好的方法?

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

我已经将你提供的代码翻译成中文,代码部分保持原样。

英文:

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&lt;Bank&gt;
{
    private static int numOfAccounts = 0;
    private readonly int _id;
    private double _balance;
    private static SortedSet&lt;Bank&gt; _banks { get; } = 
        new SortedSet&lt;Bank&gt;(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&gt;= balance) { 
            this._balance -= balance;
            return true;
        }
        return false;
    }
    public bool transfer(Bank reciver,double balance)
    {
        if (this._balance - balance &gt;= 0)
        {
            this.deposit(balance);
            reciver.deposit(balance);
            return true;
        }
        return false;
    }

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

    public static SortedSet&lt;Bank&gt; 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&lt;Bank&gt;
{
    public override int Compare(Bank? x, Bank? y)
    {
        if(x == null) return 1;
        return x.CompareTo(y);
    }
}

答案1

得分: 0

如何理解你想要按降序打印项目,那么你需要将你的GetBanks()方法从这个:

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

改为这个:

public static IOrderedEnumerable<Bank> GetBanks()
{
    // 在这里,我们按余额对银行进行排序:
    return _banks.OrderByDescending(b => b._balance);
}

最后像这样调用:

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

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

public static SortedSet&lt;Bank&gt; GetBanks()
{
return _banks; 
}

to this:

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

and finally call like this:

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

huangapple
  • 本文由 发表于 2023年3月9日 18:13:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定