0Pricing
Kotlin Multiplatform Academy · درس

حاسبة مشتركة للأسعار والضرائب

تغليف قواعد الأموال في مكان واحد متعدد المنصات

حاسبة مشتركة للأسعار والضرائب درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Money Lives in Shared Code

Price and tax math should live in one shared place so Android and iOS can never disagree on a total. 💰

The Bug You Are Preventing

When each app rounds tax its own way, the same cart shows two prices. A single shared calculator kills that whole class of bug.

Where It Goes

This calculator belongs in commonMain, the source set both platforms compile, so there is exactly one source of truth.

Model the Price First

Start with a tiny data class that holds the amount in plain numbers. Keep it pure Kotlin so it works on every target.

data class Price(val amount: Double, val taxRate: Double)

The Tax Formula

Tax is just the amount times the rate. Expressing it as one function means both apps run the exact same line.

fun tax(amount: Double, rate: Double): Double = amount * rate

Add It to the Total

The total is the base amount plus the tax you just computed. Compose small functions instead of one giant one.

fun total(amount: Double, rate: Double): Double = amount + tax(amount, rate)

Wrap It in a Calculator

Group the rules in a small class so both apps get a clean, named entry point instead of loose functions.

class PriceCalculator {
  fun total(amount: Double, rate: Double) = amount + amount * rate
}

Watch the Rounding

Floating point can drift by a cent. Decide your rounding rule once in shared code so totals are identical everywhere.

fun roundMoney(value: Double): Double = kotlin.math.round(value * 100) / 100

Support Discounts Too

Real carts have discounts. Apply the discount before tax so the saving feels honest to the customer.

fun discounted(amount: Double, off: Double) = amount * (1 - off)

Default Values Help Callers

A sensible default tax rate keeps simple call sites short while still letting either app override it when needed.

fun total(amount: Double, rate: Double = 0.20) = amount + amount * rate

Both Apps Just Call It

Android and iOS each call the same total function. No copied math, no drift, one trusted answer for the price.

val due = PriceCalculator().total(amount = 49.99, rate = 0.18)

Quick Check

Where should the price and tax calculator live in a KMP project?

Recap: One Calculator to Rule Both

You modeled price, computed tax, totaled it, and handled rounding in commonMain. One calculator, zero price drift between apps. ✅

الأسئلة الشائعة

هل درس «حاسبة مشتركة للأسعار والضرائب» مجاني؟

نعم — نص درس «حاسبة مشتركة للأسعار والضرائب» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

ماذا ستتعلم في «حاسبة مشتركة للأسعار والضرائب»؟

تغليف قواعد الأموال في مكان واحد متعدد المنصات تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟

لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «حاسبة مشتركة للأسعار والضرائب»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟

نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. حاسبة مشتركة للأسعار والضرائب
  2. تنسيق العملات والأرقام
  3. قواعد الأعمال كدوال صِرفة
  4. إبعاد الواجهة عن التعليمات البرمجية المشتركة
← العودة إلى Kotlin Multiplatform Academy