2016-05-17 30 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创建一个属性并计算它们在不同函数中的发票。 –