[자바 요약정리] 접근제어자를 이용한 은행 프로그램


public class Main {

    public static void main(String[] args) {

    Person p1 = new Person();

                p1.setName("김신의");

                p1.setAge(28);

                p1.setCashAmount(30000);


                Person p2 = new Person();

                p2.setName("마이클");

                p2.setAge(70);

                p2.setCashAmount(100000);


                // 은행 계좌 생성

                BankAccount a1 = new BankAccount();

                a1.setBalance(10000);


                BankAccount a2 = new BankAccount();

                a2.setBalance(500000);


                p1.account = a1;

                a1.owner = p1;


                p2.account = a2;

                a2.owner = p2;


                // 3 - 4. write code here

                p2.account.deposit(30000);

                p2.account.withdraw(170000);

                p2.account.deposit(620000);

                p2.account.withdraw(890000);

      

    }

}





public class Person {

    private String name;

    private int age;

    private int cashAmount;

    BankAccount account;


    public void setName(String newName) {

        name = newName;

    }


    public void setAge(int newAge) {

        age = newAge;

    }


    public void setCashAmount(int newCashAmount) {

        cashAmount = newCashAmount;

    }


    public String getName() {

        return name;

    }


    public int getAge() {

        return age;

    }


    public int getCashAmount() {

        return cashAmount;

    }

    

}






public class BankAccount {

    

      private int balance;

        Person owner;


        public void setBalance(int newBalance){

           balance = newBalance;

       }


      public int getBalance(){

           return balance;

      }

        // 파라미터 : 입금할 액수(정수)

        // 리턴 : 성공여부(불린)


        boolean deposit(int amount) {

        // 1. write code here

        boolean result = true;

        balance=balance+amount;

        owner.setCashAmount(owner.getCashAmount()-amount);

        if(balance>=0 && owner.getCashAmount()>=0){


        System.out.printf("%d원 입금하였습니다. 잔고: %d원, 현금: %d원\n", amount,balance,owner.getCashAmount());

        System.out.println(result);

        return result;

        }

        else

        {

        result = false;

        balance=balance-amount;

            owner.setCashAmount(owner.getCashAmount()+amount);

        System.out.printf("입금 실패입니다. 잔고: %d원, 현금: %d원\n", balance, owner.getCashAmount());

        System.out.println(result);

        return result;


        }

        }


        // 파라미터 : 출금할 액수(정수)

        // 리턴 : 성공여부(불린)

        boolean withdraw(int amount) {

        // 2. write code here

        boolean result = true;


        balance=balance-amount;

        owner.setCashAmount(owner.getCashAmount()+amount);

        if(balance>=0 && owner.getCashAmount()>=0){

        System.out.printf("%d원 출금하였습니다. 잔고: %d원, 현금: %d원\n", amount,balance,owner.getCashAmount());

        System.out.println(result);

        return result;

        }

        else

        {

        result = false;

        balance=balance+amount;

            owner.setCashAmount(owner.getCashAmount()-amount);

        System.out.printf("출금 실패입니다. 잔고: %d원, 현금: %d원\n", balance, owner.getCashAmount());

        System.out.println(result);

        return result;


        }

        }

}

이 글을 공유하기

댓글

Designed by JB FACTORY