자바

try-catch예제

AIN99 2022. 3. 11. 10:58
728x90

[내가 작성한 코드]

   /*
	 * 이체
	 * @param amount: 이체 금액
	 * @param otherAccount: 이체할 계좌번호
	 * @return 이체 성공시 true 실패시 false
	 */
	public boolean transfer(int amount, BankAccount otherAccount)throws IllegalArgumentException,NullPointerException{
		
		if(amount<0 ||amount>balance)  throw new IllegalArgumentException();
		if(amount<balance) {
		    withdraw(amount);
		    otherAccount.deposit(amount);
		    return true;
	    }else {
		  return false;
	    }		
	
	}

 

[테스트 코드]

public class BankTest {
	
	public static void main(String[] args) {
		CheckingAccount	tonyAccount = new CheckingAccount(3000);
		CheckingAccount steveAccount=new CheckingAccount(4000);
		try {
			tonyAccount.transfer(5000, steveAccount);
			System.out.println("송금완료");
		
	    }catch (NullPointerException e) {
			System.out.println("해당하는 계좌가 없습니다.");
			System.out.println("송금실패");
		} catch (IllegalArgumentException e) {
			System.out.println("해당하는 금액을 보낼 수 없습니다.");
			System.out.println("송금실패");
		}
	
		try {
			tonyAccount.transfer(2000, null);
			System.out.println("송금완료");
		
	    } catch (NullPointerException e) {
			System.out.println("해당하는 계좌가 없습니다.");
			System.out.println("송금실패");
		} catch (IllegalArgumentException e) {
			System.out.println("해당하는 금액을 보낼 수 없습니다.");
			System.out.println("송금실패");
		}
	}
		
}

[다른 정답]

 public boolean transfer(int amount, BankAccount otherAccount) {
		if(amount<0 ||amount>balance)  {
			throw new IllegalArgumentException("해당하는 금액을 보낼 수 없습니다.");
		}
		if(otherAccount==null) {
			throw new NullPointerException("해당하는 계좌가 없습니다.");
		}
		if(withdraw(amount)) {
		    otherAccount.deposit(amount);
		    return true;
	    }else {
		  return false;
	    }		
	
	}

출력을 이렇게 할 수도 있다.

public class BankTest {
	
	public static void main(String[] args) {
		CheckingAccount	tonyAccount = new CheckingAccount(3000);
		CheckingAccount steveAccount=new CheckingAccount(4000);
		try {
			tonyAccount.transfer(5000, steveAccount);
			System.out.println("송금완료");
		
	    }catch (NullPointerException || IllegalArgumentException e) {
			System.out.println(e.getMessage());
			System.out.println("송금실패");
            //코드를 줄이기 위해 한번에 사용될 수 있음!!!
	    }
		try {
			tonyAccount.transfer(2000, null);
			System.out.println("송금완료");
		
	    } catch (NullPointerException e) {
			System.out.println(e.getMessage());
			System.out.println("송금실패");
		} catch (IllegalArgumentException e) {
			System.out.println(e.getMessage());
			System.out.println("송금실패");
		}
	}
		
}

결과는 두개 모두 동일하다.

[번외]

scanner.nextInt(); 개행문자 엔터를 제거하지 않는다. 개행문자(엔터)전까지만 숫자로 입력받는다.

scanner.nextLine(); 를 꼭 삽입해줘야한다.

[삽입을 안해줬을경우]

->str에 엔터가 입력되어진다.

[올바르게 한 경우]

import java.util.Scanner;

public class cheack {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scanner = new Scanner(System.in);
		int num;
		String str;
		
		System.out.print("num 입력:");
		num=scanner.nextInt(); 
		scanner.nextLine(); //이것을 꼭 삽입해줘야 한다.
		
		System.out.print("str입력:");
		str=scanner.nextLine();
		
		System.out.println();
		System.out.println("num:"+num);
		System.out.println("str:"+str);
		scanner.close();

	}
	
}

728x90