Java
[JAVA] 간단하게 구현한 계좌시뮬레이션
bkuk
2022. 9. 13. 01:57
전체 코드
package javaCOde;
public class accountSimulation {
private int accountInfo = 123456789;
private int currentBlance = 1234;
private int amount;
public int CubSubTractAmt = currentBlance + amount;
public void printAccountInfo() {
System.out.println("고객님의 계좌번호는 " + accountInfo + "입니다.\n");
}
public void printcurrentBlance() {
System.out.println("고객님 계좌의 현재 금액은 " + currentBlance + "원 입니다.\n");
}
public void printDeposit(int amount) {
System.out.println("고객님께서 " + amount +"원을 입금하셨습니다.");
System.out.println("고객님 계좌의 현재 금액은 " + (currentBlance + amount) +"입니다.\n");
}
public void withdraw(int amount) {
if(amount > currentBlance)
System.out.println("고객님의 현재 계좌에서 " +(amount - currentBlance ) + "원이 부족합니다.");
else {
System.out.println(amount + "원을 출금하셨습니다.");
System.out.println("고객님의 현재 계좌 금액은 " + (currentBlance - amount) + "입니다.");
currentBlance -=amount;
}
}
public static void main(String[] args) {
accountSimulation ac = new accountSimulation();
ac.printAccountInfo();
ac.printcurrentBlance();
ac.printDeposit(1000);
ac.withdraw(1000);
}
}
발생한 오류와 해결방법
1-1) 발생한 오류
public void printDeposit(int amount) {
System.out.println("고객님께서 " + amount +"원을 입금하셨습니다.");
System.out.println("고객님 계좌의 현재 금액은 " + currentBlance + amount +"입니다.\n");
}
public static void main(String[] args) {
accountSimulation ac = new accountSimulation();
ac.printDeposit(1000);
}
산술이 아닌 문자열의 결합의 결과가 출력됨.
1-2) 예상한 결과
고객님께서 1000원을 입금하셨습니다. 고객님 계좌의 현재 금액은 2234입니다. |
2) 해결방법
연산자 우선순위 표를 확인하여 실행문을 수정
- ()
- 단항 연산자(--, ++, !)
- 산술 연산자( *, /, %, +, -)
- 비교 연산자( >, >=, <. <=, ...)
- 논리 연산자( &&, ||)
- 대입 연산자( =, +=, ...)
a) 수정 전 실행문
System.out.println("고객님 계좌의 현재 금액은 " + currentBlance + amount +"입니다.\n");
b) 수정 후 실행문
System.out.println("고객님 계좌의 현재 금액은 " + (currentBlance + amount) +"입니다.\n");