2016-05-17 33 views
0

因此,我正在開發一個發票模塊,並堅持一個邏輯。 步驟來生成發票:如何開發部分發票模塊的邏輯/算法?

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場景來減少代碼。我無法想到上面的一個好邏輯/算法。

回答

0

不知道你寫的算法有什麼問題,程序每次只會在一個路徑上運行,所以在我看來,redundency只是一個可怕的問題(而不是性能問題)。

Nontheless,我能想到的方法也不盡相同:

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'的情況(意思是'只有'一個月的天數是1'是真的),但我不確定當時會發生什麼。

聲明:我不認爲這是最好的想法...你只是要求另一種算法。

+0

嘿,你好,謝謝!這也導致如果其他因爲代碼是在Python中。我仍然需要把這些if/else。 –

+0

對不起,我沒有考慮到這一點 – OzW

+0

我在想的是做兩個單獨的函數,並在類中爲cancelled_membership創建一個屬性並計算它們在不同函數中的發票。 –