상세 컨텐츠

λ³Έλ¬Έ 제λͺ©

6.3. λ³€μˆ˜ μΆ”μΆœν•˜κΈ°

🍜개발자라면/책을 읽자✍

by :ν•΄ν”Όλž˜λΉ—πŸΎ 2021. 9. 26. 14:36

λ³Έλ¬Έ

- ν‘œν˜„μ‹μ΄ λ„ˆλ¬΄ λ³΅μž‘ν•΄μ„œ μ΄ν•΄ν•˜κΈ° μ–΄λ €μšΈ λ•Œ

 

ν‘œν˜„μ‹μ— 이름을 뢙이고 μ‹Άλ‹€.
ν˜„μž¬ ν•¨μˆ˜ μ•ˆμ—μ„œλ§Œ μ˜λ―Έκ°€ μžˆλ‹€? : λ³€μˆ˜λ‘œ μΆ”μΆœ
ν•¨μˆ˜λ₯Ό λ²—μ–΄λ‚œ 넓은 λ¬Έλ§₯μ—μ„œκΉŒμ§€ μ˜λ―Έκ°€ μžˆλ‹€? : ν•¨μˆ˜λ‘œ μΆ”μΆœ_ μ§ˆμ˜ν•¨μˆ˜λ‘œμ„œ μ‚¬μš©

방법

1. μΆ”μΆœν•˜λ €λŠ” ν‘œν˜„μ‹μ— λΆ€μž‘μš©μ€ μ—†λŠ”μ§€ ν™•μΈν•œλ‹€.

2. λΆˆλ³€ λ³€μˆ˜λ₯Ό ν•˜λ‚˜ μ„ μ–Έν•˜κ³  이름을 뢙일 ν‘œν˜„μ‹μ˜ λ³΅μ œλ³Έμ„ λŒ€μž…ν•œλ‹€.

3. 원본 ν‘œν˜„μ‹μ„ μƒˆλ‘œ λ§Œλ“  λ³€μˆ˜λ‘œ κ΅μ²΄ν•œλ‹€.

4. ν…ŒμŠ€νŠΈν•œλ‹€.

5. ν‘œν˜„μ‹μ„ μ—¬λŸ¬ κ³³μ—μ„œ μ‚¬μš©ν•œλ‹€λ©΄ 각각을 μƒˆλ‘œ λ§Œλ“  λ³€μˆ˜λ‘œ κ΅μ²΄ν•˜κ³ , κ·Έλ•Œλ§ˆλ‹€ ν…ŒμŠ€νŠΈν•œλ‹€.


  λ³€μˆ˜ μΆ”μΆœν•˜κΈ°  

ν˜„μž¬ ν•¨μˆ˜ μ•ˆμ—λ§Œ μ˜λ―Έκ°€ μžˆλ‹€ -> λ³€μˆ˜λ‘œ μΆ”μΆœ

 

// λ³€κ²½μ „
function price(order){
    // 가격(price) = κΈ°λ³Έ 가격 - μˆ˜λŸ‰ 할인 + 배솑비
    return order.quantity*order.itemPrice 
       - Math.max(0,order.quantity-500)*order.itemPrice*0.05
       + Math.min(order.quantity*order.itemPrice*0.1,100);
}

// λ³€κ²½ν›„
function price(order){
    const basePrice = order.quantity*order.itemPrice;
    const quantityDiscount = Math.max(0, order.quantity-500)*order.itemPrice*0.05;
    const shipping = Math.min(basePrice*0.1,100)
    return basePrice - quantityDiscount + shipping;
}

  클래슀 μ•ˆμ—μ„œ μΆ”μΆœν•˜κΈ°  

price() λ©”μ„œλ“œμ˜ λ²”μœ„λ₯Ό λ„˜μ–΄ 주문을 ν‘œν˜„ν•˜λŠ” Order 클래슀 전체에 적용
/* 
	이름이 가격을 κ³„μ‚°ν•˜λŠ” price()의 λ©”μ„œλ“œ λ²”μœ„λ₯Ό λ„˜μ–΄, 
	주문을 ν‘œν˜„ν•˜λŠ” order 클래슀 전체에 μ μš©λœλ‹€.
*/
class Order {
    constructor(aRecord){
       this._data = aRecord;
    }
    get quantity() {return this._data.quantity;}
    get itemPrice() {return this._data.itemPrice;}
    get price(){
        return this.quantity*this.itemPrice 
        - Math.max(0, this.quantity-500)*this.itemPrice*0.05
        + Math.min(this.quantity*this.itemPrice*0.1, 100);
    }
}

// λ³€κ²½ν›„ ==> ν•¨μˆ˜λ‘œ μΆ”μΆœν•˜κ³  μ§ˆμ˜ν•¨μˆ˜λ‘œμ„œ μ‚¬μš©ν•œλ‹€.
class Order {
    constructor(aRecord){
       this._data = aRecord;
    }
    get quantity() {return this._data.quantity;}
    get itemPrice() {return this._data.itemPrice;}
    get price(){
        return this.basePrice - this.quantityDiscount + this.shipping ;
    }
    get basePrice(){return this.quantity*this.itemPrice}
    get quantityDiscount(){return Math.max(0, this.quantity-500)*this.itemPrice*0.05}
    get shipping(){return Math.min(this.quantity*this.itemPrice*0.1, 100)}
}

 

 

 

728x90

κ΄€λ ¨κΈ€ 더보기