Skip to content

Instantly share code, notes, and snippets.

@payoung
Forked from eleddy/inheritance.py
Last active December 18, 2015 23:49
Show Gist options
  • Save payoung/5864761 to your computer and use it in GitHub Desktop.
Save payoung/5864761 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
class Toy(object):
inventory = 0
def __init__(self, price, name, quantity):
self.price = price
self.name = name
self.quantity = quantity
Toy.inventory += quantity
def __del__(self):
self._reduce_inventory(self.quantity)
def __repr__(self):
return "Toy: %s for $%s" % (self.name,
self.price)
def _get_base_value(self):
return float(self.price) * float(self.quantity)
def _reduce_inventory(self, num):
Toy.inventory -= num
def get_total_value(self):
"""
Override this function for our software to work
"""
raise Exception("You must implement get_total_value")
def sell(self, num=1):
self.quantity -= num
self._reduce_inventory(num)
class ActionFigure(Toy):
def __init__(self, price, name, quantity, royalty_rate):
super(ActionFigure, self).__init__(price, name, quantity)
self.royalty_rate = royalty_rate
def get_total_value(self):
mine = 1 - self.royalty_rate
return self._get_base_value() * float(mine)
def get_vendor_proceeds(self):
return self._get_base_value() * self.royalty_rate
class BoardGame(Toy):
def __init__(self, price, name, quantity, players=1):
super(BoardGame, self).__init__(price, name, quantity)
self.players = 1
if players > 0:
self.players = players
def __repr__(self):
return "%s ($%s) is meant for %s players" % (self.name,
self.price, self.players)
def price_per_person(self):
return float(self.price)/float(self.players)
class LegosSet(Toy):
def __init__(self, price, name, quantity, num_pieces, skill_level):
super(LegosSet, self).__init__(price, name, quantity)
self.num_pieces = num_pieces
self.skill_level = skill_level
def __repr__(self):
return "%s is for ages %s" % (self.name, self.skill_level)
def price_per_piece(self):
return float(self.price)/float(self.num_pieces)
def total_pieces(self):
return self.quantity*self.num_pieces
def test_action_figure():
a = ActionFigure(29.00, "Batman", 50, .1)
assert(a.get_total_value() == 1305.00)
print a
def test_sell():
a = ActionFigure(29.00, "Batman", 50, .1)
a.sell()
assert(a.get_total_value() == 1278.9)
print a
def test_vendor_proceeds():
a = ActionFigure(10.00, "Batman", 50, .1)
assert(a.get_vendor_proceeds() == 50.00)
print a
def test_board_game():
b = BoardGame(40.00, "Scrabble", 10, 4)
assert(b.price_per_person() == 10.00)
print b
def test_no_players():
b = BoardGame(40.00, "Scrabble", 10, 0)
assert(b.price_per_person() == 40.00)
print b
def test_inventory():
b = BoardGame(40.00, "Scrabble", 10, 0)
a1 = ActionFigure(29.00, "Batman", 50, .1)
a2 = ActionFigure(29.00, "Batman", 100, .1)
assert(Toy.inventory == 160)
def test_legos_price():
s = LegosSet(30.00, "Star Wars", 30, 100, "8-15")
assert(s.price_per_piece() == 0.3)
print s
def test_legos_qty():
s = LegosSet(30.00, "Star Wars", 30, 100, "8-15")
assert(s.total_pieces() == 3000)
if __name__ == '__main__':
test_action_figure()
test_sell()
test_vendor_proceeds()
test_board_game()
test_no_players()
test_inventory()
test_legos_price()
test_legos_qty()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment