-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract-classes-methods
79 lines (69 loc) · 2.97 KB
/
abstract-classes-methods
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#I forgot to define s for the command in line 20 but that was my only error but it should be s=Swiss()
#Assignment was to make an abstract class that would be used to then make a regular class for a bank
# Import ABC and abstractmethod from the module abc (which stands for abstract base classes)
from abc import ABC, abstractmethod
# Class Bank
class Bank(ABC):
""" An abstract bank class
[IMPLEMENT ME]
1. This class must derive from class ABC
2. Write a basicinfo() function that prints out "This is a generic bank" and
returns the string "Generic bank: 0"
3. Define a second function called withdraw and keep it empty by
adding the `pass` keyword under it. Make this function abstract by
adding an '@abstractmethod' tag right above the function declaration.
"""
### YOUR CODE HERE
def basicinfo(self):
print("this is a generic bank")
s="generic bank: 0"
return s
@abstractmethod
def withdraw(self):
pass
# Class Swiss
class Swiss(Bank):
""" A specific type of bank than derives from class Bank
[IMPLEMENT ME]
1. This class must derive from class Bank
2. Create a constructor for this class that initializes a class
variable `bal` to 1000
3. Implement the basicinfo() function so that it prints "This is the
Swiss Bank" and returns a string with "Swiss Bank: " (don't forget
the space after the colon) followed by the current bank balance.
For example, if self.bal = 80, then it would return "Swiss Bank: 80"
4. Implement withdraw so that it takes one argument (in addition to
self) that is an integer which represents the amount to be withdrawn
from the bank. Deduct the withdrawn amount from the bank bal, print
the withdrawn amount ("Withdrawn amount: {amount}"), print the new
balance ("New Balance: {self.bal}"), and return the new balance.
Note: Make sure to verify that there is enough money to withdraw!
If amount is greater than balance, then do not deduct any
money from the class variable `bal`. Instead, print a
statement saying `"Insufficient funds"`, and return the
original account balance instead.
"""
### YOUR CODE HERE
def __init__(self):
self.bal=1000
def basicinfo(self):
print("this is the Swiss Bank")
s+ "Swiss Bank: "+str(self.bal)
return s
def withdraw(self,amount):
if(amount>self.bal):
print("insufficient funds")
else:
self.bal-=amount
print("withdrawn amount: "+str(amount))
print("new balance:"+str(self.bal))
return self.bal
# Driver Code
def main():
assert issubclass(Bank, ABC), "Bank must derive from class ABC"
s = Swiss()
print(s.basicinfo())
s.withdraw(30)
s.withdraw(1000)
if __name__ == "__main__":
main()