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)
|
||||
37
Semaine_07/Jour_04/bank/entities/banks.py
Normal file
37
Semaine_07/Jour_04/bank/entities/banks.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from clients import Client, ClientNotFound
|
||||
from credit_cards import CreditCard
|
||||
|
||||
from sqlalchemy import Column, Integer, String
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from database import Base, engine
|
||||
|
||||
class Bank(Base):
|
||||
__tablename__ = "banks"
|
||||
|
||||
id = Column(Integer, primary_key= True, autoincrement= True)
|
||||
name = Column(String(200), nullable= False)
|
||||
|
||||
clients = relationship("Client", back_populate= "bank", uselist= True) # type: ignore
|
||||
# Le uselist définit le One ou le Many d'une relation inter-tables
|
||||
# Cela permet aussi de définir la dépendance d'une table à l'autre
|
||||
|
||||
def __init__(self, name: str) -> None:
|
||||
self.name = name
|
||||
self.clients: list["Client"] = []
|
||||
# Tout attribut faisant référence à une autre classe
|
||||
# est une RELATION, pas une COLONNE
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"\
|
||||
[Banque] : {self.name}"
|
||||
|
||||
def add_client(self, client: "Client"):
|
||||
self.clients.append(client)
|
||||
|
||||
def request_credit_card(self, client: "Client"):
|
||||
if client not in self.clients:
|
||||
raise ClientNotFound()
|
||||
return CreditCard(account = client.account)
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
43
Semaine_07/Jour_04/bank/entities/clients.py
Normal file
43
Semaine_07/Jour_04/bank/entities/clients.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from accounts import Account
|
||||
from services import client_service
|
||||
|
||||
from sqlalchemy import ForeignKey, Column, Integer, String, Float
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from database import Base, engine
|
||||
|
||||
class Client(Base):
|
||||
__tablename__ = "clients"
|
||||
|
||||
id = Column(Integer, primary_key= True, autoincrement= True)
|
||||
first_name = Column(String(200), nullable= False)
|
||||
last_name = Column(String(200), nullable= False)
|
||||
city = Column(String(200))
|
||||
salary = Column(Float)
|
||||
|
||||
bank_id = Column(Integer, ForeignKey("banks.id"), nullable= False)
|
||||
|
||||
bank = relationship("Bank", back_populates= "clients")
|
||||
account = relationship("Account", back_populates= "clients", uselist= False) # type: ignore
|
||||
services = relationship("Services", back_populates= "clients", secondary= client_service)
|
||||
|
||||
def __init__(self, first_name: str, last_name: str, salary: float, city: str, initial_deposit: int | float = 0) -> None:
|
||||
self.first_name = first_name
|
||||
self.last_name = last_name
|
||||
self.salary = salary
|
||||
self.city = city
|
||||
self.account: Account = Account(initial_deposit = initial_deposit)
|
||||
|
||||
def __eq__(self, other: "Client") -> bool:
|
||||
return self.first_name == other.first_name and\
|
||||
self.last_name == other.last_name and\
|
||||
self.account == other.account # type: ignore
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Client({self.first_name} {self.last_name})"
|
||||
|
||||
class ClientNotFound(Exception):
|
||||
def __init__(self, *args: object) -> None:
|
||||
super().__init__("Client not found")
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
45
Semaine_07/Jour_04/bank/entities/credit_cards.py
Normal file
45
Semaine_07/Jour_04/bank/entities/credit_cards.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from datetime import datetime
|
||||
from random import randint
|
||||
|
||||
from accounts import Account
|
||||
|
||||
from sqlalchemy import Column, Integer, CHAR
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from database import Base, engine
|
||||
|
||||
class CreditCard(Base):
|
||||
__tablename__ = "credit_cards"
|
||||
|
||||
id = Column(Integer, primary_key= True, autoincrement= True)
|
||||
number = Column(CHAR(19), nullable= False)
|
||||
secure_code = Column(CHAR(3), nullable= False)
|
||||
expire_date = Column(CHAR(5), nullable= False)
|
||||
|
||||
account = relationship("Account", back_populates= "credit_cards", uselist= False) # type: ignore
|
||||
|
||||
def __init__(self, account: "Account") -> None:
|
||||
self.number = self.credit_card_number()
|
||||
self.expire_date = self.expiration_date()
|
||||
self.secure_code = self.security_code()
|
||||
self.account = account
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"\
|
||||
[Numéro de carte] : {self.number}\n\
|
||||
[Date d'expiration] : {self.expire_date}\n\
|
||||
[Code de sécurité] : {self.secure_code}"
|
||||
|
||||
def credit_card_number(self):
|
||||
number = "".join(str(randint(0,9)) for _ in range(16))
|
||||
return " ".join(number[_: _ + 4] for _ in range(0,16,4))
|
||||
|
||||
def security_code(self):
|
||||
return "".join(str(randint(0,9)) for _ in range(3))
|
||||
|
||||
def expiration_date(self):
|
||||
current_date = datetime.now()
|
||||
future_date = current_date.replace(year=current_date.year + 5)
|
||||
return str(future_date.strftime("%m/%y"))
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
30
Semaine_07/Jour_04/bank/entities/services.py
Normal file
30
Semaine_07/Jour_04/bank/entities/services.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from sqlalchemy import ForeignKey, Column, Integer, String, Float, Table
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from database.connection import Base
|
||||
|
||||
# Table d'association
|
||||
client_service = Table(
|
||||
'client_service',
|
||||
Base.metadata,
|
||||
Column('client_id', Integer, ForeignKey('clients.id'), primary_key= True),
|
||||
Column('service_id', Integer, ForeignKey('clients.id'), primary_key= True)
|
||||
)
|
||||
|
||||
class Service(Base):
|
||||
__tablename__ = "Services"
|
||||
|
||||
id = Column(Integer, primary_key= True, autoincrement= True)
|
||||
name = Column(String(200), nullable= False)
|
||||
description = Column(String(500))
|
||||
monthly_price = Column(Float)
|
||||
|
||||
clients = relationship("Client", back_populates= "services", secondary= client_service)
|
||||
|
||||
def __init__(self, name, description= "", price= 0):
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.monthly_price = price
|
||||
|
||||
def __repr__(self):
|
||||
return f"Service({self.name})"
|
||||
Reference in New Issue
Block a user