[text] 17

Viewer

  1. class BaseWallet:
  2.     def __init__(self, name, amount):
  3.         self.name = name
  4.         self.amount = amount
  5.  
  6.     def spend_all(self):
  7.         if self.amount > 0:
  8.             self.amount = 0
  9.  
  10.     def to_base(self):
  11.         return self.amount * self.exchange_rate
  12.  
  13.     def __eq__(self, other):
  14.         if isinstance(other, (RubbleWallet, DollarWallet, EuroWallet)):
  15.             if self.amount == other.amount and type(self) == type(other):
  16.                 return True
  17.             else:
  18.                 return False
  19.             
  20.     def __add__(self, other):
  21.         if isinstance(other, (int, RubbleWallet, DollarWallet, EuroWallet)):
  22.             sc = other if type(other) == int else other.amount * (other.exchange_rate/self.exchange_rate)
  23.         return self.__class__(self.name, self.amount + sc)
  24.  
  25.     def __iadd__(self, other):
  26.         if isinstance(other, (int, RubbleWallet, DollarWallet, EuroWallet)):
  27.             sc = other if type(other) == int else other.amount * (other.exchange_rate / self.exchange_rate)
  28.         self.amount += sc
  29.         return self
  30.  
  31.     def __radd__(self, other):
  32.         return self + other
  33.     
  34.     def __sub__(self, other):
  35.         if isinstance(other, (int, RubbleWallet, DollarWallet, EuroWallet)):
  36.             sc = other if type(other) == int else other.amount * (other.exchange_rate / self.exchange_rate)
  37.         return self.__class__(self.name, self.amount - sc)
  38.     
  39.     def __rsub__(self, other):
  40.         if isinstance(other, (int, RubbleWallet, DollarWallet, EuroWallet)):
  41.             sc = other if type(other) == int else other.amount * (other.exchange_rate/self.exchange_rate)
  42.         return self.__class__(self.name, sc - self.amount)
  43.  
  44.     def __isub__(self, other):
  45.         if isinstance(other, (int, RubbleWallet, DollarWallet, EuroWallet)):
  46.             sc = other if type(other) == int else other.amount * (other.exchange_rate / self.exchange_rate)
  47.         self.amount -= sc
  48.         return self
  49.     
  50.     def __mul__(self, other):
  51.         if isinstance(other, (int, RubbleWallet, DollarWallet, EuroWallet)):
  52.             sc = other if type(other) == int else other.amount * (other.exchange_rate/self.exchange_rate)
  53.         return self.__class__(self.name, self.amount * sc)
  54.  
  55.     def __imul__(self, other):
  56.         if isinstance(other, (int, RubbleWallet, DollarWallet, EuroWallet)):
  57.             sc = other if type(other) == int else other.amount * (other.exchange_rate / self.exchange_rate)
  58.         self.amount *= sc
  59.         return self
  60.     
  61.     def __rmul__(self, other):
  62.         if isinstance(other, (int, RubbleWallet, DollarWallet, EuroWallet)):
  63.             sc = other if type(other) == int else other.amount * (other.exchange_rate/self.exchange_rate)
  64.         return self.__class__(self.name, sc * self.amount)
  65.     
  66.     def __truediv__(self, other):
  67.         if isinstance(other, (int, RubbleWallet, DollarWallet, EuroWallet)):
  68.             sc = other if type(other) == int else other.amount * (other.exchange_rate/self.exchange_rate)
  69.         return self.__class__(self.name, self.amount / sc)
  70.  
  71.     def __itruediv__(self, other):
  72.         if isinstance(other, (int, RubbleWallet, DollarWallet, EuroWallet)):
  73.             sc = other if type(other) == int else other.amount * (other.exchange_rate / self.exchange_rate)
  74.         self.amount /= sc
  75.         return self
  76.  
  77. class RubbleWallet(BaseWallet):
  78.     def __init__(self, name, amount):
  79.         super().__init__(name, amount)
  80.         self.exchange_rate = 1
  81.  
  82.     def __repr__(self):
  83.         return f"Rubble Wallet {self.name} {self.amount}"
  84.  
  85. class DollarWallet(BaseWallet):
  86.     def __init__(self, name, amount):
  87.         super().__init__(name, amount)
  88.         self.exchange_rate = 60
  89.  
  90.     def __repr__(self):
  91.         return f"Dollar Wallet {self.name} {self.amount}"
  92.  
  93.  
  94. class EuroWallet(BaseWallet):
  95.     def __init__(self, name, amount):
  96.         super().__init__(name, amount)
  97.         self.exchange_rate = 70
  98.  
  99.     def __repr__(self):
  100.         return f"Euro Wallet {self.name} {self.amount}"
  101.  

Editor

You can edit this paste and save as new:


File Description
  • 17
  • Paste Code
  • 26 Nov-2022
  • 3.98 Kb
You can Share it: