Encapsulation
easy 35 minPut the data and the methods that work on it into one unit, and let outside code reach the data only through those methods.
Watch lesson4:51
An account that owns its rules
Read it, run it, change a line and run it again.
Loading editor…
Each variant keeps the same six private fields and one private rule. The JavaScript version is the TypeScript one without types.
The same in Java
class Refused extends RuntimeException { Refused(String why) { super(why); }}final class BankAccount { private static final long DAILY_LIMIT = 50_000; private final String holder; private final String number; private long balanceCents = 0; private boolean frozen = false; private int day = 0; private long takenToday = 0; BankAccount(String holder, String number) { if (holder.isBlank()) throw new IllegalArgumentException("holder is required"); this.holder = holder.strip(); this.number = number; } long balanceCents() { return balanceCents; } String maskedNumber() { return "****" + number.substring(Math.max(0, number.length() - 2)); } boolean canWithdraw(long cents, int day) { return whyNot(cents, day) == null; } void deposit(long cents) { if (cents <= 0) throw new Refused("amount must be positive"); balanceCents = Math.addExact(balanceCents, cents); } void withdraw(long cents, int day) { String reason = whyNot(cents, day); if (reason != null) throw new Refused(reason); takenToday = takenOn(day) + cents; this.day = day; balanceCents -= cents; } void freeze() { frozen = true; } private long takenOn(int day) { return day == this.day ? takenToday : 0; } private String whyNot(long cents, int day) { if (frozen) return "account is frozen"; if (cents <= 0) return "amount must be positive"; if (cents > balanceCents) return "not enough money"; if (takenOn(day) + cents > DAILY_LIMIT) return "daily limit reached"; return null; }}The same in C++
#include <cassert>#include <optional>#include <stdexcept>#include <string>struct Refused : std::runtime_error { using std::runtime_error::runtime_error;};class BankAccount {public: static constexpr long long kDailyLimit = 50'000; BankAccount(std::string holder, std::string number) : holder_(std::move(holder)), number_(std::move(number)) { if (holder_.find_first_not_of(" \t") == std::string::npos) throw std::invalid_argument("holder is required"); } long long balanceCents() const { return balanceCents_; } std::string maskedNumber() const { return "****" + number_.substr(number_.size() > 2 ? number_.size() - 2 : 0); } bool canWithdraw(long long cents, int day) const { return !whyNot(cents, day); } void deposit(long long cents) { if (cents <= 0) throw Refused("amount must be positive"); balanceCents_ += cents; } void withdraw(long long cents, int day) { if (auto reason = whyNot(cents, day)) throw Refused(*reason); takenToday_ = takenOn(day) + cents; day_ = day; balanceCents_ -= cents; } void freeze() { frozen_ = true; }private: long long takenOn(int day) const { return day == day_ ? takenToday_ : 0; } std::optional<std::string> whyNot(long long cents, int day) const { if (frozen_) return "account is frozen"; if (cents <= 0) return "amount must be positive"; if (cents > balanceCents_) return "not enough money"; if (takenOn(day) + cents > kDailyLimit) return "daily limit reached"; return std::nullopt; } std::string holder_; std::string number_; long long balanceCents_ = 0; bool frozen_ = false; int day_ = 0; long long takenToday_ = 0;};The same in TypeScript
class Refused extends Error {}class BankAccount { static readonly DAILY_LIMIT = 50_000; readonly #holder: string; readonly #number: string; #balanceCents = 0; #frozen = false; #day = 0; #takenToday = 0; constructor(holder: string, number: string) { if (!holder.trim()) throw new Error("holder is required"); this.#holder = holder.trim(); this.#number = number; } get balanceCents(): number { return this.#balanceCents; } get maskedNumber(): string { return "****" + this.#number.slice(-2); } canWithdraw(cents: number, day: number): boolean { return this.#whyNot(cents, day) === null; } deposit(cents: number): void { if (!Number.isSafeInteger(cents) || cents <= 0) throw new Refused("amount must be positive"); this.#balanceCents += cents; } withdraw(cents: number, day: number): void { const reason = this.#whyNot(cents, day); if (reason !== null) throw new Refused(reason); this.#takenToday = this.#takenOn(day) + cents; this.#day = day; this.#balanceCents -= cents; } freeze(): void { this.#frozen = true; } #takenOn(day: number): number { return day === this.#day ? this.#takenToday : 0; } #whyNot(cents: number, day: number): string | null { if (this.#frozen) return "account is frozen"; if (!Number.isSafeInteger(cents) || cents <= 0) return "amount must be positive"; if (cents > this.#balanceCents) return "not enough money"; if (this.#takenOn(day) + cents > BankAccount.DAILY_LIMIT) return "daily limit reached"; return null; }}The same in JavaScript
class Refused extends Error {}class BankAccount { static DAILY_LIMIT = 5e4; #holder; #number; #balanceCents = 0; #frozen = false; #day = 0; #takenToday = 0; constructor(holder, number) { if (!holder.trim()) throw new Error("holder is required"); this.#holder = holder.trim(); this.#number = number; } get balanceCents() { return this.#balanceCents; } get maskedNumber() { return "****" + this.#number.slice(-2); } canWithdraw(cents, day) { return this.#whyNot(cents, day) === null; } deposit(cents) { if (!Number.isSafeInteger(cents) || cents <= 0) throw new Refused("amount must be positive"); this.#balanceCents += cents; } withdraw(cents, day) { const reason = this.#whyNot(cents, day); if (reason !== null) throw new Refused(reason); this.#takenToday = this.#takenOn(day) + cents; this.#day = day; this.#balanceCents -= cents; } freeze() { this.#frozen = true; } #takenOn(day) { return day === this.#day ? this.#takenToday : 0; } #whyNot(cents, day) { if (this.#frozen) return "account is frozen"; if (!Number.isSafeInteger(cents) || cents <= 0) return "amount must be positive"; if (cents > this.#balanceCents) return "not enough money"; if (this.#takenOn(day) + cents > BankAccount.DAILY_LIMIT) return "daily limit reached"; return null; }}