Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. def checkTransaction(day, value, t_type, dscr):
  2. '''
  3. The function checks if the parameters match the description
  4. :param day:
  5. :param value:
  6. :param t_type:
  7. :param dscr:
  8. :return: True - parameters are ok
  9. False - if they don't
  10. '''
  11. if(day < 1 or day > 30):
  12. return False
  13. if(value < 1):
  14. return False
  15. if(t_type != 'in' and t_type != 'out'):
  16. return False
  17. return True
  18.  
  19. def constructTransaction(transactions, day, value, t_type, dscr):
  20. '''
  21. The function takes list and gives it the parameters of the transaction
  22. :param day: int
  23. :param value: int
  24. :param t_type: str
  25. :param dscr: str
  26. :return: list of 4 elements if the parameters are valid
  27. empty list if they are not
  28. '''
  29. if( checkTransaction(day, value, t_type, dscr) == False):
  30. print('The parameters are invalid')
  31. transactions.append( day )
  32. transactions.append( value )
  33. transactions.append( t_type )
  34. transactions.append( dscr )
  35.  
  36. def addTransaction(tees, transaction ):
  37. '''
  38. The function adds a transaction to the history
  39. :param tees: history of saves
  40. :param transaction: list of 4 parameters
  41. :return:
  42. '''
  43. last_save = getTransactions( tees )
  44. last_save.append( transaction )
  45. tees.append( last_save )
  46.  
  47. def getTransactions( tees ):
  48. '''
  49. The function returns the current list of transactions
  50. :param tees: history of saves
  51. :return:
  52. '''
  53. return tees[ len( tees ) - 1 ]
  54.  
  55. def checkPosition(transactions, pos):
  56. ''''''
  57. if(pos < len(transactions)):
  58. return True
  59. return False
  60.  
  61. def getPosition( transactions, pos ):
  62. if(checkPosition( transactions, pos ) == False):
  63. print('Out of range')
  64. return []
  65. return transactions[ pos ]
  66.  
  67. def printTransactions(tees):
  68. '''
  69. The function prints all the currnet transactions
  70. :param tees: history of saves
  71. :return:
  72. '''
  73. aux = getTransactions( tees )
  74. for i in aux:
  75. print( i )
  76.  
  77. def createTransaction(tees, day, value, t_type, dscr):
  78. '''
  79. The function creates a new transaction and adds it to the history
  80. :param tees: history of saves
  81. :param day:
  82. :param value:
  83. :param t_type:
  84. :param dscr:
  85. :return:
  86. '''
  87. aux = []
  88. constructTransaction(aux, day, value, t_type, dscr)
  89. addTransaction(tees, aux)
  90.  
  91. def loadTransactions( tees ):
  92. '''
  93. The function adds 10 transactions to the history
  94. :param tees: history of saves
  95. :return:
  96. '''
  97. createTransaction(tees, 1, 1000, 'in', 'verify')
  98. createTransaction(tees, 2, 20, 'out', 'verify')
  99. createTransaction(tees, 2, 489, 'out', 'verify')
  100. createTransaction(tees, 3, 200, 'in', 'verify')
  101. createTransaction(tees, 4, 300, 'out', 'verify')
  102. createTransaction(tees, 5, 29, 'out', 'verify')
  103. createTransaction(tees, 5, 2000, 'in', 'verify')
  104. createTransaction(tees, 5, 500, 'out', 'verify')
  105. createTransaction(tees, 6, 50, 'out', 'verify')
  106. createTransaction(tees, 7, 100, 'out', 'verify')
  107.  
  108. def readCommand():
  109. comm = input('command > ')
  110. pos = comm.find(' ')
  111. if pos == -1:
  112. return (comm, [])
  113. command = comm[:pos]
  114. parameters = comm[pos + 1:]
  115. parameters = parameters.split(" ")
  116. for i in range(len(parameters)):
  117. parameters[i] = parameters[i].strip()
  118. return (command, parameters)
  119.  
  120. def print_commands():
  121. print('The possible commands are: ')
  122. print('-> add')
  123. print('-> insert')
  124. print('-> remove')
  125. print('-> list')
  126.  
  127. def start( tees = []):
  128. pair = readCommand()
  129. cmd = pair[ 0 ]
  130. parameters = pair[ 1 ]
  131. if(cmd == 'list' and parameters == []):
  132. printTransactions( tees )
  133. start( tees )
  134.  
  135.  
  136. def initialize():
  137. tees = [ [] ]
  138. print_commands()
  139. loadTransactions( tees )
  140. start( tees )
  141.  
  142. initialize()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement