合同开始日期:2023-01-01,结束日期:2026-01-01,免租起始期:2023-01-01,免租终止日期:023-01-31,计费面积:100平方米,月租单价20/平方米,计算方式:按1个月计算一次,收款方式为按3个月收一次,提前收款期为1个月,基本租期1年,超过基本租期后按1年一个递增周期,一个递增周期内月租单价按递增率计算,下一个递增周期内月租单价按上一个递增周期内月租单价复合递增,年递增率为6%,按java7的语法:按计算方式产生合同期内含收款日期、租期起始日期、租期终止日期、租期单价、租期(月数)租金的计划列表,生成一个完整示例并配上代码说明
时间: 2024-02-13 22:05:18 浏览: 218
好的,根据您的要求,我为您生成了以下示例代码:
```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 RentContract {
private static final int BASIC_PERIOD_MONTHS = 12;
private static final int INCREASE_PERIOD_MONTHS = 12;
private static final BigDecimal INITIAL_PRICE = new BigDecimal("20");
private static final BigDecimal INCREASE_RATE = new BigDecimal("0.06");
private Date startDate;
private Date endDate;
private Date freeStartDate;
private Date freeEndDate;
private BigDecimal area;
private BigDecimal pricePerSquareMeter;
private int billingCycleMonths;
private int advanceBillingMonths;
private int basicPeriodMonths;
private int increasePeriodMonths;
private BigDecimal initialPrice;
private BigDecimal increaseRate;
public RentContract(Date startDate, Date endDate, Date freeStartDate, Date freeEndDate,
BigDecimal area, BigDecimal pricePerSquareMeter, int billingCycleMonths,
int advanceBillingMonths, int basicPeriodMonths, int increasePeriodMonths,
BigDecimal initialPrice, BigDecimal increaseRate) {
this.startDate = startDate;
this.endDate = endDate;
this.freeStartDate = freeStartDate;
this.freeEndDate = freeEndDate;
this.area = area;
this.pricePerSquareMeter = pricePerSquareMeter;
this.billingCycleMonths = billingCycleMonths;
this.advanceBillingMonths = advanceBillingMonths;
this.basicPeriodMonths = basicPeriodMonths;
this.increasePeriodMonths = increasePeriodMonths;
this.initialPrice = initialPrice;
this.increaseRate = increaseRate;
}
public List<RentPlan> generateRentPlan() {
List<RentPlan> rentPlanList = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
BigDecimal currentPrice = initialPrice;
int currentMonthCount = 0;
while (calendar.getTime().before(endDate)) {
if (calendar.getTime().before(freeEndDate)) {
// 免租期内不产生租金
rentPlanList.add(new RentPlan(calendar.getTime(), null, null, null, null));
} else {
BigDecimal currentRent = area.multiply(currentPrice);
Date rentStartDate = calendar.getTime();
calendar.add(Calendar.MONTH, billingCycleMonths);
Date rentEndDate = calendar.getTime();
if (rentEndDate.after(endDate)) {
rentEndDate = endDate;
currentRent = calculateFinalRent(rentStartDate, rentEndDate, currentPrice, currentMonthCount);
}
RentPlan rentPlan = new RentPlan(rentStartDate, rentEndDate, currentPrice, billingCycleMonths, currentRent);
rentPlanList.add(rentPlan);
currentMonthCount += billingCycleMonths;
if (currentMonthCount % (basicPeriodMonths + increasePeriodMonths) == 0) {
currentPrice = currentPrice.multiply(BigDecimal.ONE.add(increaseRate)).setScale(2, BigDecimal.ROUND_HALF_UP);
}
}
calendar.add(Calendar.MONTH, advanceBillingMonths);
}
return rentPlanList;
}
private BigDecimal calculateFinalRent(Date rentStartDate, Date rentEndDate, BigDecimal currentPrice, int currentMonthCount) {
int months = (int) ((rentEndDate.getTime() - rentStartDate.getTime()) / (1000 * 60 * 60 * 24 * 30L));
int basicPeriods = currentMonthCount / basicPeriodMonths;
int remainingMonths = currentMonthCount % basicPeriodMonths;
BigDecimal finalPrice = currentPrice;
for (int i = 0; i < basicPeriods; i++) {
finalPrice = finalPrice.multiply(BigDecimal.ONE.add(increaseRate)).setScale(2, BigDecimal.ROUND_HALF_UP);
}
for (int i = 0; i < remainingMonths; i++) {
finalPrice = finalPrice.multiply(BigDecimal.ONE.add(increaseRate)).setScale(2, BigDecimal.ROUND_HALF_UP);
}
for (int i = 0; i < months - currentMonthCount; i++) {
finalPrice = finalPrice.multiply(BigDecimal.ONE.add(increaseRate)).setScale(2, BigDecimal.ROUND_HALF_UP);
}
return area.multiply(finalPrice);
}
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = null;
Date endDate = null;
Date freeStartDate = null;
Date freeEndDate = null;
try {
startDate = sdf.parse("2023-01-01");
endDate = sdf.parse("2026-01-01");
freeStartDate = sdf.parse("2023-01-01");
freeEndDate = sdf.parse("2023-01-31");
} catch (Exception e) {
e.printStackTrace();
}
BigDecimal area = new BigDecimal("100");
BigDecimal pricePerSquareMeter = new BigDecimal("20");
int billingCycleMonths = 1;
int advanceBillingMonths = 3;
int basicPeriodMonths = BASIC_PERIOD_MONTHS;
int increasePeriodMonths = INCREASE_PERIOD_MONTHS;
BigDecimal initialPrice = INITIAL_PRICE;
BigDecimal increaseRate = INCREASE_RATE;
RentContract rentContract = new RentContract(startDate, endDate, freeStartDate, freeEndDate,
area, pricePerSquareMeter, billingCycleMonths, advanceBillingMonths, basicPeriodMonths,
increasePeriodMonths, initialPrice, increaseRate);
List<RentPlan> rentPlanList = rentContract.generateRentPlan();
for (RentPlan rentPlan : rentPlanList) {
System.out.println(rentPlan.toString());
}
}
}
class RentPlan {
private Date startDate;
private Date endDate;
private BigDecimal price;
private int months;
private BigDecimal rent;
public RentPlan(Date startDate, Date endDate, BigDecimal price, int months, BigDecimal rent) {
this.startDate = startDate;
this.endDate = endDate;
this.price = price;
this.months = months;
this.rent = rent;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public int getMonths() {
return months;
}
public void setMonths(int months) {
this.months = months;
}
public BigDecimal getRent() {
return rent;
}
public void setRent(BigDecimal rent) {
this.rent = rent;
}
@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String startDateStr = startDate != null ? sdf.format(startDate) : "";
String endDateStr = endDate != null ? sdf.format(endDate) : "";
String priceStr = price != null ? price.toString() : "";
String rentStr = rent != null ? rent.toString() : "";
return String.format("起始日期:%s,终止日期:%s,租金单价:%s,租期:%d个月,租金:%s", startDateStr, endDateStr, priceStr, months, rentStr);
}
}
```
上述代码实现了一个租赁合同的计划生成器,其主要逻辑如下:
1. 初始化合同参数,包括起始日期、结束日期、免租期、计费面积、月租单价、计算方式等;
2. 根据起始日期和结束日期,按计算方式逐个月生成租金计划;
3. 对于免租期内的月份,不产生租金计划;
4. 对于非免租期内的月份,根据计费面积和月租单价计算租金,生成租金计划;
5. 对于超过基本租期的租期,按照递增周期计算租金单价;
6. 对于递增周期内的月份,租金单价按照递增率逐月复合递增;
7. 最后一个周期内的租金单价按照剩余月份计算。
示例代码中,我们通过一个 `RentContract` 类来封装合同参数和租金计划生成逻辑,以及一个 `RentPlan` 类来封装租金计划数据。在 `main` 方法中,我们初始化合同参数并调用 `generateRentPlan` 方法生成租金计划,最后打印出所有租金计划的信息。
需要注意的是,在计算租金单价和租金总额时,我们使用了 `BigDecimal` 类型来避免浮点数精度误差带来的问题。同时,在格式化输出租金计划信息时,我们使用了 `String.format` 方法来格式化输出,增强了代码的可读性。
阅读全文