需要为负数显示 "Insufficient Funds"

Need to show "Insufficient Funds" for negative amounts

当用户提款超过 initialAccountBalance 中的金额(等于 500.00)时,我需要为 "insufficient Funds" 抛出异常。但是,我不确定在哪里放置例外。

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    double initialAccountBalance = 500.00;

    System.out.print("Enter a transaction type (Balance, Deposit, or Withdrawal): ");
    String transactionType = in.nextLine();

    if (transactionType.equalsIgnoreCase("Balance")){
        System.out.println("Balance " +initialAccountBalance);
        System.out.println();

    } else if (transactionType.equalsIgnoreCase("Deposit")){
        System.out.println("Enter deposit: ");
        int deposit = in.nextInt();
        double balance = initialAccountBalance + deposit;
        System.out.printf("Account Balance: %8.2f", balance);

    } else if(transactionType.equalsIgnoreCase("Withdrawal")){
        System.out.println("Enter withdrawal: ");
        int withdrawal = in.nextInt();
        double balance = initialAccountBalance - withdrawal;
        System.out.printf("Account Balance: %8.2f", balance);

    } else {
        System.out.println("Invalid transaction type");
    }
}
...
} else if (transactionType.equalsIgnoreCase("Withdrawal")){
    System.out.println("Enter withdrawal: ");
    int withdrawal = in.nextInt();

    if (withdrawal > initialAccountBalance) {
        throw new InsufficientFundsException("Insufficient Funds");
    }

    double balance = initialAccountBalance - withdrawal;
    System.out.printf("Account Balance: %8.2f", balance);

}
...

你的InsufficientFundsException应该是这样的:

public class InsufficientFundsException extends Exception {

    private static final long serialVersionUID = -1L;

    public InsufficientFundsException() {
    }

    public InsufficientFundsException(String message) {
        super(message);
    }
}

您可以在用户输入提款金额后立即放置一个 if,如下所示:

//...
System.out.println("Enter withdrawal: ");
int withdrawal = in.nextInt();
if (withdrawal > initialAccountBalance)
    throw new RuntimeException("Insufficient Funds");
double balance = initialAccountBalance - withdrawal;
System.out.printf("Account Balance: %8.2f", balance);
//...

如果你想抛出自己的异常,你必须创建一个新的 class 来扩展 Exception,就像评论中提到的@chenchuk。

public class InsufficientFundsException extends Exception{
    //if you need to you can Overwrite any method of Exception here
}

所以你可以在你的 main 方法中抛出 InsufficientFundsException:

//...
System.out.println("Enter withdrawal: ");
int withdrawal = in.nextInt();
if (withdrawal > initialAccountBalance)
    throw new InsufficientFundsException();
double balance = initialAccountBalance - withdrawal;
System.out.printf("Account Balance: %8.2f", balance);
//...

但是您必须通过使用 try-catch 块或向方法头添加 throws 声明来提供某种处理异常的方法。 但是你应该查一下,这里要深入解释太多了...

希望对您有所帮助(:

 public void withdrawal() {  
        long amt;  
        System.out.println("Enter the amount you want to withdraw: ");  
        amt = sc.nextLong();  
        if (balance >= amt) {  
            balance = balance - amt;  
            System.out.println("Balance after withdrawal: " + balance);  
        } else {  
            System.out.println("Your balance is less than " + amt + "\tTransaction failed...!!" );  
        }  
    }