[英]How to develop logic/algorithm for a part of invoice module?
因此,我正在開發發票模塊,並堅持邏輯。 生成發票的步驟:
Bringing all those rows whose cancelled_date is none or is of current_month from the database
這給了我所有用於生成當月客戶發票的數據。
偽代碼
If membership is new:
if (working_days/total_days) in a month is 1:
Don't calculate prorata
else:
calculate pro rata(For no. of days)
else:
calculate invoice generally
現在的問題是:可以在上述情況下設置客戶的cancelled_date,例如: 偽代碼-
If membership is new:
if (working_days/total_days) in a month is 1:
if cancelled_date == end_date_month:
Don't calculate prorata
else:
calculate pro rata
else:
if cancelled_date == end_date_month:
calculate pro rata(For no. of days)
else:
calculate pro rata (start_date & end_date for current
month)
else:
if cancelled_date == end_date_month:
calculate invoice generally
else:
calculate pro rata
我僅通過解決cancelled_date方案就不能使代碼冗余。 我無法想到以上的良好邏輯/算法。
不確定您編寫的算法有什么問題,程序每次只會走一條路,所以在我看來,冗余只是一個美學問題(而不是性能問題)。
盡管如此,我可以想到另一種方法:
score = 0
If membership is new:
score += 1
if (working_days/total_days) in a month is 1:
score += 10
if if cancelled_date == end_date_month:
score += 100
switch score:
case 1: calculate pro rata (start_date & end_date for current month)
case 11: calculate pro rata
case 111: Don't calculate prorata
case 101: calculate pro rata(For no. of days)
case 100: calculate invoice generally
case 0: calculate pro rata
由於您最多需要檢查3件事(是新的,一個月的w / t天為1,c_date等於e_date),因此您可以為每個檢查“分配”一個值(1、10、100)。 對這些值求和將得到一個唯一值,然后您可以在switch語句中對其進行操作。 這樣,您只需為每個檢查編寫一次if
語句。
也許您想添加一個'10'案例(這意味着'一個月中的w / t天為1'是正確的),但是我不確定那時會發生什么。
免責聲明:我不認為這是最好的主意……您只是要求其他算法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.