자바 스터디 노트 #5

자바 객체지향(OOP) 다시 돌아와서 보기(Revisiting Java OOP)

메서드(Method)

int sellApple(int money) // 사과 구매액이 메소드의 인자(argument)로 전달
{
  int appleNum = money / 1000; // 사과가 개당 1000원이라고 가정
  totalApples -= appleNum; // 총 사과의 수가 줄어든다
  balance += money; // 총액에 건내받은 돈이 추가된다
  return appleNum; // 팔은 사과의 갯수를 반환한다
}

클래스(Class)

과일 판매자(FruitSalesman) 클래스

class FruitSalesman
{
  final int APPLE_PRICE = 1000;
  int totalApples = 20;
  int totalBalance = 0;

  public int sellApple(int moneyTaken)
  {
    int appleNum = moneyTaken / APPLE_PRICE;
    totalApples -= appleNum;
    totalBalance += moneyTaken;
    return appleNum;
  }
  public void showSalesResult()
  {
    System.out.println("Total Apples left: "+totalApples)
    System.out.println("Total Balance: "+totalBalance)
  }
}

과일 구매자, 손님(Customer) 클래스

class Customer
{
  int myMoney = 5000;
  int applesOwned = 0;

  public void purchaseAppleFrom(FruitSalesman seller, int moneyTaken)
  {
    applesOwned += seller.sellApple(moneyTaken);
    myMoney -= moneyTaken;
  }
  public void showPurchaseResult()
  {
    System.out.println("현재 잔액: "+myMoney)
    System.out.println("사과 갯수: "+applesOwned)
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *