Split Phase, ๋‹จ๊ณ„ ์ชผ๊ฐœ๊ธฐ๋ฅผ ์•Œ์•„๋ณด์ž.

์š”์•ฝ

์ฝ”๋“œ

const orderData = orderString.split(/\s+/);
const productPrice = priceList[orderData[0].split("-")[1]];
const orderPrice = parseInt(orderData[1]) * productPrice;
const orderRecord = parseOrder(order);
const orderPrice = price(orderRecord, priceList);
 
function parseOrder(aString) {
    const values = aString.split(/\s+/);
    return ({
        productID: values[0].split("-")[1],
        quantity: parseInt(values[1])
    });
}
 
function price(order, priceList) {
    return order.quantity * priceList[order.productID];
}

๋ฐฐ๊ฒฝ

  • ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ๋Œ€์ƒ์„ ํ•œ๊บผ๋ฒˆ์— ๋‹ค๋ฃจ๋Š” ์ฝ”๋“œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ๋ณ„๊ฐœ ๋ชจ๋“ˆ๋กœ ๋‚˜๋ˆ„์ž.
  • ์œ„์˜ ์˜ˆ์‹œ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด, ํŒŒ์‹ฑ ๋กœ์ง๊ณผ ์‹ค์ œ ๊ณ„์‚ฐ ๋กœ์ง์ด ํ˜ผ์žฌ๋˜์–ด ์žˆ๋‹ค.
  • ์ฐจ๋ผ๋ฆฌ ํŒŒ์‹ฑ์„ ํ•˜๊ณ , ๊ณ„์‚ฐ์„ ํ•˜๋Š” ๊ฒƒ์„ ๋‚˜๋ˆ„๋ฉด ํŽธํ•ด์ง„๋‹ค.
  • ๋˜ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๋กœ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์žˆ๋‹ค.
  • ํ…์ŠคํŠธ๋ฅผ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ํ˜•ํƒœ๋กœ ์ผ๋‹จ ๋ฐ”๊ฟ”์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์ตœ์ ํ™”๋ฅผ ํ•˜๊ณ , ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋ชฉ์  ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • ์ด๋ ‡๊ฒŒ ๋‹จ๊ณ„๋ฅผ ์ชผ๊ฐœ๋ฉด ๊ฐ ๋‹จ๊ณ„๋ฅผ ๋…๋ฆฝ์ ์œผ๋กœ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์–ด์„œ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์‰ฌ์›Œ์ง„๋‹ค.
  • ์ด๋ ‡๊ฒŒ ๋“ค์œผ๋ฉด ๊ทœ๋ชจ๊ฐ€ ํฐ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ ์šฉํ•˜๋Š” ๊ฑฐ๋ผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์œผ๋‚˜,
  • ๊ทœ๋ชจ์— ์ƒ๊ด€์—†์ด ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

์ ˆ์ฐจ

  1. ๋‘๋ฒˆ์งธ ๋‹จ๊ณ„์— ํ•ด๋‹นํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋…๋ฆฝ ํ•จ์ˆ˜๋กœ ์ถ”์ถœํ•œ๋‹ค.
  2. ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  3. ์ค‘๊ฐ„ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์•ž์—์„œ ์ถ”์ถœํ•œ ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.
  4. ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  5. ์ถ”์ถœํ•œ ๋‘๋ฒˆ์งธ ๋‹จ๊ณ„ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๊ฒ€ํ† ํ•œ๋‹ค.
  6. ์ฒซ๋ฒˆ์งธ ๋‹จ๊ณ„ ์ฝ”๋“œ๋ฅผ ํ•จ์ˆ˜๋กœ ์ถ”์ถœํ•˜๋ฉด์„œ ์ค‘๊ฐ„ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ˆ˜์ •ํ•œ๋‹ค.

์˜ˆ์‹œ

function priceOrder(product, quantity, shippingMethod) {
    const basePrice = product.basePrice * quantity;
    const discount = Math.max(quantity - product.discountThreshold, 0) * product.basePrice * product.discountRate;
    const shippingPerCase = (basePrice > shippingMethod.discountThreshold) ? shippingMethod.discountedFee : shippingMethod.feePerCase;
    const shippingCost = quantity * shippingPerCase;
    const price = basePrice - discount + shippingCost;
    return price;
}
  • ํ•˜๋‚˜๋Š” ์ƒํ’ˆ ๊ฐ€๊ฒฉ์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด๊ณ , ํ•˜๋‚˜๋Š” ๋ฐฐ์†ก๋น„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
  • ๊ฐ ๋‹จ๊ณ„๋ฅผ ๋‚˜๋ˆ„๋Š”๊ฒŒ ์œ ์ง€๋ณด์ˆ˜์— ์ข‹์•„๋ณด์ธ๋‹ค.

๋ฐฐ์†ก๋น„ ๊ณ„์‚ฐ ๋ถ€๋ถ„ ์ถ”์ถœ```javascript

function priceOrder(product, quantity, shippingMethod) { const basePrice = product.basePrice * quantity; const discount = Math.max(quantity - product.discountThreshold, 0) * product.basePrice * product.discountRate; const price = applyShipping(basePrice, shippingMethod, quantity, discount); return price; }

function applyShipping(basePrice, shippingMethod, quantity, discount) { const shippingPerCase = (basePrice > shippingMethod.discountThreshold) ? shippingMethod.discountedFee : shippingMethod.feePerCase; const shippingCost = quantity * shippingPerCase; const price = basePrice - discount + shippingCost; return price; }


- ๋‘๋ฒˆ์งธ ๋‹จ๊ณ„๋ฅผ ํ•จ์ˆ˜๋กœ ์ถ”์ถœํ–ˆ๋‹ค.
- ์ผ๋‹จ ๋‹ค๋ฅธ ์ž‘์—…ํ•˜์ง€ ๋ง๊ณ  ์ธ์ˆ˜๋กœ ๋‹ค ๋Œ๊ณ  ๋“ค์–ด์˜ค๋Š”๊ฒŒ ์ข‹๋‹ค. ํ•œ๋ฒˆ์— ์—ฌ๋Ÿฌ๊ฐœ ํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

## ์ค‘๊ฐ„ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ๋งŒ๋“ค๊ธฐ 
```javascript
function priceOrder(product, quantity, shippingMethod) {
    const basePrice = product.basePrice * quantity;
    const discount = Math.max(quantity - product.discountThreshold, 0) * product.basePrice * product.discountRate;
    const priceData = {};
    const price = applyShipping(priceData, basePrice, shippingMethod, quantity, discount);
    return price;
}

function applyShipping(priceData, basePrice, shippingMethod, quantity, discount) {
    const shippingPerCase = (basePrice > shippingMethod.discountThreshold) ? shippingMethod.discountedFee : shippingMethod.feePerCase;
    const shippingCost = quantity * shippingPerCase;
    const price = basePrice - discount + shippingCost;
    return price;
}
  • basePrice๋ฅผ priceData๋ผ๋Š” ์ค‘๊ฐ„ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ ์˜ฎ๊ฒผ๋‹ค.

applyShipping์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฒ€ํ† 

 
  • basePrice๋Š” ์ค‘๊ฐ„ ๋ฐ์ดํ„ฐ์—์„œ ๋ฐ›์œผ๋ฉด ๋œ๋‹ค.
  • shippingMethod๋Š” ์•ž์—์„œ ๊ณ„์‚ฐํ•˜์ง€ ์•Š์œผ๋‹ˆ ๋ฐ›์•„์•ผ ํ•œ๋‹ค.
  • quantity๋Š” ์ค‘๊ฐ„ ๋ฐ์ดํ„ฐ์—์„œ ๋ฐ›์œผ๋ฉด ๋œ๋‹ค. ์ƒํ’ˆ ๊ฐ€๊ฒฉ์— ๊ด€๋ จ๋œ ์ •๋ณด๋‹ˆ๊นŒ.
  • discount๋Š” ์ค‘๊ฐ„ ๋ฐ์ดํ„ฐ์—์„œ ๋ฐ›์œผ๋ฉด ๋œ๋‹ค.
function priceOrder(product, quantity, shippingMethod) {
    const priceData = calculatePricingData(product, quantity);
    const price = applyShipping(priceData, shippingMethod);
    return price;
}
 
function calculatePricingData(product, quantity) {
    const basePrice = product.basePrice * quantity;
    const discount = Math.max(quantity - product.discountThreshold, 0) * product.basePrice * product.discountRate;
    return {basePrice: basePrice, quantity: quantity, discount: discount};
}
 
function applyShipping(priceData, shippingMethod) {
    const shippingPerCase = (priceData.basePrice > shippingMethod.discountThreshold) ? shippingMethod.discountedFee : shippingMethod.feePerCase;
    const shippingCost = priceData.quantity * shippingPerCase;
    const price = priceData.basePrice - priceData.discount + shippingCost;
    return price;
}

Reference