0

普通預金と普通預金の 2 つの口座を保持する小さな銀行プログラムを作成する任務があります。しかし、大まかなバージョンを完成させようとしていたとき、プロンプトで、私が病気だった日について学んだことを例外として使用することになっていることを読みました.

例外の使用方法や、独自の例外クラスを作成する必要があるかどうかについては明確にしません。プログラムの任意の時点で「q」または「Q」で始まる何かが入力された場合にプログラムが終了して終了するように、例外が必要です。もう 1 つの規定は、貯蓄が 25 ドルを下回った場合にアカウントを凍結するというものでした。その機能には例外が理想的だと思います。

public abstract class BankAccount {

    int balance;
    int deposits;
    int withdrawals;
    int annualInterestRate;
    int charges;

    public void _BankAccount(int newBalance, int newInterest) {
        balance = newBalance;
        annualInterestRate = newInterest;
    }

    public void Deposit(int newDeposit) {
        balance = balance + newDeposit;
        deposits++;
    }

    public void Withdraw(int newWithdraw) {
        balance = balance - newWithdraw;
        withdrawals++;
    }

    public void calcInterest() {
        int monthlyInterestRate = (annualInterestRate / 12);
        int monthlyInterest = balance * monthlyInterestRate;
        balance = balance + monthlyInterest;
    }

    public void monthlyProcess() {
        balance = balance - charges;
        calcInterest();
        deposits = 0;
        withdrawals = 0;
    }
}

public class SavingsAccount extends BankAccount {

    boolean status;

    public void savingWithdraw(int newWithdraw) {
        if (balance < 25) {
            System.out.println("Error – Not enough funds.");
        } else {
            Withdraw(newWithdraw);
        }
    }

    public void savingDeposit(int newDeposit) {
        if (balance < 25) {
            Deposit(newDeposit);
            System.out.println("Savings account is now active.");
        } else {
            Deposit(newDeposit);
        }
    }

    public void savingMonthlyProcess() {
        if (withdrawals > 4) {
            charges = ((withdrawals - 4) * 1);
            balance = balance - ((withdrawals - 4) * 1);
            if (balance < 25) {
                System.out.println("Savings account is now inactive.");
            }
        }
    }
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String choice;
    int num = 0;
    boolean quit = false;
    do {
        System.out.println("Which account would you like to access, regular or savings?:");
        choice = in.nextLine();
        if (choice.equals("regular")) {
            num = 0;
        }
        if (choice.equals("savings")) {
            num = 1;
        }
        switch (num) {
            case 0:
                System.out.println("What action do you wish to perform \n(Withdraw, deposit, monthly processing)?:");
                choice = in.nextLine();
                if (choice.equals("withdraw")) {
                    num = 0;
                }
                if (choice.equals("deposit")) {
                    num = 1;
                }
                if (choice.equals("monthly processing")) {
                    num = 2;
                }
                switch (num) {
                    case 0:
                        System.out.println("Enter amount to withdraw:");
                        Withdraw(in.nextInt());
                    case 1:
                        System.out.println("Enter amount to withdraw:");
                        Deposit(in.nextInt());
                    case 2:
                        MonthlyProcess();
                }
            case 1:
                System.out.println("What action do you wish to perform \n(Withdraw, deposit, monthly processing)?:");
                choice = in.nextLine();
                if (choice.equals("withdraw")) {
                    num = 0;
                }
                if (choice.equals("deposit")) {
                    num = 1;
                }
                if (choice.equals("monthly processing")) {
                    num = 2;
                }
                switch (num) {
                    case 0:
                        System.out.println("Enter amount to withdraw:");
                        savingsWithdraw(in.nextInt());
                    case 1:
                        System.out.println("Enter amount to withdraw:");
                        savingsDeposit(in.nextInt());
                    case 2:
                        savingMonthlyProcess();
                }

        }
    }


}

}

4

1 に答える 1

0

例外は、望ましくない状況を処理するために使用されます。あなたの場合、組み込みの例外クラスを使用して、ランタイム例外のほとんどを処理できますが、カスタム メッセージを使用できます。

もう 1 つの規定は、貯蓄が 25 ドルを下回った場合にアカウントを凍結するというものでした。

上記の条件の場合、新しい例外クラスを作成し、この例外をユーザー定義のエラー メッセージで処理することができます。

java での例外処理の詳細については、こちらを参照してください。

于 2013-11-21T01:43:40.727 に答える