EdisonChendi
9/28/2016 - 3:13 PM

Code Drinker

Code Drinker

# -*- coding: UTF-8 -*-

class ColaDrinker(object):

    def __init__(self, money, cola_price, redeem_num):
        self.cola_price = cola_price
        self.redeem_num = redeem_num
        self.bottles = 0
        self.money = money

    def buy(self):
        self.cola = int(self.money // self.cola_price)
        self.money = self.money - (self.cola * self.cola_price)

    def drink(self):
        self.bottles += self.cola
        drinked = self.cola
        self.cola = 0
        return drinked

    def redeem(self):
        self.cola = self.bottles // self.redeem_num
        self.bottles = self.bottles % self.redeem_num

    def can_redeem(self):
        return (self.bottles // self.redeem_num) > 0

    def run(self):
        self.buy()
        records = []
        records.append(self.drink())
        while(self.can_redeem()):
            self.redeem()
            records.append(self.drink())
            print(records)
        return records

if __name__ == "__main__":
    colordrinker = ColaDrinker(money=20, cola_price=1, redeem_num=2)
    print(sum(colordrinker.run()))
    colordrinker = ColaDrinker(money=1000, cola_price=2, redeem_num=3)
    print(sum(colordrinker.run()))