-4

コードを数回調べましたが、これに何が影響していて、デフォルトのコンストラクターのみを使用するように強制されているのかわかりません。たとえば、投資額を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;
    }
}
4

2 に答える 2

4

「デフォルトのコンストラクターを使用するように強制する」ことは何もありません。デフォルトのコンストラクターを呼び出すだけです

Investment otherClass = new Investment()

2引数のコンストラクターを使用するには、引数を渡します

new Investment(2000.0D, 5.0D)
于 2013-02-18T02:10:23.073 に答える
0

私は次の行を推測します:

public final static double AMOUNT_DEFAULT = 1000;
public Investment(double AMOUNT_DEFAULT, double YEARS_DEFAULT)

問題です。AMOUNT_DEFAULT = 1000を宣言し、それをfinalとして宣言しています。したがって、常に1000になります。コンストラクターで別の変数名を使用してみてください。

編集:私は興味を持ってこれをすばやくテストしましたが、コードは実際には意図したとおりに機能するようです。ただし、そのような変数の命名を避けることは、依然としてあなたの利益になります。

于 2013-02-18T02:11:48.420 に答える