자바 객체지향(OOP) 다시 돌아와서 보기(Revisiting Java OOP)
메서드(Method)
int sellApple(int money)
{
int appleNum = money / 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)
}
}