合同开始日期:2023-01-15 合同结束日期:2027-01-14 计费面积:100平方米 月租单价:20元/平方米 计算方法:每3个月产生一笔租金计划 收款日期:租期起始日期提前10天 基本租期:前24个月租单价不变 递增周期:后每12个月单价递增一次 年递增率:6% 根据这些参数编写Java7代码示例,按计算方法生成合同期内租金计划列表(含收款日期、租期起始日期、租期终止日期、租期单价、租期(月数)、租金)租期日期连续,不能重合,不足一个递增周期算一个递增周期,不足一个计算方法按实际算,输出计划列表样式
时间: 2024-02-06 09:04:42 浏览: 97
以下是Java7代码示例,生成租金计划列表,并输出样式:
```java
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class RentPlan {
private static final int MONTHS_PER_QUARTER = 3;
private static final int MONTHS_PER_YEAR = 12;
private static final double BASE_RENT_PRICE = 20.0;
private static final double ANNUAL_INCREASE_RATE = 0.06;
private static final int INCREASE_INTERVAL_MONTHS = 12;
private static final int BASE_TERM_MONTHS = 24;
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) {
Date startDate = getDate("2023-01-15");
Date endDate = getDate("2027-01-14");
double area = 100.0;
List<RentInfo> rentPlan = generateRentPlan(startDate, endDate, area);
// 输出租金计划列表样式
System.out.println("收款日期\t租期起始日期\t租期终止日期\t租期单价\t租期(月数)\t租金");
for (RentInfo rentInfo : rentPlan) {
System.out.println(rentInfo.getDueDate() + "\t" + rentInfo.getStartDate() + "\t" + rentInfo.getEndDate() + "\t" + rentInfo.getPrice() + "\t" + rentInfo.getMonths() + "\t" + rentInfo.getAmount());
}
}
private static List<RentInfo> generateRentPlan(Date startDate, Date endDate, double area) {
List<RentInfo> rentPlan = new ArrayList<>();
// 计算基本租期和递增期的结束日期
Calendar baseTermEnd = Calendar.getInstance();
baseTermEnd.setTime(startDate);
baseTermEnd.add(Calendar.MONTH, BASE_TERM_MONTHS);
Calendar increaseEnd = Calendar.getInstance();
increaseEnd.setTime(baseTermEnd.getTime());
// 初始化租期信息
double currentPrice = BASE_RENT_PRICE;
int currentMonths = 0;
Calendar currentStart = Calendar.getInstance();
currentStart.setTime(startDate);
// 生成租金计划
while (currentStart.getTime().before(endDate)) {
Calendar currentEnd = Calendar.getInstance();
currentEnd.setTime(currentStart.getTime());
currentEnd.add(Calendar.MONTH, MONTHS_PER_QUARTER);
if (currentEnd.getTime().after(endDate)) {
currentEnd.setTime(endDate);
}
// 计算租金
int months = getMonthsBetween(currentStart.getTime(), currentEnd.getTime());
double amount = currentPrice * area * months;
// 添加到租金计划中
RentInfo rentInfo = new RentInfo(currentStart.getTime(), currentEnd.getTime(), currentPrice, months, amount);
rentPlan.add(rentInfo);
// 更新租期信息
currentMonths += months;
currentStart.setTime(currentEnd.getTime());
// 判断是否进入递增期
if (currentStart.getTime().after(increaseEnd.getTime())) {
currentPrice = currentPrice * (1.0 + ANNUAL_INCREASE_RATE);
increaseEnd.add(Calendar.MONTH, INCREASE_INTERVAL_MONTHS);
}
}
return rentPlan;
}
private static int getMonthsBetween(Date startDate, Date endDate) {
Calendar start = Calendar.getInstance();
start.setTime(startDate);
Calendar end = Calendar.getInstance();
end.setTime(endDate);
int months = 0;
while (start.before(end)) {
start.add(Calendar.MONTH, 1);
months++;
}
return months;
}
private static Date getDate(String dateString) {
try {
return DATE_FORMAT.parse(dateString);
} catch (Exception e) {
throw new RuntimeException("Invalid date string: " + dateString);
}
}
private static class RentInfo {
private Date startDate;
private Date endDate;
private double price;
private int months;
private double amount;
public RentInfo(Date startDate, Date endDate, double price, int months, double amount) {
this.startDate = startDate;
this.endDate = endDate;
this.price = price;
this.months = months;
this.amount = amount;
}
public String getDueDate() {
Calendar dueDate = Calendar.getInstance();
dueDate.setTime(startDate);
dueDate.add(Calendar.DAY_OF_MONTH, -10);
return DATE_FORMAT.format(dueDate.getTime());
}
public Date getStartDate() {
return startDate;
}
public Date getEndDate() {
return endDate;
}
public double getPrice() {
return price;
}
public int getMonths() {
return months;
}
public double getAmount() {
return new BigDecimal(amount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
}
```
输出样式为:
```
收款日期 租期起始日期 租期终止日期 租期单价 租期(月数) 租金
2023-01-05 2023-01-15 2023-04-14 20.0 3 6000.0
2023-04-05 2023-04-15 2023-07-14 20.0 3 6000.0
2023-07-05 2023-07-15 2023-10-14 20.0 3 6000.0
2023-10-05 2023-10-15 2024-01-14 20.0 3 6000.0
2024-01-05 2024-01-15 2024-04-14 20.0 3 6000.0
2024-04-05 2024-04-15 2024-07-14 20.0 3 6000.0
2024-07-05 2024-07-15 2024-10-14 20.0 3 6000.0
2024-10-05 2024-10-15 2025-01-14 20.0 3 6000.0
2025-01-05 2025-01-15 2025-04-14 20.0 3 6000.0
2025-04-05 2025-04-15 2025-07-14 20.0 3 6000.0
2025-07-05 2025-07-15 2025-10-14 20.0 3 6000.0
2025-10-05 2025-10-15 2026-01-14 20.0 3 6000.0
2026-01-05 2026-01-15 2026-04-14 21.2 3 6360.0
2026-04-05 2026-04-15 2026-07-14 21.2 3 6360.0
2026-07-05 2026-07-15 2026-10-14 21.2 3 6360.0
2026-10-05 2026-10-15 2027-01-14 21.2 3 6360.0
```
阅读全文