首页 > 解决方案 > 由用户结束或重新启动的程序的良好班级组织

问题描述

我编写了一个小程序,它根据用户在控制台中输入的贷款金额、利率和期限来计算总利息、总利息百分比和其他指标。我的问题是:我希望程序比较用户想要输入的贷款的成本。那么,当用户输入他们想要测试另一笔贷款时,让我的所有方法重新运行的最佳方法是什么?我应该使用方法链接吗?我应该有一个不同的班级来管理这部分程序吗?提前致谢。贷款计划的代码如下。

import java.util.Scanner;

public class loanCalculator implements Comparable<loanCalculator> {

  //class variables 
  double term, rate, amount, monthlyPayment, perRate, totalRepaid, 
  totalPrincipalRepaid, totalInterestRepaid, totalInterestPercentage; 

  final double MONTHS_IN_YEAR = 12; 

  Scanner scan = new Scanner(System.in);

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    loanCalculator loan = new loanCalculator(); 
    loan.getTerm(); 
    loan.getRate(); 
    loan.getAmount();
    loan.setPeriodicInterestRate();
    loan.setMonthlyPayment();
    loan.setTotalRepaid();
    loan.setTotalPrincipalRepaid();
    loan.setTotalInterestRepaid();
    loan.setTotalInterestPercentage();
    System.out.println(loan.toString()); 

  }

  void getTerm() { 
    System.out.println("Enter the term of the loan in years");
    this.term = scan.nextDouble(); 
  }

  void getRate() { 
    System.out.println("Enter the rate");
    this.rate = scan.nextDouble(); 
  }

  void getAmount() { 
    System.out.println("Enter the amount (no commas or dollar signs");
    this.amount = scan.nextDouble(); 
  }

  void setPeriodicInterestRate() { 
    this.perRate = this.rate / 12 /100; 
  }

  void setMonthlyPayment() { 
    double N = -term*MONTHS_IN_YEAR;
    this.monthlyPayment = (perRate * amount) / (1-(Math.pow((1+perRate), N)));
  }

  void setTotalRepaid() { 
    this.totalRepaid = term * MONTHS_IN_YEAR * (monthlyPayment);
  }

  void setTotalPrincipalRepaid() { 
    this.totalPrincipalRepaid = amount; 
  }

  void setTotalInterestRepaid() { 
    this.totalInterestRepaid = totalRepaid - totalPrincipalRepaid; 
  }

  void setTotalInterestPercentage() { 
    totalInterestPercentage = totalInterestRepaid/amount; 
  }

  @Override
  public String toString() { 
    return "Amount: " + amount + "\n" + "Term: " + term + "\n" + "Rate: " + rate + 
            "\n" + "Monthly Payment: " + monthlyPayment + "\n" + "Total Repaid: " + totalRepaid + 
            "\n" + "Total Int Repaid: " + totalInterestRepaid + "\n" + "Total Int Percentage: " + 
            totalInterestPercentage; 
  }

  @Override
  public int compareTo(loanCalculator loan1) {
    // TODO Auto-generated method stub
    if(this.totalInterestPercentage - loan1.totalInterestPercentage > 0) { 
        return 1; 
    }

    if(this.totalInterestPercentage - loan1.totalInterestPercentage < 0) { 
        return -1; 
    }

    else if(this.totalInterestPercentage - loan1.totalInterestPercentage ==0) { 
        return 0; 
    }

    return 0;

  }

  public void difference(loanCalculator loan1) { 

    if(this.compareTo(loan1) == 1) { 
        System.out.print("Loan 2 is cheaper by: " + (this.totalInterestPercentage - loan1.totalInterestPercentage)); 
    }

    if(this.compareTo(loan1) == 0) { 
        System.out.print("The two loans cost the same");
    }

    else if(this.compareTo(loan1) == 1) { 
        System.out.print("Loan 1 is cheaper by: " + (this.totalInterestPercentage - loan1.totalInterestPercentage)); 
    }

  }
}

标签: javacode-organization

解决方案


(从头开始)将 main 方法中的代码放入一个新方法中:

public static void calculator() {
...
}

然后在 main 的循环中调用calculator():

while(true) {
    calculator();
}

如何终止?可能是这样的:

 while(true) {
     try {
        calculator();
     } catch (Exception ex) { //scanner will throw an Exception on not valid input
        break;
     }
 }

推荐阅读