Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lehrkraefte:blc:informatik:ffprg1-2020:oop [2021/03/18 10:05] michael.greminger |
lehrkraefte:blc:informatik:ffprg1-2020:oop [2021/03/26 18:02] (current) michael.greminger |
||
---|---|---|---|
Line 4: | Line 4: | ||
=== Präsentation === | === Präsentation === | ||
+ | {{lehrkraefte: | ||
=== Aufgabe 1 === | === Aufgabe 1 === | ||
Line 17: | Line 18: | ||
" | " | ||
- | < | + | < |
- | < | + | < |
class Konto: | class Konto: | ||
def __init__(self, | def __init__(self, | ||
Line 33: | Line 34: | ||
k2 = Konto(" | k2 = Konto(" | ||
print k1.kontostand() | print k1.kontostand() | ||
- | print k2.inhaber() | + | print k2.inhaber() |
- | </ | + | </code> |
+ | </hidden> | ||
=== Aufgabe 2 === | === Aufgabe 2 === | ||
Line 47: | Line 49: | ||
Testen Sie diese Klasse, indem sie eine Instanz kreieren und benutzen. | Testen Sie diese Klasse, indem sie eine Instanz kreieren und benutzen. | ||
+ | |||
+ | <hidden Lösungsvorschlag> | ||
+ | <code python> | ||
+ | from datetime import datetime | ||
+ | |||
+ | class Konto: | ||
+ | def __init__(self, | ||
+ | | ||
+ | | ||
+ | |||
+ | def inhaber(self): | ||
+ | | ||
+ | |||
+ | def kontostand(self): | ||
+ | return self._kontostand | ||
+ | | ||
+ | class Buchung: | ||
+ | def __init__(self, | ||
+ | | ||
+ | | ||
+ | |||
+ | def betrag(self): | ||
+ | | ||
+ | |||
+ | def datum(self): | ||
+ | return self._datum | ||
+ | |||
+ | b1 = Buchung(456, | ||
+ | print b1.datum() | ||
+ | |||
+ | k1 = Konto(" | ||
+ | k2 = Konto(" | ||
+ | print k1.kontostand() | ||
+ | print k2.inhaber() | ||
+ | </ | ||
+ | </ | ||
=== Aufgabe 3 === | === Aufgabe 3 === | ||
- | Fügen Sie zur Klasse Konto eine neue Methode ein " | + | Fügen Sie zur Klasse Konto eine neue Methode ein " |
+ | |||
+ | <hidden Lösungsvorschlag> | ||
+ | <code python> | ||
+ | from datetime import datetime | ||
+ | |||
+ | class Konto: | ||
+ | def __init__(self, | ||
+ | self._inhaber = i | ||
+ | self._kontostand = k | ||
+ | self._buchungen = [] | ||
+ | |||
+ | def inhaber(self): | ||
+ | | ||
+ | |||
+ | def kontostand(self): | ||
+ | return self._kontostand | ||
+ | |||
+ | def addBuchung(self, | ||
+ | buchungInstanz = Buchung(betrag, | ||
+ | self._buchungen.append(buchungInstanz) | ||
+ | self._kontostand += buchungInstanz.betrag() | ||
+ | |||
+ | class Buchung: | ||
+ | def __init__(self, | ||
+ | | ||
+ | | ||
+ | |||
+ | def betrag(self): | ||
+ | | ||
+ | |||
+ | def datum(self): | ||
+ | return self._datum | ||
+ | |||
+ | |||
+ | k1 = Konto(" | ||
+ | k1.addBuchung(100, | ||
+ | print k1.kontostand() | ||
+ | </ | ||
+ | </ | ||
=== Aufgabe 4 === | === Aufgabe 4 === | ||
Fügen Sie zur Klasse Konto eine Methode " | Fügen Sie zur Klasse Konto eine Methode " | ||
+ | |||
+ | <hidden Lösungsvorschlag> | ||
+ | <code python> | ||
+ | from datetime import datetime | ||
+ | |||
+ | class Konto: | ||
+ | def __init__(self, | ||
+ | self._inhaber = i | ||
+ | self._kontostand = k | ||
+ | self._buchungen = [] | ||
+ | |||
+ | def inhaber(self): | ||
+ | | ||
+ | |||
+ | def kontostand(self): | ||
+ | return self._kontostand | ||
+ | | ||
+ | def kontostandDatum(self, | ||
+ | datum = datetime.strptime(dString, | ||
+ | index = len(self._buchungen) - 1 | ||
+ | betrag = self._kontostand | ||
+ | while index >= 0 and datum < self._buchungen[index].datum(): | ||
+ | betrag = betrag - self._buchungen[index].betrag() | ||
+ | index = index - 1 | ||
+ | return betrag | ||
+ | | ||
+ | def addBuchung(self, | ||
+ | b = Buchung(b, d) | ||
+ | self._buchungen.append(b) | ||
+ | self._kontostand += b.betrag() | ||
+ | | ||
+ | class Buchung: | ||
+ | def __init__(self, | ||
+ | | ||
+ | | ||
+ | |||
+ | def betrag(self): | ||
+ | | ||
+ | |||
+ | def datum(self): | ||
+ | return self._datum | ||
+ | |||
+ | |||
+ | k1 = Konto(" | ||
+ | k1.addBuchung(100, | ||
+ | k1.addBuchung(200, | ||
+ | k1.addBuchung(300, | ||
+ | print k1.kontostand() | ||
+ | print k1.kontostandDatum(" | ||
+ | print k1.kontostandDatum(" | ||
+ | print k1.kontostandDatum(" | ||
+ | print k1.kontostandDatum(" | ||
+ | </ | ||
+ | </ | ||
===== Teil 2 ===== | ===== Teil 2 ===== | ||
=== Präsentation === | === Präsentation === | ||
+ | {{lehrkraefte: | ||
=== Aufgabe 5 === | === Aufgabe 5 === | ||
+ | |||
+ | Fügen Sie bei Konto eine neue Methode " | ||
+ | |||
+ | <hidden Lösungsvorschlag> | ||
+ | <code python> | ||
+ | from datetime import datetime | ||
+ | |||
+ | class Konto: | ||
+ | def __init__(self, | ||
+ | self._inhaber = i | ||
+ | self._kontostand = k | ||
+ | self._buchungen = [] | ||
+ | |||
+ | def inhaber(self): | ||
+ | | ||
+ | |||
+ | def kontostand(self): | ||
+ | return self._kontostand | ||
+ | | ||
+ | def zinssatz(self): | ||
+ | return 0.035 | ||
+ | | ||
+ | def kontostandDatum(self, | ||
+ | datum = datetime.strptime(dString, | ||
+ | index = len(self._buchungen) - 1 | ||
+ | betrag = self._kontostand | ||
+ | while index >= 0 and datum < self._buchungen[index].datum(): | ||
+ | betrag = betrag - self._buchungen[index].betrag() | ||
+ | index = index - 1 | ||
+ | return betrag | ||
+ | | ||
+ | def addBuchung(self, | ||
+ | b = Buchung(b, d) | ||
+ | self._buchungen.append(b) | ||
+ | self._kontostand += b.betrag() | ||
+ | | ||
+ | class Supersparkonto (Konto): | ||
+ | def __init__(self, | ||
+ | Konto.__init__(self, | ||
+ | | ||
+ | def zinssatz(self): | ||
+ | abgehoben = 0 | ||
+ | for b in self._buchungen: | ||
+ | if b.betrag() < 0: | ||
+ | abgehoben += 1 | ||
+ | zins = Konto.zinssatz(self) | ||
+ | if abgehoben < 3: | ||
+ | zins += 0.01 | ||
+ | return zins | ||
+ | | ||
+ | class Buchung: | ||
+ | def __init__(self, | ||
+ | | ||
+ | | ||
+ | |||
+ | def betrag(self): | ||
+ | | ||
+ | |||
+ | def datum(self): | ||
+ | return self._datum | ||
+ | |||
+ | |||
+ | k1 = Supersparkonto(" | ||
+ | k1.addBuchung(-100, | ||
+ | k1.addBuchung(-200, | ||
+ | print k1.zinssatz() | ||
+ | k1.addBuchung(-300, | ||
+ | print k1.zinssatz() | ||
+ | |||
+ | </ | ||
+ | </ | ||
+ | |||
+ | === Aufgabe 6 === | ||
+ | |||
+ | Füge zur Klasse Konto eine Methode " | ||
+ | |||
+ | Zusatz: Natürlich ist es auch möglich, die korrekte Zinsgutschrift zu berechnen (wieviele Tage war welcher Betrag auf dem Konto). Wenn jemand mit allem fertig ist, kann das gerne versucht werden. | ||
+ | |||
+ | <hidden Lösungsvorschlag> | ||
+ | <code python> | ||
+ | from datetime import datetime | ||
+ | |||
+ | class Konto: | ||
+ | def __init__(self, | ||
+ | self._inhaber = i | ||
+ | self._kontostand = k | ||
+ | self._buchungen = [] | ||
+ | |||
+ | def inhaber(self): | ||
+ | | ||
+ | |||
+ | def kontostand(self): | ||
+ | return self._kontostand | ||
+ | | ||
+ | def zinssatz(self): | ||
+ | return 0.035 | ||
+ | | ||
+ | def kontostandDatum(self, | ||
+ | datum = datetime.strptime(dString, | ||
+ | index = len(self._buchungen) - 1 | ||
+ | betrag = self._kontostand | ||
+ | while index >= 0 and datum < self._buchungen[index].datum(): | ||
+ | betrag = betrag - self._buchungen[index].betrag() | ||
+ | index = index - 1 | ||
+ | return betrag | ||
+ | | ||
+ | def addBuchung(self, | ||
+ | b = Buchung(b, d) | ||
+ | self._buchungen.append(b) | ||
+ | self._kontostand += b.betrag() | ||
+ | | ||
+ | def jahresAbschluss(self): | ||
+ | self.addBuchung(self.kontostand() * self.zinssatz(), | ||
+ | | ||
+ | class Supersparkonto (Konto): | ||
+ | def __init__(self, | ||
+ | Konto.__init__(self, | ||
+ | | ||
+ | def zinssatz(self): | ||
+ | abgehoben = 0 | ||
+ | for b in self._buchungen: | ||
+ | if b.betrag() < 0: | ||
+ | abgehoben += 1 | ||
+ | zins = Konto.zinssatz(self) | ||
+ | if abgehoben < 3: | ||
+ | zins += 0.01 | ||
+ | return zins | ||
+ | | ||
+ | class Buchung: | ||
+ | def __init__(self, | ||
+ | | ||
+ | | ||
+ | |||
+ | def betrag(self): | ||
+ | | ||
+ | |||
+ | def datum(self): | ||
+ | return self._datum | ||
+ | |||
+ | |||
+ | k1 = Supersparkonto(" | ||
+ | k1.addBuchung(-100, | ||
+ | k1.addBuchung(-200, | ||
+ | k1.addBuchung(-300, | ||
+ | print k1.kontostand() | ||
+ | k1.jahresAbschluss() | ||
+ | print k1.kontostand() | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ |