-
Notifications
You must be signed in to change notification settings - Fork 1
/
taxes-calculator.py
241 lines (207 loc) · 7.71 KB
/
taxes-calculator.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import re
import unittest
class taxCalculator(object):
items = []
taxedItems = []
salesTaxes = 0.0
total = 0.0
# 'Costants' for the taxes
salesTax = 0.1
importationTax = 0.05
def addItem(self, item):
self.items.append(item)
def clear(self):
self.items = []
self.taxedItems = []
self.salesTaxes = 0.0
self.total = 0.0
def calculateTax(self):
for item in self.items:
taxedItem = self.calculateSingleTax(item)
self.taxedItems.append(taxedItem)
self.taxedItems.append('Sales Taxes: ' + Helper.numberToString(self.salesTaxes))
self.taxedItems.append('Total: ' + Helper.numberToString(self.total))
def calculateSingleTax(self, item):
itemWithoutAt = Helper.replaceAt(item)
tax = 0.0
if Helper.mustBeTaxed(itemWithoutAt):
tax += self.salesTax
if Helper.isImported(itemWithoutAt):
tax += self.importationTax
return self.taxPrice(itemWithoutAt, tax)
def taxPrice(self, item, tax):
price = re.findall('\d+.\d+', item)
# Convert string into float, execute tax%, save, back to string
try:
priceFloat = float(price[0])
if Helper.thereIsPromo(self.items) and Helper.isPromo(item) == False:
priceFloat *= .9
if Helper.thereIsPromo(self.items) and len(self.items) == 1:
priceFloat *= .9
totalTax = priceFloat * tax
if Helper.isImported(item):
totalTax = Helper.roundToFive(totalTax)
self.salesTaxes += totalTax
taxedPrice = round(priceFloat + totalTax, 2)
self.total += taxedPrice
return Helper.arrangeImported(item.replace(price[0], Helper.numberToString(taxedPrice), 1))
except:
return item # In case the string is not of the correct format nothing happens
class Helper(object):
@staticmethod
def numberToString(numb):
return str("%.2f" % numb)
@staticmethod
def replaceAt(item):
return item.replace(' at ', ': ', 1)
@staticmethod
def roundToFive(number):
twoDecimalPrecision = (number * 100) % 10
if twoDecimalPrecision == 5 or twoDecimalPrecision == 0:
return number
return number - twoDecimalPrecision / 100 + (0.05 if twoDecimalPrecision < 5 else 0.1)
@staticmethod
def mustBeTaxed(item):
freeTaxRE = 'books?|chocolates?|headache|pills?'
return len(re.findall(freeTaxRE, item, flags=re.IGNORECASE)) == 0
@staticmethod
def isImported(item):
return len(re.findall('imported', item, flags=re.IGNORECASE)) > 0
@staticmethod
def arrangeImported(item):
number = re.findall('\d+ ', item)
imported = re.findall('imported ', item, flags=re.IGNORECASE)
try: # from '1 box of imported chocolates at 11.25' to '1 imported box of chocolates: 11.25'
if len(number) > 0:
return item.replace(imported[0], '', 1).replace(number[0], number[0] + imported[0], 1)
return imported[0] + item.replace(imported[0], '', 1)
except:
return item # In case the string is not properly fomratted
@staticmethod
def isPromo(item):
return len(re.findall('promo', item, flags=re.IGNORECASE)) > 0
@staticmethod
def thereIsPromo(items):
for item in items:
if Helper.isPromo(item):
return True
return False
# @staticmethod
# def allpyDiscount(items, promo):
# for item in items:
# if (item != promo):
# number = Helper.findPrice(item)
# @staticmethod
# def findPrice(item):
# return Helper.float(re.findall('\d+.\d+', item)[0])
# UnitTest for the class taxCalculator
class taxTest(unittest.TestCase):
tax = taxCalculator()
def test_addItem_adds_item_to_items(self):
self.tax.clear()
self.tax.addItem('Hello test')
self.assertEqual(len(self.tax.items), 1)
def test_calculateTax_slould_move_items_to_taxedItems(self):
self.tax.clear()
self.tax.addItem('1 chocolate bar at 0.85')
self.tax.calculateTax()
self.assertEqual(self.tax.taxedItems[0], '1 chocolate bar: 0.85')
def text_calculateSingleTax_should_increase_number(self):
increasedItem = self.tax.calculateSingleTax('1 music CD at 14.99')
self.assertEqual(increasedItem, '1 music CD: 16.49')
def test_calculateTax_slould_increment_price(self):
self.tax.clear()
self.tax.addItem('1 music CD at 14.99')
self.tax.calculateTax()
self.assertEqual(self.tax.taxedItems[0], '1 music CD: 16.49')
def test_calculateTax_should_not_increment_for_books(self):
self.tax.clear()
self.tax.addItem('1 book at 12.49')
self.tax.calculateTax()
self.assertEqual(self.tax.taxedItems[0], '1 book: 12.49')
def test_imported_items_should_have_more_tax(self):
self.tax.clear()
self.tax.addItem('1 imported box of chocolates at 10.00')
self.tax.calculateTax()
self.assertEqual(self.tax.taxedItems[0], '1 imported box of chocolates: 10.50')
def test_random_stirng_should_not_raise_error(self):
self.tax.clear()
self.tax.addItem('foobar foo bar foo')
self.tax.calculateTax()
self.assertEqual(self.tax.taxedItems[0], 'foobar foo bar foo')
def test_taxes_should_be_done_at_once(self):
self.tax.clear()
self.tax.addItem('1 imported bottle of perfume at 47.50')
self.tax.calculateTax()
self.assertEqual(self.tax.taxedItems[0], '1 imported bottle of perfume: 54.65')
def test_imported_items_should_round_to_the_nearest_5(self):
self.tax.clear()
self.tax.addItem('1 box of imported chocolates at 11.25')
self.tax.calculateTax()
self.assertEqual(self.tax.taxedItems[0], '1 imported box of chocolates: 11.85')
def test_imported_items_should_move_the_imported_word(self):
self.tax.clear()
self.tax.addItem('1 box of imported chocolates at 11.25')
self.tax.calculateTax()
self.assertEqual(self.tax.taxedItems[0], '1 imported box of chocolates: 11.85')
def test_Input_1(self):
self.tax.clear()
self.tax.addItem('1 book at 12.49')
self.tax.addItem('1 music CD at 14.99')
self.tax.addItem('1 chocolate bar at 0.85')
self.tax.calculateTax()
results = []
results.append('1 book: 12.49')
results.append('1 music CD: 16.49')
results.append('1 chocolate bar: 0.85')
results.append('Sales Taxes: 1.50')
results.append('Total: 29.83')
self.assertEqual(self.tax.taxedItems, results)
def test_Input_2(self):
self.tax.clear()
self.tax.addItem('1 imported box of chocolates at 10.00')
self.tax.addItem('1 imported bottle of perfume at 47.50')
self.tax.calculateTax()
results = []
results.append('1 imported box of chocolates: 10.50')
results.append('1 imported bottle of perfume: 54.65')
results.append('Sales Taxes: 7.65')
results.append('Total: 65.15')
self.assertEqual(self.tax.taxedItems, results)
def test_Input_3(self):
self.tax.clear()
self.tax.addItem('1 imported bottle of perfume at 27.99')
self.tax.addItem('1 bottle of perfume at 18.99')
self.tax.addItem('1 packet of headache pills at 9.75')
self.tax.addItem('1 box of imported chocolates at 11.25')
self.tax.calculateTax()
results = []
results.append('1 imported bottle of perfume: 32.19')
results.append('1 bottle of perfume: 20.89')
results.append('1 packet of headache pills: 9.75')
results.append('1 imported box of chocolates: 11.85')
results.append('Sales Taxes: 6.70')
results.append('Total: 74.68')
self.assertEqual(self.tax.taxedItems, results)
def test_Promo_Product(self):
self.tax.clear()
self.tax.addItem('1 promo product at 50.00')
self.tax.calculateTax()
self.assertEqual(self.tax.taxedItems[0], '1 promo product: 49.50')
def test_isPromoTest(self):
self.assertEqual(True, Helper.isPromo('1 promo product at 45'))
def test_thereIsPromoTest(self):
lista = ['1 promo product at 45.00', '1 product at 45.00', '1 product at 45.00']
self.assertEqual(True, Helper.thereIsPromo(lista))
def test_Promo_doppio(self):
self.tax.clear()
self.tax.addItem('1 promo product at 50.00')
self.tax.addItem('1 music CD at 14.99')
self.tax.calculateTax()
results = []
results.append('1 promo product: 55.00')
results.append('1 music CD: 14.84')
results.append('Sales Taxes: 6.35')
results.append('Total: 69.84')
self.assertEqual(self.tax.taxedItems, results)
unittest.main()