-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.py
More file actions
67 lines (49 loc) · 1.98 KB
/
classes.py
File metadata and controls
67 lines (49 loc) · 1.98 KB
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
class Category:
def __init__(self, category, amount=0):
self.category = category
self.amount = amount
def deposit(self, amount):
self.amount += amount
print(f"Deposit : $ {amount} added to {self.category}")
def withdraw(self, amount):
if self.check_balance(amount):
self.amount -= amount
print(f"Withdraw : $ {amount} withdrawn from {self.category}")
return True
else:
print("Withdraw failed!!")
return False
def balance(self):
self.amount += 0
print(f"Balance : You have $ {self.amount} in {self.category}")
return self.amount
def check_balance(self, amount):
if self.amount <= amount:
print(f"Check Balance : Your total balance in {self.category} category is {self.amount} which is less than or equal to {amount} you requested to check !")
return False
return True
def transfer(self, amount, category):
if not self.check_balance(amount):
print(f"You dont have enough balance to transfer. You requested {amount} and you have {self.amount}")
return False
self.amount -= amount
category.amount += amount
print(f"Transfer : You are transfering {self.amount} from {self.category} to {category.category} with {category.amount} as original balance in {category.category}")
return True
# test with some inputs
instance = Category("Food", 3000)
instance1 = Category("Car", 500)
instance2 = Category("Clothing", 200)
instance3 = Category("Travel", 100)
# Food category - balance, deposit and withdraw
print(instance.balance())
instance.deposit(300)
print(instance.balance())
instance.withdraw(200)
print(instance.balance())
print(instance.check_balance(300))
print(instance1.balance())
instance.transfer(100, instance2)
print(instance2.balance())
instance.transfer(100, instance3)
print(instance3.balance())