java - 由用户结束或重新启动的程序的良好班级组织
问题描述
我编写了一个小程序,它根据用户在控制台中输入的贷款金额、利率和期限来计算总利息、总利息百分比和其他指标。我的问题是:我希望程序比较用户想要输入的贷款的成本。那么,当用户输入他们想要测试另一笔贷款时,让我的所有方法重新运行的最佳方法是什么?我应该使用方法链接吗?我应该有一个不同的班级来管理这部分程序吗?提前致谢。贷款计划的代码如下。
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));
}
}
}
解决方案
(从头开始)将 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;
}
}
推荐阅读
- c# - Foreach-loop 循环太多次并请求其他模型
- elasticsearch - 在 ES 中运行 Explain API 时没有得到解释结果
- c++ - 循环的声明范围
- php - cart api 仅获取产品的一个详细信息,但我想要每个产品的详细信息(如果存在)
- python - 来自数据立方体的多个像素的平均光谱
- c# - 如何在运行时在窗体中显示继承自 Picturebox 和其他类的实例?
- ios - 如何解决多个命令产生编译错误
- sql - 在 PostgreSQL 中旋转表
- android - Keystore 中的 Android 加密密钥 - 将应用程序移动到新设备
- android - 片段更新文本更改