{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a1bdb97f",
   "metadata": {},
   "source": [
    "# 14-10 · Enkapsulacja\n",
    "\n",
    "Praktyka do sekcji [„Enkapsulacja”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "901bb800",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "Chroń stan obiektu, pozwalając na jego zmianę tylko przez metody."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "400cd45b",
   "metadata": {},
   "source": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b56260f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Konto:\n",
    "    def __init__(self, balans):\n",
    "        self.__balans = balans\n",
    "\n",
    "    def popolnit(self, summa):\n",
    "        if summa > 0:\n",
    "            self.__balans += summa\n",
    "            return True\n",
    "        return False\n",
    "\n",
    "    def poluchit_balans(self):\n",
    "        return self.__balans\n",
    "\n",
    "schet = Konto(100)\n",
    "schet.popolnit(50)\n",
    "print(schet.poluchit_balans())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2612385",
   "metadata": {},
   "source": [
    "## Zadanie niezależne od zadania ★★\n",
    "\n",
    "Dodaj `Konto` metoda `snyat(summa)`to zmniejsza `__balans` tylko jeśli `summa` nie przekracza bieżącego salda i zwraca `True`/`False` w zależności od sukcesu."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef537ccf",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Konto:\n",
    "    def __init__(self, balans):\n",
    "        self.__balans = balans\n",
    "\n",
    "    def popolnit(self, summa):\n",
    "        if summa > 0:\n",
    "            self.__balans += summa\n",
    "            return True\n",
    "        return False\n",
    "\n",
    "    def snyat(self, summa):\n",
    "        if 0 < summa <= self.__balans:\n",
    "            self.__balans -= summa\n",
    "            return True\n",
    "        return False\n",
    "\n",
    "    def poluchit_balans(self):\n",
    "        return self.__balans\n",
    "\n",
    "schet = Konto(100)\n",
    "schet.popolnit(50)\n",
    "uspeshno = schet.snyat(30)\n",
    "neuspeshno = schet.snyat(9999)\n",
    "print(schet.poluchit_balans(), uspeshno, neuspeshno)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "language_info": {
   "name": "python",
   "version": "3.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
