Ruby 之交租阶段信息生成

题目

我看了一下,这个题目应该不是什么机密,所以先放上来了。大概意思是根据合同信息生成交租阶段信息。

解答

要求是要使用 Ruby 生成交租阶段信息,由于时间比较仓促,变量名那些就用得随意了些。要点主要有下面这些:

  • 第一个交费阶段是要单独算
  • 整月和非整月的计算方式是不一样的(同样的整月,不同月的天数不一样,但是租金还是要算成一样的,我目前是把合同起始结束月都当做非整月来算)
  • 循环结束时间判断(其实也就是缴费阶段结束日期和合同的结束日期进行对比判断)
ruby 复制代码
require 'time'
require 'terminal-table'

class Rent
  def initialize(contract_start, contract_end, month_rent, cycle)
    @contract_start = Time.strptime(contract_start, "%Y-%m-%d").to_date
    @contract_end = Time.strptime(contract_end, "%Y-%m-%d").to_date
    @month_rent = month_rent
    @cycle = cycle

    @year_total_rent = 12 * month_rent.to_f
    @tb = Terminal::Table.new
  end

  def generate_next_month_last_day(time)
    date = Date.new(time.year, time.month, time.day)
    date = date.next_month
    Date.civil(date.year, date.month, -1)
  end

  def calculate_first_month_rent(time)
    month_last_day = Date.civil(time.year, time.month, -1)
    month_day_left = month_last_day.day - time.day + 1
    @year_total_rent * month_day_left / 365
  end

  def calculate_last_month_rent(time)
    @year_total_rent * time.day / 365
  end

  def add_rows
    # 合同起始和结束日期当月都按非整月计算,其他整月按整月的租金计算
    rent_day = @contract_start
    total = calculate_first_month_rent(@contract_start)

    to_end = false
    tmp_cycle = @cycle - 1
    next_month_last_day = rent_day
    if tmp_cycle == 0 # 一月一租
      next_month_last_day = Date.civil(next_month_last_day.year, next_month_last_day.month, -1)
    end
    until tmp_cycle == 0
      next_month_last_day = generate_next_month_last_day(next_month_last_day)
      if next_month_last_day > @contract_end
        total += calculate_last_month_rent(@contract_end)
        next_month_last_day = @contract_end
        to_end = true
        break
      end
      total += @month_rent
      tmp_cycle -= 1
    end

    start_date = rent_day
    end_date = next_month_last_day
    @tb.add_row([start_date, end_date, rent_day, "%.2f" % total])
    total = 0

    until to_end
      tmp_cycle = @cycle
      rent_day = Date.new(next_month_last_day.year, next_month_last_day.month, 15)
      start_date = next_month_last_day + 1
      until tmp_cycle == 0
        next_month_last_day = generate_next_month_last_day(next_month_last_day)
        if next_month_last_day > @contract_end
          total += calculate_last_month_rent(@contract_end)
          next_month_last_day = @contract_end
          to_end = true
          break
        end
        total += @month_rent
        tmp_cycle -= 1
      end
      end_date = next_month_last_day
      @tb.add_row([start_date, end_date, rent_day, "%.2f" % total])
      total = 0
    end
  end

  def show_table
    set_table_title
    set_table_headings
    add_rows
    print_table
  end

  def set_table_title
    @tb.title = "交租阶段信息"
  end

  def set_table_headings
    @tb.headings = ["开始日期", "结束日期", "交租日期", "交租阶段总租金"]
  end


  def set_align_column
    @tb.number_of_columns.times do |index|
      @tb.align_column(index + 1, :right)
    end
    @tb.align_column(0, :left)
  end

  def print_table
    puts @tb
    puts
  end
end

rent = Rent.new("2020-11-16", "2021-03-16", 5000, 2)
rent.show_table
ruby 复制代码
+------------+------------+------------+----------------+
|                     交租阶段信息                      |
+------------+------------+------------+----------------+
| 开始日期   | 结束日期   | 交租日期   | 交租阶段总租金 |
+------------+------------+------------+----------------+
| 2020-11-16 | 2020-12-31 | 2020-11-16 | 7465.75        |
| 2021-01-01 | 2021-02-28 | 2020-12-15 | 10000.00       |
| 2021-03-01 | 2021-03-16 | 2021-02-15 | 2630.14        |
+------------+------------+------------+----------------+

目前按照一月一交的话,交租日期那块显示还有点问题,这个后面再优化吧。

ruby 复制代码
+------------+------------+------------+----------------+
|                     交租阶段信息                      |
+------------+------------+------------+----------------+
| 开始日期   | 结束日期   | 交租日期   | 交租阶段总租金 |
+------------+------------+------------+----------------+
| 2020-11-16 | 2020-11-30 | 2020-11-16 | 2465.75        |
| 2020-12-01 | 2020-12-31 | 2020-11-15 | 5000.00        |
| 2021-01-01 | 2021-01-31 | 2020-12-15 | 5000.00        |
| 2021-02-01 | 2021-02-28 | 2021-01-15 | 5000.00        |
| 2021-03-01 | 2021-03-16 | 2021-02-15 | 2630.14        |
+------------+------------+------------+----------------+
相关推荐
niu_sama1 小时前
仿RabbitMQ实现消息队列客户端
分布式·rabbitmq·ruby
小诸葛的博客3 小时前
RabbitMQ入门5—exchange参数之durability
分布式·rabbitmq·ruby
Yvemil71 天前
MQ 架构设计原理与消息中间件详解(二)
开发语言·后端·ruby
一丝晨光1 天前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
Yvemil72 天前
RabbitMQ 入门到精通指南
开发语言·后端·ruby
鸡c2 天前
rabbitMq------客户端模块
分布式·rabbitmq·ruby
wclass-zhengge3 天前
RabbbitMQ篇(环境搭建 - 下载 & 安装)(持续更新迭代)
开发语言·后端·ruby
课堂随想4 天前
【git】配置 Git 的换行符处理和安全性||安装 Ruby
git·ruby
我的运维人生6 天前
MacOS升级Ruby版本详解:步骤、挑战与解决方案
开发语言·macos·ruby·运维开发·技术共享
Jet-W9 天前
Ruby基础语法
开发语言·后端·ruby