티스토리 뷰

 전체 구조

 

 


 

Customer.java

package com.ktds.smahn;

 

public class Customer {

 

       // 구매자가 가지는 속성은 인스턴트 필드로 만든다.

 

       private int money; //구매자가 가지고 있는 금액

       private int appleCount; //구매자가 가지고 있는 사과의

 

       public void setMoney(int money) {

             this.money = money;

       }

 

       public void setAppleCount(int appleCount) {

             this.appleCount = appleCount;

       }

 

       public int getMoney() {

             return this.money;

             // this 쓰지 않아도 인스턴스 필드를 가리킨다.

       }

 

       public int getAppleCount() {

             return this.appleCount;

       }

 

       /**

        * 돈을 지불한다

        * @param money 지불한

        */

       public int pay(int money) {

             this.money -= money;

             return money;

       }

 

       /**

        * 사과를 받는다

        * @param appleCount 사려고 하는 사과의

        */

       public void getApple(int appleCount) {

             this.appleCount += appleCount;

       }

 

 

       /**

        * 거스름돈을 받는다

        * @param money 거스름돈

        */

       public void takeMoney ( int money ) {

             this.money += money;

       }

      

       /**

        * 정보를 출력한다

        */

       public void printMyInfo() {

             System.out.println("구매자가 가진 사과의 : " + this.appleCount);

             System.out.println("구매자가 가진 : " + this.money);

       }

 

}

 

 


 

Seller.java

 

package com.ktds.smahn;

 

public class Seller {

 

       private int money;

       private int appleCount;

       private int applePrice;

 

       public Seller () {

            

       }

      

       public Seller( int money, int appleCount, int applePrice ) {

             System.out.println("인스턴스가 만들어졌습니다.");

             this.setMoney(money);

             this.setAppleCount(appleCount);

             this.setApplePrice(applePrice);

            

       }

 

       public int getMoney() {

             return money;

       }

 

       public void setMoney(int money) {

             this.money = money;

       }

 

       public int getAppleCount() {

             return appleCount;

       }

 

       public void setAppleCount(int appleCount) {

             this.appleCount = appleCount;

       }

 

       public int getApplePrice() {

             return applePrice;

       }

 

       public void setApplePrice(int applePrice) {

             this.applePrice = applePrice;

       }

 

       public void getMoneyFromCustomer(int money, Customer customer) {

             this.money += customer.pay(money);

       }

 

       public void giveApple(int appleCount, Customer customer) {

             this.appleCount -= appleCount; // 판매자가 가지고 있는 사과의 수가 감소

             customer.getApple(appleCount); // 구매자에게 사과를

       }

 

       public void giveRemain(int money, int appleCount, Customer customer) {

             int remain = money - (appleCount * this.applePrice); //

             this.money -= remain;

             customer.takeMoney(remain);

       }

 

       public void printMyInfo() {

             System.out.println("판매자가 가진 사과의 : " + this.appleCount);

             System.out.println("판매자가 가진 금액 : " + this.money);

       }

 

}

 

 

 


 

 

Emart.java

 

package com.ktds.smahn;

 

public class EMart {

 

       public void start() {

            

             Seller 과일장수1 = new Seller( 50000, 20, 1500 );

             Seller 과일장수2 = new Seller( 5000, 1, 5000 );

             Seller 과일장수3 = new Seller();

             과일장수3.setAppleCount(10);

 

            

             System.out.println(과일장수1);

            

             Customer 손님 = new Customer();

             손님.setMoney(60000);

             손님.setAppleCount(0);

            

             과일장수1.getMoneyFromCustomer(3000, 손님);

             과일장수1.printMyInfo();

             손님.printMyInfo();

            

             과일장수1.giveApple(1, 손님);

             과일장수1.printMyInfo();

             손님.printMyInfo();

            

             과일장수1.giveRemain(3000, 1, 손님);

             과일장수1.printMyInfo();

             손님.printMyInfo();

 

             System.out.println("----------------------------------------------------------");

             과일장수2.getMoneyFromCustomer(5000, 손님);

             과일장수2.printMyInfo();

             손님.printMyInfo();

            

             과일장수2.giveApple(1, 손님);

             과일장수2.printMyInfo();

             손님.printMyInfo();

            

             과일장수2.giveRemain(5000, 1, 손님);

             과일장수2.printMyInfo();

             손님.printMyInfo();

       }

       public static void main(String[] args) {

            

             EMart mart = new EMart();

             mart.start();

            

       }

}

 

'프로그래밍 > Java' 카테고리의 다른 글

예외처리  (1) 2016.01.25
컬렉션 프레임워크(List, HashMap, Iterator)  (0) 2016.01.22
클래스와 메소드  (0) 2016.01.22
[실습문제] 사칙연산 계산기 프로그램  (0) 2016.01.21
클래스, final  (0) 2016.01.20
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함