-
Notifications
You must be signed in to change notification settings - Fork 0
/
capitalGains.py
81 lines (62 loc) · 2.13 KB
/
capitalGains.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
__author__ = 'sarvps'
'''
Author: Sarv Parteek Singh
Course: CISC 610
Term: Late Summer
Mid-term exam, Problem 2
Brief: Finds capital gains/losses for a given input sequence
'''
class Queue:
def __init__(self):
self.elements = []
def isEmpty(self):
return self.elements == []
def enqueue(self,element): #at the start of list
self.elements.insert(0,element)
def dequeue(self): # from the end of list
return self.elements.pop()
def size(self):
return len(self.elements)
class shareQueue(Queue):
def __init__(self,price):
Queue.__init__(self)
self.gains = 0
self.expenditure = 0
def getGains(self):
return self.gains
def getNetExpenditure(self):
return self.expenditure
def buyShares(self,shareQuantity,sharePrice):
for i in range(shareQuantity):
self.enqueue(sharePrice)
self.expenditure += sharePrice
def sellShares(self,shareQuantity,sellingPrice):
for i in range(shareQuantity):
self.gains += sellingPrice - self.dequeue()
def getList(self):
return self.elements
def handleBuying(squeue):
res = input("Do you wish to buy shares(y/n)?\n")
if (res != 'y' and res != 'Y'):
return
quantity = int(input("How many shares do you wish to buy?\n"))
price = int(input("At what price do you wish to buy these shares?\n"))
squeue.buyShares(quantity,price)
def handleSelling(squeue):
res = input("Do you wish to sell shares(y/n)?\n")
if (res != 'y' and res != 'Y'):
return
quantity = int(input("How many shares do you wish to sell?\n"))
price = int(input("At what price do you wish to sell these shares?\n"))
squeue.sellShares(quantity,price)
def showExpenditureAndGains(squeue):
print("Your current expenditure is ${} and gains are ${}".format(squeue.getNetExpenditure(),squeue.getGains()))
q = Queue()
s = shareQueue(q)
while (True):
handleBuying(s)
handleSelling(s)
showExpenditureAndGains(s)
res = input("Do you wish to continue(y/n)?\n")
if (res != 'y' and res != 'Y'):
break