英文:
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<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);
}
}
答案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<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());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论