我已经查看了几次代码,但我不确定是什么影响了它并强制它只使用默认构造函数。例如,如果我尝试输入 2000 作为投资金额,它仍将默认为 1000。
public class Investment {
private double moneyInvested;
private double yearsInvested;
public final static double AMOUNT_DEFAULT = 1000;
public final static double YEARS_DEFAULT = 5;
public final static double RATE = 0.12;
public Investment() {
moneyInvested = AMOUNT_DEFAULT;
yearsInvested = YEARS_DEFAULT;
}
public Investment(double AMOUNT_DEFAULT, double YEARS_DEFAULT) {
if (moneyInvested <= 0) {
moneyInvested = AMOUNT_DEFAULT;
}
if (yearsInvested <= 0) {
yearsInvested = YEARS_DEFAULT;
}
}
public double getMoneyInvested() {
return moneyInvested;
}
public double getYearsInvested() {
return yearsInvested;
}
public void setMoneyInvested(double inputMoney) {
moneyInvested = inputMoney;
if (inputMoney <= 0) {
inputMoney = 1000;
}
}
public void setYearsInvested(double inputYears) {
yearsInvested = inputYears;
if (inputYears <= 0) {
inputYears = 1;
}
}
public static String returnValue(double inputYears, double inputMoney) {
double returnInvestment;
int initYears = 1;
String returnValue = "";
while (initYears <= inputYears) {
returnInvestment = Math.pow(1.12, initYears) * inputMoney;
int investReturn = (int) returnInvestment;
returnValue = "The amount at end of year " + initYears + " is "
+ investReturn;
JOptionPane.showMessageDialog(null, returnValue);
initYears++;
}
return returnValue;
}
}
public class MakeInvestment {
public static void main(String[] args) {
Investment otherClass = new Investment();
double yA = otherClass.YEARS_DEFAULT;
double mA = otherClass.AMOUNT_DEFAULT;
while (inputAmount() == false) {
inputAmount();
}
while (inputYears() == false) {
inputYears();
}
otherClass.returnValue(yA, mA);
}
public static boolean inputAmount() {
String amountAmount = "";
amountAmount = JOptionPane.showInputDialog(null,
"Enter the amount to invest (9999).", "Investment Amount",
JOptionPane.QUESTION_MESSAGE);
if (amountAmount == null || amountAmount.length() == 0) {
JOptionPane
.showMessageDialog(
null,
"Nothing entered - You must enter a number for amount invested.",
"Investment Amount Error",
JOptionPane.ERROR_MESSAGE);
return false;
}
for (int i = 0; i < amountAmount.length(); i++) {
if (!Character.isDigit(amountAmount.charAt(i))) {
JOptionPane.showMessageDialog(null,
"You must enter a number for amount invested.",
"Investment Amount Error", JOptionPane.ERROR_MESSAGE);
return false;
}
}
double dblAmount = Double.parseDouble(amountAmount);
return true;
}
public static boolean inputYears() {
String yearAmount = "";
yearAmount = JOptionPane.showInputDialog(null,
"Enter the number of years to invest.", "Investment Years",
JOptionPane.QUESTION_MESSAGE);
if (yearAmount == null || yearAmount.length() == 0) {
JOptionPane
.showMessageDialog(
null,
"Nothing entered - You must enter a number for years to invest.",
"Investment Years Error", JOptionPane.ERROR_MESSAGE);
return false;
}
for (int i = 0; i < yearAmount.length(); i++) {
if (!Character.isDigit(yearAmount.charAt(i))) {
JOptionPane.showMessageDialog(null,
"You must enter a number of years to invest.",
"Investment Years Error", JOptionPane.ERROR_MESSAGE);
return false;
}
}
double dblYear = Double.parseDouble(yearAmount);
return true;
}
}