index — ti25-glauchau-code @ 9310e74950cb91ac2b8c3ca6a6fe6c4254689fbc

Meine Lösungen (oder auch nicht) für die Programmieraufgaben in der TI25 an der Staatlichen Studienakademie Glauchau

oop/2026-05-28/echteprobeklausur/Aufgabe 1/warenkorb.py (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
class Warenkorb:
    def __init__(self, credit):
        self.__credit = credit
        self.__items = {}

    def addItem(self, item_name: str, price: float):
        self.__items[item_name] = price

    def getItems(self) -> dict:
        return self.__items

    def balance(self) -> float:
        return self.__credit

    def checkout(self) -> bool:
        price = 0
        for _, value in self.__items.items():
            price += value

        works = self.__checkCredit(price)
        self.__credit -= price if works else 0
        if works:
            self.__items = {}
        return works

    def __checkCredit(self, total: float) -> bool:
        return True if total <= self.__credit else False


class AddItemCounterDecorator(Warenkorb):
    def __init__(self, warenkorb: Warenkorb):
        self._warenkorb = warenkorb
        self.__counter = 0

    def __del__(self):
        print(f"ItemCounter: {self.__counter}")

    def addItem(self) -> str:
        self.__counter += 1
        return self._warenkorb.addItem()