Skip to content

Commit 664f21b

Browse files
committed
SplitWise machine-coding implementation in python
1 parent 27dfb8c commit 664f21b

File tree

8 files changed

+235
-0
lines changed

8 files changed

+235
-0
lines changed

S/splitwise/CalculationService.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from CalculationServiceImp import CalcultaionServiceImpl
2+
3+
4+
class CalculationService(CalcultaionServiceImpl):
5+
6+
def percentageSplit(self, paidby, amount, debtusers, contributions):
7+
billCopy = list()
8+
for i in range(len(debtusers)):
9+
c = (contributions[i] * amount) / 100
10+
paidby.getDebts()[debtusers[i]] = paidby.getDebts().get(debtusers[i], 0) + c
11+
billCopy.append((paidby, debtusers[i], c))
12+
return billCopy
13+
14+
def equalSplit(self, paidby, amount, debtusers, contributions):
15+
billCopy = list()
16+
for i in range(len(debtusers)):
17+
c = (amount) / len(debtusers)
18+
paidby.getDebts()[debtusers[i]] = paidby.getDebts().get(debtusers[i], 0) + c
19+
billCopy.append((paidby, debtusers[i], c))
20+
return billCopy
21+
22+
def exactSplit(self, paidby, amount, debtusers, contributions):
23+
billCopy = list()
24+
for i in range(len(debtusers)):
25+
c = contributions[i]
26+
paidby.getDebts()[debtusers[i]] = paidby.getDebts().get(debtusers[i], 0) + c
27+
billCopy.append((paidby, debtusers[i]))
28+
return billCopy
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import abc
2+
class CalcultaionServiceImpl(metaclass = abc.ABCMeta):
3+
4+
@abc.abstractmethod
5+
def percentageSplit(paidby,amount,debtusers,contributions):
6+
pass
7+
8+
@abc.abstractmethod
9+
def equalSplit(paidby,amount,debtusers,contributions):
10+
pass
11+
12+
@abc.abstractmethod
13+
def exactSplit(paidby,amount,debtusers,contributions):
14+
pass
15+
16+
17+

S/splitwise/Expense.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from CalculationService import CalculationService
2+
3+
4+
class Expense:
5+
6+
__paidBy = None
7+
__amount = None
8+
__divisions = None
9+
__debtUsers = None
10+
__strategy = None
11+
__contributions = None
12+
__bill = None
13+
calculation = CalculationService()
14+
15+
def __init__(self,paiby,amount,divisions, debtUsers,strategy,contributions):
16+
self.__paidBy = paiby
17+
self.__amount = amount
18+
self.__divisions = divisions
19+
self.__debtUsers = debtUsers
20+
self.__strategy = strategy
21+
self.__contributions = contributions
22+
self.__bill = list()
23+
24+
25+
def getBill(self):
26+
27+
if self.__strategy=="PERCENT":
28+
self.__bill = self.calculation.percentageSplit(self.__paidBy,self.__amount,self.__debtUsers,self.__contributions)
29+
elif self.__strategy=="EXACT":
30+
self.__bill = self.calculation.exactSplit(self.__paidBy,self.__amount,self.__debtUsers,self.__contributions)
31+
elif self.__strategy=="EQUAL":
32+
self.__bill = self.calculation.equalSplit(self.__paidBy,self.__amount,self.__debtUsers,self.__contributions)
33+
return self.__bill
34+
35+
def getPaidBy(self):
36+
return self.__paidBy
37+
38+
def getDeptUsers(self):
39+
return self.__debtUsers

S/splitwise/ExpenseController.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from Expense import Expense
2+
3+
4+
class ExpenseController:
5+
__expense= None
6+
__settledBill = None
7+
def __init__(self,paiby,amount,divisions, debtUsers,strategy,contributions):
8+
self.__settledBill= list()
9+
self.__expense = Expense(paiby,amount,divisions, debtUsers,strategy,contributions)
10+
self.__settleBill()
11+
12+
13+
def __settleBill(self):
14+
bill = self.__expense.getBill()
15+
16+
for b in bill:
17+
gu,du = b[0],b[1]
18+
if (du.getDebts().get(gu,0)>gu.getDebts().get(du,0)):
19+
du.getDebts()[gu] = du.getDebts().get(gu,0) - gu.getDebts().get(du,0)
20+
gu.getDebts()[du] = 0
21+
updateString = gu.getName() + " pays " + str(du.getDebts()[gu]) + "to " + du.getName()
22+
self.__settledBill.append(updateString)
23+
elif (du.getDebts().get(gu,0)<gu.getDebts().get(du,0)):
24+
gu.getDebts()[du] = gu.getDebts().get(du,0) - du.getDebts().get(gu,0)
25+
du.getDebts()[gu] = 0
26+
updateString = du.getName() + " pays " + str(gu.getDebts()[du]) + "to " + gu.getName()
27+
self.__settledBill.append(updateString)
28+
else:
29+
gu.getDebts()[du] = gu.getDebts().get(du,0) - du.getDebts().get(gu,0)
30+
du.getDebts()[gu] = 0
31+
32+
def getSettledBill(self):
33+
return self.__settledBill
34+

S/splitwise/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- To run the implementation:
2+
```
3+
python3 SplitwiseDriver.py
4+
```
5+
**Output**:
6+
```
7+
u2 pays 620.0to u1
8+
u3 pays 1130.0to u1
9+
u1 pays 230.0to u4
10+
```
11+
12+
**NOTE**: Customise around the default input values set in the SplitwiseDriver.py to test out different results.

S/splitwise/SplitwiseDriver.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from ExpenseController import ExpenseController
2+
from UserController import UserController
3+
from User import User
4+
5+
6+
class SplitwiseDriver:
7+
8+
def printStatement(self, statements):
9+
for s in statements:
10+
print(s)
11+
12+
def main(self):
13+
expenseList = list()
14+
userList = list()
15+
16+
u1 = User("u1", 1)
17+
userList.append(u1)
18+
u2 = User("u2", 2)
19+
userList.append(u2)
20+
u3 = User("u3", 3)
21+
userList.append(u3)
22+
u4 = User("u4", 4)
23+
userList.append(u4)
24+
userManager = UserController(userList)
25+
26+
ex1 = ExpenseController(u1, 1000, 4, [u1, u2, u3, u4], "EQUAL", [])
27+
28+
# self.printStatement(userManager.getUserBill(u4))
29+
# self.printStatement(userManager.getUserBill(u1))
30+
31+
ex2 = ExpenseController(u1, 1250, 2, [u2, u3], "EXACT", [370, 880])
32+
# self.printStatement(ex2.getSettledBill())
33+
34+
ex3 = ExpenseController(u4, 1200, 4, [u1, u2, u3, u4], "PERCENT", [40, 20, 20, 20])
35+
self.printStatement(userManager.getUserBill(u1))
36+
# self.printStatement(ex3.getSettledBill())
37+
38+
39+
spl = SplitwiseDriver()
40+
spl.main()

S/splitwise/User.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class User:
2+
__name = None
3+
__dues = None
4+
__id = None
5+
6+
def __init__(self,name,id):
7+
self.__id= id
8+
self.__name= name
9+
self.__dues= dict()
10+
11+
def getName(self):
12+
return self.__name
13+
14+
def getId(self):
15+
return self.__id
16+
17+
def getDebts(self):
18+
return self.__dues
19+
20+

S/splitwise/UserController.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class UserController:
2+
__userList= None
3+
__settledBill = None
4+
def __init__(self,userList):
5+
self.__settledBill= list()
6+
self.__userList = userList
7+
self.__settleBill()
8+
9+
10+
def __settleBill(self):
11+
for gu in self.__userList:
12+
for du in self.__userList:
13+
if (gu!=du):
14+
if (du.getDebts().get(gu,0)>gu.getDebts().get(du,0)):
15+
du.getDebts()[gu] = du.getDebts().get(gu,0) - gu.getDebts().get(du,0)
16+
gu.getDebts()[du] = 0
17+
updateString = gu.getName() + " pays " + str(du.getDebts()[gu]) + "to " + du.getName()
18+
self.__settledBill.append(updateString)
19+
elif (du.getDebts().get(gu,0)<gu.getDebts().get(du,0)):
20+
gu.getDebts()[du] = gu.getDebts().get(du,0) - du.getDebts().get(gu,0)
21+
du.getDebts()[gu] = 0
22+
updateString = du.getName() + " pays " + str(gu.getDebts()[du]) + "to " + gu.getName()
23+
self.__settledBill.append(updateString)
24+
else:
25+
gu.getDebts()[du] = gu.getDebts().get(du,0) - du.getDebts().get(gu,0)
26+
du.getDebts()[gu] = 0
27+
28+
def getSettledBill(self):
29+
return self.__settledBill
30+
31+
def getUserBill(self,u):
32+
self.getSettledBill()
33+
userBill = list()
34+
for gu in self.__userList:
35+
if (gu!=u):
36+
if (u.getDebts().get(gu,0)>gu.getDebts().get(u,0)):
37+
updateString = gu.getName() + " pays " + str(u.getDebts()[gu]) + "to " + u.getName()
38+
userBill.append(updateString)
39+
elif (u.getDebts().get(gu,0)<gu.getDebts().get(u,0)):
40+
u.getDebts()[gu] = 0
41+
updateString = u.getName() + " pays " + str(gu.getDebts()[u]) + "to " + gu.getName()
42+
userBill.append(updateString)
43+
if (len(userBill)==0):
44+
userBill.append("No Bill ")
45+
return userBill

0 commit comments

Comments
 (0)