it-praxisportal/04_str_und_darstellung/musterloesung.py

43 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Kapitel 04: __str__ und Objektlisten – Musterlösung
===================================================
"""
class Artikel:
def __init__(self, name, preis, anzahl=1):
self.name = name
self.preis = float(preis)
self.anzahl = int(anzahl)
def gesamtpreis(self):
return self.preis * self.anzahl
def __str__(self):
return f"{self.anzahl}x {self.name} (je {self.preis:.2f} €) = {self.gesamtpreis():.2f} €"
class Warenkorb:
def __init__(self):
self.artikel_liste = []
def artikel_hinzufuegen(self, artikel):
self.artikel_liste.append(artikel)
def gesamtsumme(self):
return sum(item.gesamtpreis() for item in self.artikel_liste)
def bon_text(self):
zeilen = ["--- KASSENZETTEL ---"]
for artikel in self.artikel_liste:
zeilen.append(str(artikel))
zeilen.append("--------------------")
zeilen.append(f"Gesamtsumme: {self.gesamtsumme():.2f} €")
return "\n".join(zeilen)
if __name__ == "__main__":
korb = Warenkorb()
korb.artikel_hinzufuegen(Artikel("Milch", 1.29, 2))
korb.artikel_hinzufuegen(Artikel("Brot", 2.49, 1))
korb.artikel_hinzufuegen(Artikel("Schokolade", 0.99, 3))
print(korb.bon_text())