Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def initialise_expenses(expenseList):
- expenseList.append(createExpense(1,100,'clothing'))
- expenseList.append(createExpense(1,10,'transport'))
- expenseList.append(createExpense(1,25,'food'))
- expenseList.append(createExpense(3,50,'others'))
- expenseList.append(createExpense(5,25,'food'))
- expenseList.append(createExpense(9,30,'food'))
- expenseList.append(createExpense(15,22,'food'))
- expenseList.append(createExpense(22,34,'food'))
- expenseList.append(createExpense(23,66,'housekeeping'))
- expenseList.append(createExpense(24,100,'internet'))
- def getDay(x):
- return x['day']
- def getCategory(x):
- return x['category']
- def getAmmount(x):
- return x['ammount']
- def setDay(x,y):
- x['day']=y
- def setCategory(x,y):
- x['category']=y
- def setAmmount(x,y):
- x['ammount']=y
- def isCategory(x):
- realCategories=['housekeeping','food','transport','clothing','internet','others']
- if x in realCategories:
- return True
- return False
- def nicePrint(expenseList):
- for i in range(0,len(expenseList)):
- print('day:',getDay(expenseList[i]),' category:',getCategory(expenseList[i]),' ammount:',getAmmount(expenseList[i]))
- def createExpense(day,ammount,category):
- try:
- return {'day':int(day),
- 'ammount':int(ammount),
- 'category':category}
- except ValueError as ve:
- raise ValueError
- def insertIntoPlace(expenseList,expense):
- i=0
- x=getDay(expenseList[0])
- j=len(expenseList)-1
- while i<j and x<getDay(expense):
- i+=1
- x=getDay(expenseList[i])
- if x<getDay(expense):
- expenseList.append(expense)
- else:
- expenseList.insert(i,expense)
- def insertExpense(expenseList,category,ammount,day = False):
- if day == False:
- insertIntoPlace(expenseList,createExpense(getDay(expenseList[-1]),ammount,category))
- else:
- insertIntoPlace(expenseList,createExpense(day,ammount,category))
- def ammountBigger(ammount1,ammount2):
- if ammount1>ammount2:
- return True
- return False
- def ammountSmaller(ammount1,ammount2):
- if ammount1<ammount2:
- return True
- return False
- def ammountEqual(ammount1,ammount2):
- if ammount1==ammount2:
- return True
- return False
- def categoryList(expenseList,category):
- l=[]
- for i in range(0,len(expenseList)):
- if getCategory(expenseList[i])==category:
- l.append(expenseList[i])
- return l
- def removeBySomething(expenseList,value,getSomething):
- i=0
- x=len(expenseList)-1
- while i<=x:
- if getSomething(expenseList[i])==value:
- del expenseList[i]
- x-=1
- i-=1
- i+=1
- def removeFromList(expenseList,p1,p2=None):
- try:
- if p2==None:
- if isCategory(p1)==True:
- removeBySomething(expenseList,p1,getCategory)
- else:
- removeBySomething(expenseList,int(p1),getDay)
- else:
- i=int(p1)+1
- j=int(p2)
- while i<j :
- removeBySomething(expenseList,i,getDay)
- i+=1
- nicePrint(expenseList)
- except ValueError as ve:
- raise ValueError
- def showExpensesUI(expenseList,category=None,sign=None,value=0):
- if category==None:
- nicePrint(expenseList)
- return
- writeList=categoryList(expenseList,category)
- if sign==None:
- nicePrint(writeList)
- return
- i=0
- x=len(writeList)-1
- while i<=x:
- if sign(getAmmount(writeList[i]),value)==False:
- del writeList[i]
- x-=1
- i-=1
- i+=1
- if len(writeList)>0:
- nicePrint(writeList)
- else:
- print('No such thing was found')
- return
- def addUI(expenseList,parameterList):
- if len(parameterList)==2 and isCategory(parameterList[1])==True:
- insertExpense(expenseList,parameterList[1],parameterList[0])
- print('expense registered')
- else:
- print('Invalid input')
- def insertUI(expenseList,parameterList):
- if len(parameterList)==3 and isCategory(parameterList[2])==True:
- insertExpense(expenseList,parameterList[2],parameterList[1],parameterList[0])
- print('expense registered')
- else:
- print('invalid input')
- def writeUI(expenseList,parameterList):
- realSigns={'>':ammountBigger,'=':ammountEqual,'<':ammountSmaller}
- if len(parameterList)>3 or len(parameterList)==2 :
- print('invalid input')
- return
- if len(parameterList)==0:
- showExpensesUI(expenseList)
- elif len(parameterList)==1 and isCategory(parameterList[0])==True:
- showExpensesUI(expenseList,parameterList[0])
- elif isCategory(parameterList[0])==True and parameterList[1] in realSigns.keys() :
- showExpensesUI(expenseList,parameterList[0],realSigns[parameterList[1]],int(parameterList[2]))
- else:
- print('invalid input')
- def removeUI(expenseList,parameterList):
- if len(parameterList)>3 or len(parameterList)==2 or len(parameterList)==0:
- print('invalid input')
- return
- if len(parameterList)==3:
- if not parameterList[1] == 'to':
- print('invalid input')
- return
- removeFromList(expenseList,parameterList[0],parameterList[2])
- else:
- removeFromList(expenseList,parameterList[0])
- def readCommand():
- UInput=input()
- command=UInput.split(' ')[0]
- parameters=UInput.split(' ')[1:]
- return {'command':command,'parameters':parameters}
- def start():
- expenseList=[]
- initialise_expenses(expenseList)
- print(expenseList)
- while True:
- realCommands={'add':addUI,'insert':insertUI,'list':writeUI,'remove':removeUI}
- read=readCommand()
- try:
- if read['command'] in realCommands.keys():
- realCommands[read['command']](expenseList,read['parameters'])
- elif read['command'] == 'exit':
- return
- else:
- print('invalid input brother')
- except ValueError as ve:
- print("Value Error")
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement