Skip to content
Justice Tech School

Practice: Bank Account

Combine a class, getter and exception.

Example
class Account {
  double _balance = 0;
  double get balance => _balance;
  void deposit(double n) {
    if (n <= 0) throw ArgumentError("must be positive");
    _balance += n;
  }
}
void main() {
  var a = Account()..deposit(50);
  try { a.deposit(-5); } catch (e) { print(e); }
  print(a.balance);
}

Exercise

Private field.

double balance = 0;

Quiz Test

Why a getter only?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.