import java.math.BigDecimal;
/**
* 还款方式
*
* @author ASUS_XP
*/
public class Test {
public static void main(String[] args) {
//get5(30000,0.02,3);
get4(30000,0.02,3);
//get3(30000,0.02,3);
//get2(30000,0.02,3);
//get1(30000,0.02,3);
//get(30000,0.02,3);
}
/**
* 提前收息 - 等额本金
* @param principal 本金
* @param rate 月利率
* @param m 借款月数
*/
public static void get5(double principal, double rate, int months){
//还本
double hb = 0;
//还息
double hx = 0;
for (int i = 0; i < months; i++) {
hx = getDoubleValue(principal * rate);
if(i == 0){
System.out.println("还款本金:"+ hb +" -还款利息:"+ hx +"-还款合计:" + hx +"-剩余本金" + principal);
}
hb = getDoubleValue(principal/ (months-i));
principal = principal - hb;
System.out.println("还款本金:"+ hb +"-还款利息:"+hx +"-还款合计:" + (hb + hx) +"-剩余本金:" + principal);
}
}
/**
* 提前收息 - 按月付息到期还本
* @param principal 本金
* @param rate 月利率
* @param m 借款月数
*/
public static void get4(double principal, double rate, int months){
//还本
double hb = 0;
//还息
double hx = getDoubleValue(principal * rate);
for (int i = 0; i < months; i++) {
System.out.println("还款本金:"+ hb +"-还款利息:"+hx +"-还款合计:" + (hb + hx) +"-剩余本金:"+ principal );
if(i == months-1){
System.out.println("还款本金:"+ principal +"-还款利息:0" +"-还款合计:" + (principal) +"-剩余本金:0");
}
}
}
/**
* 按时收息-按月付息到期还本
* @param principal 本金
* @param rate 月利率
* @param m 借款月数
*/
public static void get3(double principal, double rate, int months){
//还本
double hb = 0;
//还息
double hx = getDoubleValue(principal * rate);
for (int i = 0; i < months; i++) {
if(i == months-1){
System.out.println("还款本金:"+ principal +"-还款利息:"+hx +"-还款合计:" + (principal + hx) +"-剩余本金:0");
}else{
System.out.println("还款本金:"+ hb +"-还款利息:"+hx +"-还款合计:" + (hb + hx) +"-剩余本金:" + principal);
}
}
}
/**
* 按时收息-等额本金
* @param principal 本金
* @param rate 月利率
* @param m 借款月数
*/
public static void get2(double principal, double rate, int months){
//还本
double hb = getDoubleValue(principal/ months);
//还息
double hx = 0;
for (int i = 0; i < months; i++) {
hx = getDoubleValue(principal * rate);
principal = principal - hb;
System.out.println("还款本金:"+ hb +"-还款利息:"+hx +"-还款合计:" + (hb + hx) +"-剩余本金:" + principal);
}
}
/**
* 按时收息-等额等息
* @param principal 本金
* @param rate 月利率
* @param m 借款月数
*/
public static void get1(double principal, double rate, int months){
//还本
double hb = getDoubleValue(principal/months);
//还息
double hx = getDoubleValue(principal*rate);
for (int i = 0; i < months; i++) {
principal = principal - hb;
System.out.println("还款本金:"+ hb +"-还款利息:"+hx +"-还款合计:" + (hb + hx) +"-剩余本金:" + principal);
}
}
/**
* 按时收息-等额本息
* 月还款金额合计 A = Aβ(1+β)^m / [(1+β)^m-1]
* @param principal 本金
* @param rate 月利率
* @param m 借款月数
*/
public static void get(double principal, double rate, int months){
double lx = principal*rate;
double X = lx * Math.pow(1+rate, months);
double Y = Math.pow(1+rate, months)-1;
//本息合计
double bx = getDoubleValue(X/Y);
double hb = 0;
for (int i = 0; i < months; i++) {
lx = getDoubleValue(principal*rate);
hb = getDoubleValue(bx - lx);
principal = getDoubleValue(principal - hb);
System.out.println("还款本金:"+ hb +"-还款利息:"+lx +"-还款合计:" + bx +"-剩余本金:" + principal);
}
}
/**
* 保留两位小数
* @param param
* @return
*/
public static double getDoubleValue(double param){
BigDecimal bg = new BigDecimal(param);
return bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
/**
* 还款方式
*
* @author ASUS_XP
*/
public class Test {
public static void main(String[] args) {
//get5(30000,0.02,3);
get4(30000,0.02,3);
//get3(30000,0.02,3);
//get2(30000,0.02,3);
//get1(30000,0.02,3);
//get(30000,0.02,3);
}
/**
* 提前收息 - 等额本金
* @param principal 本金
* @param rate 月利率
* @param m 借款月数
*/
public static void get5(double principal, double rate, int months){
//还本
double hb = 0;
//还息
double hx = 0;
for (int i = 0; i < months; i++) {
hx = getDoubleValue(principal * rate);
if(i == 0){
System.out.println("还款本金:"+ hb +" -还款利息:"+ hx +"-还款合计:" + hx +"-剩余本金" + principal);
}
hb = getDoubleValue(principal/ (months-i));
principal = principal - hb;
System.out.println("还款本金:"+ hb +"-还款利息:"+hx +"-还款合计:" + (hb + hx) +"-剩余本金:" + principal);
}
}
/**
* 提前收息 - 按月付息到期还本
* @param principal 本金
* @param rate 月利率
* @param m 借款月数
*/
public static void get4(double principal, double rate, int months){
//还本
double hb = 0;
//还息
double hx = getDoubleValue(principal * rate);
for (int i = 0; i < months; i++) {
System.out.println("还款本金:"+ hb +"-还款利息:"+hx +"-还款合计:" + (hb + hx) +"-剩余本金:"+ principal );
if(i == months-1){
System.out.println("还款本金:"+ principal +"-还款利息:0" +"-还款合计:" + (principal) +"-剩余本金:0");
}
}
}
/**
* 按时收息-按月付息到期还本
* @param principal 本金
* @param rate 月利率
* @param m 借款月数
*/
public static void get3(double principal, double rate, int months){
//还本
double hb = 0;
//还息
double hx = getDoubleValue(principal * rate);
for (int i = 0; i < months; i++) {
if(i == months-1){
System.out.println("还款本金:"+ principal +"-还款利息:"+hx +"-还款合计:" + (principal + hx) +"-剩余本金:0");
}else{
System.out.println("还款本金:"+ hb +"-还款利息:"+hx +"-还款合计:" + (hb + hx) +"-剩余本金:" + principal);
}
}
}
/**
* 按时收息-等额本金
* @param principal 本金
* @param rate 月利率
* @param m 借款月数
*/
public static void get2(double principal, double rate, int months){
//还本
double hb = getDoubleValue(principal/ months);
//还息
double hx = 0;
for (int i = 0; i < months; i++) {
hx = getDoubleValue(principal * rate);
principal = principal - hb;
System.out.println("还款本金:"+ hb +"-还款利息:"+hx +"-还款合计:" + (hb + hx) +"-剩余本金:" + principal);
}
}
/**
* 按时收息-等额等息
* @param principal 本金
* @param rate 月利率
* @param m 借款月数
*/
public static void get1(double principal, double rate, int months){
//还本
double hb = getDoubleValue(principal/months);
//还息
double hx = getDoubleValue(principal*rate);
for (int i = 0; i < months; i++) {
principal = principal - hb;
System.out.println("还款本金:"+ hb +"-还款利息:"+hx +"-还款合计:" + (hb + hx) +"-剩余本金:" + principal);
}
}
/**
* 按时收息-等额本息
* 月还款金额合计 A = Aβ(1+β)^m / [(1+β)^m-1]
* @param principal 本金
* @param rate 月利率
* @param m 借款月数
*/
public static void get(double principal, double rate, int months){
double lx = principal*rate;
double X = lx * Math.pow(1+rate, months);
double Y = Math.pow(1+rate, months)-1;
//本息合计
double bx = getDoubleValue(X/Y);
double hb = 0;
for (int i = 0; i < months; i++) {
lx = getDoubleValue(principal*rate);
hb = getDoubleValue(bx - lx);
principal = getDoubleValue(principal - hb);
System.out.println("还款本金:"+ hb +"-还款利息:"+lx +"-还款合计:" + bx +"-剩余本金:" + principal);
}
}
/**
* 保留两位小数
* @param param
* @return
*/
public static double getDoubleValue(double param){
BigDecimal bg = new BigDecimal(param);
return bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}