본문 바로가기

2023.11.21-2024.05.31

231214 JAVA Apple

package apple

class Apple

package apple;

//두 인스턴스가 발생하는 장소

public class Apple {

public static void main(String[] args) {

 

AppleSeller seller = new AppleSeller();

AppleBuyer buyer = new AppleBuyer();

 

buyer.buyApple(seller,5000);

seller.tellSeller();

buyer.tellBuyer();

 

 

}

 

}

 

package apple

class AppleBuyer

package apple;

 

//사과를 사는 사람

public class AppleBuyer {

// 속성

// 필요한 정보?

// 가지고 있는 사과의 수

int appleCount;// =0개 와 같다

// 보유 금액

int buyerMoney = 10000;

 

// 생성자

 

// 기능

// 사과를 사는 기능

public void buyApple(AppleSeller seller, int money) {

int num = seller.sellApple(money);// 제일 어려운 부분임댜

 

//내 사과에 더해주시고

appleCount += num;

//내 돈에서 빼주시면 됩니다.

buyerMoney -= money;

}

 

// 내 정보를 말하는 기능

public void tellBuyer() {

System.out.println("구매한 사과는" + appleCount + " 개 입니다.");

System.out.println("남은 돈은" + buyerMoney + "원 입니다.");

}

// 메소드 명은 tellBuyer

// "구매한 사과는 + appleCount + " 개 입니다."

// "남은 돈은"+ buyerMoney+"원 입니다."

 

}

 

 

 

package apple

class AppleSeller

package apple;

 

//사과를 파는 사람.

public class AppleSeller {

// 속성

// 필요한 정보

// 사과 가격 apple price -> 상수

final int APPLE_PRICE = 1000;

// 사과 수

int appleCount = 50;

// 보유 금액

int sellerMoney;// =0;

//

 

// 생성자

 

// 기능

// 사과를 파는 기능

public int sellApple(int money) {

int num = money / APPLE_PRICE;//num : 내가 산 사과

//받은 돈 -> 내돈에 저장

sellerMoney += money;// sellerMoney=sellerMoney+moey;

//줄 사과 -> 내 사과에서 빼기

appleCount = appleCount- num; //appleCount-=num;

return num;

}

 

// 내 정보를 말하는 기능

public void tellSeller() {

System.out.println("남은 사과는" + appleCount + " 개 입니다.");

System.out.println("수익은" + sellerMoney + "원 입니다.");

 

}

// 메소드 명은 tellSeller

// "남은 사과는 + appleCount + " 개 입니다."

// "수익은"+ sellerMoney+"원 입니다."

}

'2023.11.21-2024.05.31' 카테고리의 다른 글

231215 JAVA Human  (0) 2023.12.15
231215 JAVA Constructor01(생성자)  (0) 2023.12.15
231214 JAVA dec14; OOP2  (0) 2023.12.15
231214 JAVA OOP3  (0) 2023.12.15
231214 JAVA OOP2  (0) 2023.12.15