class FruitSeller { private int numOfApple; private int myMoney; private final int APPLE_PRICE;
public FruitSeller(int money, int appleNum, int price) { myMoney=money; numOfApple=appleNum; APPLE_PRICE=price; }
public int saleApple(int money) { int num=money/APPLE_PRICE; numOfApple-=num; myMoney+=money; return num; }
public void showSaleResult() { System.out.println("³²Àº »ç°ú : "+numOfApple); System.out.println("ÆǸŠ¼öÀÍ : "+myMoney); } }
-------------------------------------------------------------
class FruitBuyer { private int myMoney; private int numOfApple; public FruitBuyer(int money) { myMoney=money; numOfApple=0; }
public void buyApple(FruitSeller seller, int money) { numOfApple+=seller.saleApple(money); myMoney-=money; } public void showBuyResult() { System.out.println("ÇöÀç ÀÜ¾× : "+myMoney); System.out.println("»ç°ú °³¼ö : "+numOfApple); } }
------------------------------------------------------------- class FruitSalesMain5 { public static void main(String[] args) { FruitSeller seller=new FruitSeller(0, 30, 1500); FruitBuyer buyer = new FruitBuyer(10000);
buyer.buyApple(seller, 4500);
System.out.println("°úÀÏ ÆǸÅÀÚÀÇ ÇöÀç »óȲ"); seller.showSaleResult();
System.out.println("°úÀÏ ±¸¸ÅÀÚÀÇ ÇöÀç »óȲ"); buyer.showBuyResult();
} }
À§ÀÇ -------------------------------------------------------------Ç¥½Ã´Â Á¦°¡ Ŭ·¡½ºÇ¥½Ã¶§¹®¿¡ ÀÓÀÇ·Î ±Û¾²¸é¼ ³ÖÀº°Å¿¡¿ä.
private ¼±¾ðÀº ±× º¯¼ö°¡ ¼±¾ðµÈ Ŭ·¡½º ¹Û¿¡¼± Á¢±ÙÀ» ¸øÇÑ´Ù°í ¹è¿ü½À´Ï´Ù.
±Ùµ¥ FruitSalesMain5Ŭ·¡½º¿¡¼± buyer.Apple(seller, 4500);À» ÅëÇؼ FruitBuyerÀÇ º¯¼ö numOfApple°ú FruitSeller Ŭ·¡½ºÀÇ º¯¼öµéÀ» º¯È½ÃÅ°Àݾƿä?
±Ùµ¥ Ã¥¿¡¼± ÀÌ·¸°Ô ÇÏ´Â°Ô ¸Â´Ù°í Çϴµ¥ ÀÌÇØ°¡ Àß ¾ÈµË´Ï´Ù. |
°á±¹ buyer.buyApple Àº FruitBuyer ÀÇ buyApple ¸Þ¼Òµå¸¦ ¾È¿¡¼ È£ÃâÇÑ°Ô µË´Ï´Ù. ¶§¹®¿¡ FruitBuyer Ŭ·¡½º ³»¿¡¼
»ç¿ëÇÏ´Â private º¯¼öµµ »ç¿ë °¡´ÉÇѰű¸¿ä.