如何为发票模块的一部分开发逻辑/算法?

时间:2016-05-17 18:49:23

标签: python algorithm code-duplication invoice

所以我正在研究发票模块并坚持逻辑。 生成发票的步骤:

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场景来解决代码简化问题。我无法想到上面的良好逻辑/算法。

1 个答案:

答案 0 :(得分:0)

不确定你编写的算法有什么问题,程序每次只会进入一条路径,所以在我看来,redundency只是一个arestethic问题(而不是性能问题)。

尽管如此,我可以想到一个不同的方法:

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

由于您最多需要检查三件事(新的,一个月内的天数为1,c_date等于e_date),您可以指定'每个支票的值(1,10,100)。对这些值求和将给出一个唯一值,然后您可以在switch语句中对其进行操作。这样,您只需要为每次检查编写一次if语句。

也许你想要添加一个' 10'案例(仅表示'一个月内的日期是1'是真的),但我不确定当时会发生什么。

免责声明:我不认为这是最好的主意......你只是要求另一种算法。