Semaine 7
This commit is contained in:
51
Semaine_07/Jour_04/bank/entities/accounts.py
Normal file
51
Semaine_07/Jour_04/bank/entities/accounts.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from random import randint
|
||||
|
||||
from sqlalchemy import ForeignKey, Column, Integer, Float
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from database import Base, engine
|
||||
|
||||
class Account(Base):
|
||||
__tablename__ = "accounts"
|
||||
|
||||
id = Column(Integer, primary_key= True, autoincrement= True)
|
||||
number = Column(Integer, nullable= False)
|
||||
balance = Column(Float, default= 0, nullable= False)
|
||||
|
||||
client_id = Column(Integer, ForeignKey("clients.id"), nullable= False)
|
||||
credit_card_id = Column(Integer, ForeignKey("credit_cards.id"))
|
||||
|
||||
client = relationship('Client', back_populates= "accounts")
|
||||
|
||||
def __init__(self, initial_deposit: int | float) -> None:
|
||||
self.number = self.account_number()
|
||||
self.amount = initial_deposit
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"\
|
||||
[Numéro de compte] : {self.number}\n\
|
||||
[Montant disponible] : {self.amount}"
|
||||
|
||||
def __eq__(self, other: 'Account'):
|
||||
return self.number == other.number
|
||||
|
||||
def account_number(self):
|
||||
number = "".join(str(randint(0,9)) for _ in range(7))
|
||||
return " ".join(number[_: _ + 4] for _ in range(0,7,4))
|
||||
|
||||
def add_money(self, add_amount: int | float):
|
||||
if add_amount < 0:
|
||||
return f"{add_amount} n'est pas un entier positif. Recommencez."
|
||||
self.amount += add_amount
|
||||
return f"{add_amount} ont été ajoutés. Le nouveau solde est de {self.amount}."
|
||||
|
||||
def take_money(self, take_amount: int | float):
|
||||
if take_amount < 0:
|
||||
return f"{take_amount} n'est pas un entier positif. Recommencez."
|
||||
self.amount -= take_amount
|
||||
if self.amount < 0:
|
||||
return f"{take_amount} ont été retirés. Le nouveau solde est de {self.amount}.\n\
|
||||
Vous êtes débiteur et devrez payer des agios si vous ne régularisez pas votre situation."
|
||||
return f"{take_amount} ont été retirés. Le nouveau solde est de {self.amount}."
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
Reference in New Issue
Block a user