2013-11-21 3 views
0

정규 및 저축의 두 가지 계정을 유지하는 작은 은행 프로그램을 만들도록 지정했습니다. 그러나 대략적인 버전을 완성하려고 할 때, 나는 내가 아팠던 날에 대해 배웠던 것을 사용하라는 메시지를 읽었습니다.atm 프로젝트에 예외 구현

예외 사용 방법이나 자체 예외 클래스를 만들어야하는지에 대해서는 명확하지 않습니다. 프로그램의 어느 지점에서나 'q'또는 'Q'로 시작하는 무언가가 입력되면 프로그램이 종료되고 종료되도록 예외가 필요합니다. 또 다른 규정은 예금이 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(); 
       } 

     } 
    } 


} 

}

+0

''예외가있어서 'q'또는 'Q'로 시작하는 프로그램이 종료되면 프로그램이 종료되고 종료됩니다. "- 사용하지 않을지는 확실하지 않습니다. 이 동작에 대한 예외입니다. 대신 숫자가 예상되는 곳에서 숫자가 아닌 입력을 입력하거나 사용자가 계정보다 많은 금액을 인출하려고하면 예외를 사용합니다. –

+0

일반적으로 예외는 비정규적인 비표준 동작을 위해 예약되어 있습니다. 'Q'를 누르는 것이 의도 된 프로그램 종료 방법 인 경우 예외를 통해 처리하면 안됩니다. 예금 금액 등으로'$ 34aby53'을 입력하는 것과 같은 것에 대해서는 예외를 처리해야합니다. 또는'Deposit' 및'Withdraw' 메소드에 음수 값을 전송하는 경우'- $ 25' 입금은 '$ 25' 입금이되어야하므로, – nhgrif

+0

먼저 스위치 케이스에 ** break; **를 추가해야합니다. 그리고 계속해라. – MouseLearnJava

답변

0

예외가 요구되지 않는 상황을 처리하는 데 사용됩니다. 귀하의 경우, inbuilt 예외 클래스를 사용하여 대부분의 런타임 예외를 처리 할 수 ​​있지만 사용자 정의 메시지를 처리 ​​할 수 ​​있습니다.

또 다른 규정은 저축이 계좌 동결로 25 달러 미만인 경우입니다.

위의 조건에서 새 예외 클래스를 만들고 사용자 정의 오류 메시지로이 예외를 처리 할 수 ​​있습니다.

Exception handling in java에 대한 자세한 내용을 볼 수 있습니다.