diff --git a/Level-1/hack.py b/Level-1/hack.py index 731ddc0..e022f7b 100644 --- a/Level-1/hack.py +++ b/Level-1/hack.py @@ -9,7 +9,7 @@ def test_4(self): payment = c.Item(type='payment', description='invoice_4', amount=1e19, quantity=1) reimbursement = c.Item(type='payment', description='reimbursement_4', amount=-1e19, quantity=1) order_4 = c.Order(id='4', items=[payment, tv, reimbursement]) - self.assertEqual(c.validorder(order_4), 'Order ID: 4 - Payment imbalance: $-1000.00') + self.assertNotEqual(c.validorder(order_4), 'Order ID: 4 - Full payment received!') if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/Level-1/solution.py b/Level-1/solution.py index 82f16f9..5edf817 100644 --- a/Level-1/solution.py +++ b/Level-1/solution.py @@ -7,22 +7,34 @@ MAX_QUANTITY = 100 # maximum quantity of an item in the shop MAX_TOTAL = 1e6 # maximum total amount accepted for an order +# sets a reasonable min & max value for the invoice amounts +def validamount(amount): + return amount > -1*MAX_ITEM_AMOUNT and amount < MAX_ITEM_AMOUNT + +# sets a reasonable min & max value for the item quantities +def validquantity(quantity): + return quantity > -1 * MAX_QUANTITY and quantity < MAX_QUANTITY + def validorder(order): net = 0 for item in order.items: if item.type == 'payment': - # sets a reasonable min & max value for the invoice amounts - if item.amount > -1*MAX_ITEM_AMOUNT and item.amount < MAX_ITEM_AMOUNT: - net += item.amount + if not validamount(item.amount): + return("Invalid amount") + net += item.amount elif item.type == 'product': - if item.quantity > 0 and item.quantity <= MAX_QUANTITY and item.amount > 0 and item.amount <= MAX_ITEM_AMOUNT: - net -= item.amount * item.quantity + if not validquantity(item.quantity): + return("Invalid quantity") + if not validamount(item.amount): + return("Invalid amount") + + net -= item.amount * item.quantity if net > MAX_TOTAL or net < -1*MAX_TOTAL: return("Total amount exceeded") else: return("Invalid item type: %s" % item.type) - + if net != 0: return("Order ID: %s - Payment imbalance: $%0.2f" % (order.id, net)) else: