Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 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 = []
  44. last_save += getTransactions( tees )
  45. last_save.append( transaction )
  46. tees.append( last_save )
  47.  
  48. def getTransactions( tees ):
  49. '''
  50. The function returns the current list of transactions
  51. :param tees: history of saves
  52. :return:
  53. '''
  54. return tees[ len( tees ) - 1 ]
  55.  
  56. def checkPosition(transactions, pos):
  57. ''''''
  58. if(pos < len(transactions)):
  59. return True
  60. return False
  61.  
  62. def getPosition( transactions, pos ):
  63. if(checkPosition( transactions, pos ) == False):
  64. print('Out of range')
  65. return []
  66. return transactions[ pos ]
  67.  
  68. def printTransactions(tees):
  69. '''
  70. The function prints all the currnet transactions
  71. :param tees: history of saves
  72. :return:
  73. '''
  74. aux = getTransactions( tees )
  75. for i in aux:
  76. print( i )
  77.  
  78. def createTransaction(tees, day, value, t_type, dscr):
  79. '''
  80. The function creates a new transaction and adds it to the history
  81. :param tees: history of saves
  82. :param day:
  83. :param value:
  84. :param t_type:
  85. :param dscr:
  86. :return:
  87. '''
  88. aux = []
  89. constructTransaction(aux, day, value, t_type, dscr)
  90. addTransaction(tees, aux)
  91.  
  92. def loadTransactions( tees ):
  93. '''
  94. The function adds 10 transactions to the history
  95. :param tees: history of saves
  96. :return:
  97. '''
  98. createTransaction(tees, 1, 1000, 'in', 'verify')
  99. createTransaction(tees, 2, 20, 'out', 'verify')
  100. createTransaction(tees, 2, 489, 'out', 'verify')
  101. createTransaction(tees, 3, 200, 'in', 'verify')
  102. createTransaction(tees, 4, 300, 'out', 'verify')
  103. createTransaction(tees, 5, 29, 'out', 'verify')
  104. createTransaction(tees, 5, 2000, 'in', 'verify')
  105. createTransaction(tees, 5, 500, 'out', 'verify')
  106. createTransaction(tees, 6, 50, 'out', 'verify')
  107. createTransaction(tees, 7, 100, 'out', 'verify')
  108.  
  109. def readCommand():
  110. comm = input('command > ')
  111. pos = comm.find(' ')
  112. if pos == -1:
  113. return (comm, [])
  114. command = comm[:pos]
  115. parameters = comm[pos + 1:]
  116. parameters = parameters.split(" ")
  117. for i in range(len(parameters)):
  118. parameters[i] = parameters[i].strip()
  119. return (command, parameters)
  120.  
  121. def print_commands():
  122. print('The possible commands are: ')
  123. print('-> add')
  124. print('-> insert')
  125. print('-> remove')
  126. print('-> list')
  127.  
  128. def start( tees = []):
  129. pair = readCommand()
  130. cmd = pair[ 0 ]
  131. parameters = pair[ 1 ]
  132. if(cmd == 'list' and parameters == []):
  133. printTransactions( tees )
  134. start( tees )
  135.  
  136.  
  137. def initialize():
  138. tees = [ [] ]
  139. print_commands()
  140. loadTransactions( tees )
  141. start( tees )
  142.  
  143. initialize()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement