Współdzielony kalkulator cen i podatków
Enkapsulowanie reguł dotyczących pieniędzy w jednym wieloplatformowym miejscu
Współdzielony kalkulator cen i podatków to bezpłatna lekcja Kotlin Multiplatform Academy na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Kotlin Multiplatform Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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 * rateAdd 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) / 100Support 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 * rateBoth 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. ✅
Często zadawane pytania
Czy lekcja „Współdzielony kalkulator cen i podatków” jest bezpłatna?
Tak — pełny tekst „Współdzielony kalkulator cen i podatków” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Kotlin Multiplatform Academy, przejdź na CoddyKit PRO. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Współdzielony kalkulator cen i podatków”?
Enkapsulowanie reguł dotyczących pieniędzy w jednym wieloplatformowym miejscu Ćwiczysz Kotlin Multiplatform Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Kotlin Multiplatform Academy?
Nie wymagamy żadnego doświadczenia. Kotlin Multiplatform Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.
Ile czasu zajmuje lekcja „Współdzielony kalkulator cen i podatków”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Kotlin Multiplatform Academy?
Tak. Każda lekcja Kotlin Multiplatform Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Współdzielony kalkulator cen i podatków
- Formatowanie walut i liczb
- Reguły biznesowe jako czyste funkcje
- Interfejs poza współdzielonym kodem