개발
계좌 관리 프로그램
오승미
2021. 5. 10. 14:25
// 계좌 저장을 테스트하기위한 코드
class MethodExample1 {
public static void main(String args[]) {
Account obj1 = new Account("111-222-33333333", "김영식" , 200000);
Account obj2 = new Account("555-666-77777777", "박진희" , 1000000);
obj1.deposit(1000000);
obj2.withdraw(200000);
printAccount(obj1);
printAccount(obj2);
}
static void printAccount(Account obj) {
System.out.println("계좌번호: " + obj.getAccountNo());
System.out.println("예금주 이름: " + obj.getOwnerName());
System.out.println("잔액: " + obj.getBalance());
System.out.println(); // 줄 바꿈 문자 출력
}
}
// 계좌 저장 코드
class Account {
private String accountNo; // 계좌번호
private String ownerName; // 예금주 이름
private int balance; // 잔액
Account(String accountNo, String ownerName, int balance) { // 생성자
this.accountNo = accountNo;
this.ownerName = ownerName;
this.balance = balance;
}
public String getAccountNo()
{
return accountNo;
}
public void setAccountNo(String accountNo)
{
this.accountNo = accountNo;
}
public String getOwnerName()
{
return ownerName;
}
public void setOwnerName(String ownerName)
{
this.ownerName = ownerName;
}
public int getBalance()
{
return balance;
}
public void setBalance(int balance)
{
this.balance = balance;
}
void deposit (int amount) { // "예금한다" 기능을 구현하는 메소드 선언
balance += amount;
}
int withdraw(int amount) { // "인출한다" 기능을 구현하는 메소드 선언
if (balance < amount)
return 0;
balance -= amount;
return amount;
}
}