gpt4 book ai didi

Swift - 如何在一群人之间平均分配一笔钱?

转载 作者:行者123 更新时间:2023-11-30 10:43:16 24 4
gpt4 key购买 nike

我目前正在构建一个克隆银行应用程序,我想做的一件事是添加“拆分交易”(然后可以与一组 friend 共享,每个 friend 支付给定的金额)。

最初,交易在 friend 之间平均分配(除非它没有平均分配,在这种情况下,剩余的部分将添加到一个不幸的 friend 身上)。然后,用户可以手动调整每次支付的金额,然后更新其他金额。如果用户手动调整了 friend 的金额,则当用户随后调整另一个 friend 的金额时,该 friend 的“分割”不会自动更新(即,如果用户说 friend 1 支付了 12 英镑,则该 friend 的“分割”将始终为 12 英镑,直到用户另有说法)。

我已经摆弄了一段时间,试图使该方法尽可能简洁和“快速” - 但我真的很感激任何有关我的方法的反馈。

出于这里的目的,我只是尝试在人们之间平均分配资金(但我仍然想解释“用户定义的分配”,以便当前的代码有意义)。

我正在使用https://github.com/Flight-School/Money来表示交易值(value),全部在 Transaction 类中。我需要进行相当多的舍入以确保分割和余数保留到小数点后两位。相关代码如下:

用于保存金额以及用户是否设置的结构(出于可编码原因需要是自定义对象):

struct SplitTransactionAmount: Codable {
let amount: Money<GBP>
let setByUser: Bool
}

用于保存 friend 姓名及其分割的字典,如果由用户设置 - 也是一个名称的 PeopleSplittingTransaction 数组,以便于显示。

var splitTransaction: [String: SplitTransactionAmount]
var namesOfPeopleSplittingTransaction = [String]()

这是分割事务的方法:

private func splitTransaction(amount: Money<GBP>, with friends: [String]) -> [String: SplitTransactionAmount] {

//First we remove any duplicate names.
let uniqueFriends = friends.removingDuplicates()
//Create an empty dictionary to hold the new values before returning.
var newSplitTransaction = [String: SplitTransactionAmount]()

let totalAmountToSplitRounded = amount.rounded.amount
let numberOfSplitters = uniqueFriends.count

let eachTotalRaw = totalAmountToSplitRounded / Decimal(numberOfSplitters)
let eachTotalRounded = Money<GBP>(eachTotalRaw).rounded.amount

let remainder = totalAmountToSplitRounded - (Decimal(numberOfSplitters) * eachTotalRounded)

if remainder == 0 {
//If the amount to split each goes in to the total with no remainder, everyone pays the same.
for friend in uniqueFriends {
newSplitTransaction[friend] = SplitTransactionAmount(amount: Money(eachTotalRounded), setByUser: false)
}
} else {
for friend in uniqueFriends {
if friend == uniqueFriends.first! {
//Unlucky first friend has to pay a few pence more!
newSplitTransaction[friend] = SplitTransactionAmount(amount: Money(eachTotalRounded + remainder), setByUser: false)
} else {
newSplitTransaction[friend] = SplitTransactionAmount(amount: Money(eachTotalRounded), setByUser: false)
}
}
}
return newSplitTransaction
}

我认为我发现的问题是代码对我来说非常有意义,但我不确定它对外部读者来说有多清晰。对我的方法的任何想法将不胜感激(并且对这个长问题感到抱歉!)。我也很想知道是否有办法写得更简洁!

非常感谢

最佳答案

IMO,简化为不同 ifs 上两个循环的单个循环。并减少重复代码(例如SplitTransactionAmount的init)


for (index, friend) in uniqueFriends.enumerated(){ //or if you don't prefer enumerated, use == on first index like before.
let money: Money<GBP>
if remainder != 0, index == 0 { //ensure remainder is only used if has value.
//Unlucky first friend has to pay a few pence more!
money = Money(eachTotalRounded + remainder)
} else {
money = Money(eachTotalRounded)
}


newSplitTransaction[friend] = SplitTransactionAmount(amount: money setByUser: false)

}

关于Swift - 如何在一群人之间平均分配一笔钱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56339500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com