예제 #1
0
파일: main.py 프로젝트: ryan-Heard/cart-ex
def main():
    total = 0
    basket = {}
    codes = [True]

    basket = Basket()

    with open('prices.json') as json_data:
        prices = json.load(json_data)

    print("Please type 'total' to see current total")
    print("Please type 'checkout' to see current total and empty cart")
    print("Type 'q' to quit")

    while True:
        codes = input("Please enter your codes seperated by ',': ").split(',')
        logger.info(codes)
        if codes[0] == 'q':
            break

        elif codes[0] == 'total':
            basket.print_cart()

        elif codes[0] == 'checkout':
            basket.print_cart()
            basket.check_out()

        else:
            codes = [code.strip() for code in codes]
            for item in codes:
                if item not in prices:
                    logger.error("{} is not a code or command".format(item))
                    print("{} is not a code or command".format(item))
                else:
                    try:
                        basket.add_items(item)
                    except Exception as e:
                        logger.error(e)
예제 #2
0
class TestBasket(unittest.TestCase):

    def setUp(self):
        self.basket = Basket()

    def test_add_items(self):
        self.basket.add_items('CH1')
        self.assertEqual(self.basket.items.count('CH1'), 1)

        self.basket.add_items('CH1')
        self.assertEqual(self.basket.items.count('CH1'), 2)

        self.basket.check_out()
        self.assertEqual(len(self.basket.items), 0)

    def test_checkout(self):
        self.basket.add_items('CH1, AP1, CF1, MK1')
        self.basket.check_out()
        self.assertEqual(len(self.basket.items), 0)

    def test_milk_dis(self):
        self.basket.add_items('CH1, AP1, CF1, MK1')
        self.assertAlmostEqual(self.basket.total(), 20.34, places=2)

    def test_simple_add(self):
        self.basket.add_items('MK1, AP1')
        self.assertAlmostEqual(self.basket.total(), 10.75, places=2)

    def test_BOGO(self):
        self.basket.add_items('CF1')
        self.assertAlmostEqual(self.basket.total(), 11.23, places=2)
        self.basket.check_out()

        self.basket.add_items('CF1, CF1')
        self.assertAlmostEqual(self.basket.total(), 11.23, places=2)

        self.basket.add_items('CF1, CF1')
        self.assertAlmostEqual(self.basket.total(), 22.46, places=2)

    def test_get_disc(self):
        item = {'Code': 'AP1', 'Price': 4.50}
        self.assertEqual(self.basket.get_discount_code(item),
                         {'name': 'APPL', 'disc': 4.50, 'visual': "-4.50"})
        item = {'Code': 'AP1', 'Price': 2.25}
        self.assertEqual(self.basket.get_discount_code(item),
                         {'name': 'APOM', 'disc': .5, 'visual': "-50%"})
        item = {'Code': 'AP1', 'Price': 3.00}
        self.assertEqual(self.basket.get_discount_code(item),
                         {'name': 'APOM', 'disc': .5, 'visual': "-50%"})

        item = {'Code': 'MK1', 'Price': 0}
        self.assertEqual(self.basket.get_discount_code(item),
                         {'name': 'CHMK', 'disc': 4.75, 'visual': "-4.75"})

        item = {'Code': 'CF1', 'Price': 0}
        self.assertEqual(self.basket.get_discount_code(item),
                         {'name': 'BOGO', 'disc': 11.23, 'visual': "-11.23"})

    def test_APPL_disc(self):
        self.basket.add_items('AP1, AP1, CH1, AP1')
        self.assertAlmostEqual(self.basket.total(), 16.61, places=2)

    def test_APOM_disc(self):
        self.basket.add_items('AP1, AP1, CH1, AP1, OM1')
        self.assertAlmostEqual(self.basket.total(), 18.05, places=2)