mirror of
https://github.com/QualityInUse/lab-testing-maksktl.git
synced 2026-08-05 12:05:26 +03:00
Initial commit
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
class Account:
|
||||
|
||||
def __init__(self, database, customer_id=None, account_type="checking", balance=0.0):
|
||||
self.database = database
|
||||
self.balance = balance
|
||||
if customer_id:
|
||||
self.account_id = self._create_account(customer_id, account_type, balance)
|
||||
|
||||
def _create_account(self, customer_id, account_type, balance):
|
||||
return self.database.add_account(customer_id, account_type, balance)
|
||||
|
||||
def delete_account(self, account_id):
|
||||
self.database.delete_account(account_id)
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
from .database import Database
|
||||
from .transaction import Transaction
|
||||
from .customer import Customer
|
||||
from .account import Account
|
||||
|
||||
class Bank:
|
||||
def __init__(self, db_path='bank.db'):
|
||||
self.database = Database(db_path)
|
||||
self.transaction_system = Transaction(self.database)
|
||||
|
||||
def add_customer(self, name, address):
|
||||
"""Add a new customer to the bank."""
|
||||
return self.database.add_customer(name, address)
|
||||
|
||||
def update_customer_details(self, customer_id, name, address):
|
||||
"""Update details for an existing customer."""
|
||||
self.database.update_customer(customer_id, name, address)
|
||||
|
||||
def delete_customer(self, customer_id):
|
||||
"""Remove a customer and their accounts from the bank."""
|
||||
|
||||
accounts = self.database.get_customer_accounts(customer_id)
|
||||
for account in accounts:
|
||||
self.close_account(account[0])
|
||||
self.database.delete_customer(customer_id)
|
||||
|
||||
def open_account(self, customer_id, account_type, balance):
|
||||
"""Open a new account for an existing customer."""
|
||||
|
||||
account = Account(self.database, customer_id=customer_id, account_type=account_type, balance=balance)
|
||||
return account.account_id
|
||||
|
||||
def close_account(self, account_id):
|
||||
"""Close an existing account."""
|
||||
|
||||
self.database.delete_account(account_id)
|
||||
|
||||
def deposit_to_account(self, account_id, amount):
|
||||
"""Deposit money into an account."""
|
||||
self.transaction_system.deposit(account_id, amount)
|
||||
|
||||
def withdraw_from_account(self, account_id, amount):
|
||||
"""Withdraw money from an account."""
|
||||
self.transaction_system.withdraw(account_id, amount)
|
||||
|
||||
def transfer_between_accounts(self, from_account_id, to_account_id, amount):
|
||||
"""Transfer money between two accounts."""
|
||||
self.transaction_system.transfer(from_account_id, to_account_id, amount)
|
||||
|
||||
def get_customer_accounts(self, customer_id):
|
||||
"""Retrieve all accounts associated with a customer."""
|
||||
|
||||
return self.database.get_customer_accounts(customer_id)
|
||||
|
||||
def get_account_transactions(self, account_id):
|
||||
"""Get a list of transactions for a specific account."""
|
||||
|
||||
return self.database.get_transactions(account_id)
|
||||
|
||||
def get_all_customers(self):
|
||||
"""Retrieve all customers from the bank."""
|
||||
return self.database.get_all_customers()
|
||||
|
||||
def get_account(self, account_id):
|
||||
"""Retrieve details for a specific account."""
|
||||
return self.database.get_account(account_id)
|
||||
|
||||
def close_connection(self):
|
||||
self.database.close()
|
||||
@@ -0,0 +1,21 @@
|
||||
class Customer:
|
||||
def __init__(self, database, customer_id=None, name=None, address=None):
|
||||
self.database = database
|
||||
self.customer_id = customer_id
|
||||
if customer_id is None and name and address:
|
||||
self.customer_id = self.database.add_customer(name, address)
|
||||
elif customer_id:
|
||||
self._load_customer()
|
||||
|
||||
def _load_customer(self):
|
||||
details = self.database.get_customer(self.customer_id)
|
||||
if details:
|
||||
self.name, self.address = details[1], details[2]
|
||||
else:
|
||||
raise ValueError("Customer does not exist.")
|
||||
|
||||
def update_details(self, name, address):
|
||||
self.database.update_customer(self.customer_id, name, address)
|
||||
|
||||
def delete_customer(self):
|
||||
self.database.delete_customer(self.customer_id)
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
import sqlite3
|
||||
|
||||
class Database:
|
||||
def __init__(self, db_path='bank.db'):
|
||||
self.db_path = db_path
|
||||
self.conn = sqlite3.connect(self.db_path)
|
||||
self._init_db()
|
||||
|
||||
def _init_db(self):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS customers (
|
||||
customer_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
address TEXT NOT NULL
|
||||
);""")
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS accounts (
|
||||
account_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
customer_id INTEGER,
|
||||
account_type TEXT NOT NULL,
|
||||
balance REAL NOT NULL,
|
||||
FOREIGN KEY(customer_id) REFERENCES customers(customer_id)
|
||||
);""")
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS transactions (
|
||||
transaction_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
from_account_id INTEGER,
|
||||
to_account_id INTEGER,
|
||||
amount REAL NOT NULL,
|
||||
transaction_type TEXT NOT NULL,
|
||||
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY(from_account_id) REFERENCES accounts(account_id),
|
||||
FOREIGN KEY(to_account_id) REFERENCES accounts(account_id)
|
||||
);""")
|
||||
self.conn.commit()
|
||||
|
||||
def add_customer(self, name, address):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("""
|
||||
INSERT INTO customers (name, address) VALUES (?, ?)
|
||||
""", (name, address))
|
||||
self.conn.commit()
|
||||
return cursor.lastrowid
|
||||
|
||||
def update_customer(self, customer_id, name, address):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("""
|
||||
UPDATE customers SET name = ?, address = ? WHERE customer_id = ?
|
||||
""", (name, address, customer_id))
|
||||
self.conn.commit()
|
||||
|
||||
def get_customer(self, customer_id):
|
||||
"""Retrieve a customer's details by their customer ID."""
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("SELECT * FROM customers WHERE customer_id = ?", (customer_id,))
|
||||
return cursor.fetchone()
|
||||
|
||||
def delete_customer(self, customer_id):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("""
|
||||
DELETE FROM customers WHERE customer_id = ?
|
||||
""", (customer_id,))
|
||||
self.conn.commit()
|
||||
|
||||
def add_account(self, customer_id, account_type, balance):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("""
|
||||
INSERT INTO accounts (customer_id, account_type, balance) VALUES (?, ?, ?)
|
||||
""", (customer_id, account_type, balance))
|
||||
self.conn.commit()
|
||||
return cursor.lastrowid
|
||||
|
||||
|
||||
def get_account(self, account_id):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("""
|
||||
SELECT account_id, customer_id, account_type, balance FROM accounts WHERE account_id = ?
|
||||
""", (account_id,))
|
||||
return cursor.fetchone()
|
||||
|
||||
|
||||
|
||||
def get_customer_accounts(self, customer_id):
|
||||
"""Retrieve all accounts associated with a customer by their customer ID."""
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("SELECT * FROM accounts WHERE customer_id = ?", (customer_id,))
|
||||
|
||||
return cursor.fetchall()
|
||||
|
||||
def update_account_balance(self, account_id, balance):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("""
|
||||
UPDATE accounts SET balance = ? WHERE account_id = ?
|
||||
""", (balance, account_id))
|
||||
self.conn.commit()
|
||||
|
||||
def delete_account(self, account_id):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("""
|
||||
DELETE FROM accounts WHERE account_id = ?
|
||||
""", (account_id,))
|
||||
self.conn.commit()
|
||||
|
||||
def add_transaction(self, from_account_id, to_account_id, amount, transaction_type):
|
||||
self.conn.execute("INSERT INTO transactions (from_account_id, to_account_id, amount, transaction_type) VALUES (?, ?, ?, ?)",
|
||||
(from_account_id, to_account_id, amount, transaction_type))
|
||||
self.conn.commit()
|
||||
|
||||
def get_transactions(self, account_id):
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("""
|
||||
SELECT transaction_id, from_account_id, to_account_id, amount, transaction_type, timestamp
|
||||
FROM transactions
|
||||
WHERE from_account_id = ? OR to_account_id = ?
|
||||
""", (account_id, account_id,))
|
||||
return cursor.fetchall()
|
||||
|
||||
def get_all_customers(self):
|
||||
"""Retrieve all customers from the database."""
|
||||
cursor = self.conn.cursor()
|
||||
cursor.execute("SELECT * FROM customers ORDER BY customer_id ASC")
|
||||
return cursor.fetchall()
|
||||
|
||||
def close(self):
|
||||
if self.conn:
|
||||
self.conn.close()
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
from .bank import Bank
|
||||
|
||||
def print_menu():
|
||||
print("\n--- Bank System Main Menu ---")
|
||||
print("1. Add Customer")
|
||||
print("2. Update Customer Details")
|
||||
print("3. Delete Customer")
|
||||
print("4. Open Account")
|
||||
print("5. Close Account")
|
||||
print("6. Deposit")
|
||||
print("7. Withdraw")
|
||||
print("8. Transfer")
|
||||
print("9. View Customer Accounts")
|
||||
print("10. View Account Transactions")
|
||||
print("11. View All Customers")
|
||||
print("12. Exit")
|
||||
|
||||
def main():
|
||||
bank = Bank()
|
||||
while True:
|
||||
print_menu()
|
||||
choice = input("Enter your choice: ")
|
||||
|
||||
try:
|
||||
if choice == "1":
|
||||
name = input("Customer name: ")
|
||||
address = input("Customer address: ")
|
||||
customer_id = bank.add_customer(name, address)
|
||||
print(f"Customer added with ID: {customer_id}")
|
||||
|
||||
elif choice == "2":
|
||||
customer_id = int(input("Customer ID: "))
|
||||
name = input("New name: ")
|
||||
address = input("New address: ")
|
||||
bank.update_customer_details(customer_id, name, address)
|
||||
print("Customer details updated.")
|
||||
|
||||
elif choice == "3":
|
||||
customer_id = int(input("Customer ID to delete: "))
|
||||
bank.delete_customer(customer_id)
|
||||
print("Customer deleted.")
|
||||
|
||||
elif choice == "4":
|
||||
customer_id = int(input("Customer ID for new account: "))
|
||||
account_type = input("Account type (checking/savings): ")
|
||||
balance = float(input("Initial balance: "))
|
||||
account_id = bank.open_account(customer_id, account_type, balance)
|
||||
print(f"Account {account_id} opened.")
|
||||
|
||||
elif choice == "5":
|
||||
account_id = int(input("Account ID to close: "))
|
||||
bank.close_account(account_id)
|
||||
print("Account closed.")
|
||||
|
||||
elif choice == "6":
|
||||
account_id = int(input("Account ID for deposit: "))
|
||||
amount = float(input("Amount to deposit: "))
|
||||
bank.deposit_to_account(account_id, amount)
|
||||
print("Deposit successful.")
|
||||
|
||||
elif choice == "7":
|
||||
account_id = int(input("Account ID for withdrawal: "))
|
||||
amount = float(input("Amount to withdraw: "))
|
||||
bank.withdraw_from_account(account_id, amount)
|
||||
print("Withdrawal successful.")
|
||||
|
||||
elif choice == "8":
|
||||
from_account_id = int(input("From Account ID: "))
|
||||
to_account_id = int(input("To Account ID: "))
|
||||
amount = float(input("Amount to transfer: "))
|
||||
bank.transfer_between_accounts(from_account_id, to_account_id, amount)
|
||||
print("Transfer successful.")
|
||||
|
||||
elif choice == "9":
|
||||
customer_id = int(input("Customer ID to view accounts: "))
|
||||
accounts = bank.get_customer_accounts(customer_id)
|
||||
for account in accounts:
|
||||
print(account)
|
||||
|
||||
elif choice == "10":
|
||||
account_id = int(input("Account ID to view transactions: "))
|
||||
transactions = bank.get_account_transactions(account_id)
|
||||
for transaction in transactions:
|
||||
print(transaction)
|
||||
|
||||
elif choice == "11":
|
||||
customers = bank.get_all_customers()
|
||||
for customer in customers:
|
||||
print(f"ID: {customer[0]}, Name: {customer[1]}, Address: {customer[2]}")
|
||||
|
||||
elif choice == "12":
|
||||
print("Exiting the bank system.")
|
||||
break
|
||||
|
||||
else:
|
||||
print("Invalid choice, please try again.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,34 @@
|
||||
class Transaction:
|
||||
def __init__(self, database):
|
||||
self.database = database
|
||||
|
||||
def deposit(self, account_id, amount):
|
||||
if amount <= 0:
|
||||
raise ValueError("Amount must be positive.")
|
||||
account = self.database.get_account(account_id)
|
||||
if not account:
|
||||
raise ValueError("Account does not exist.")
|
||||
new_balance = account[3] + amount
|
||||
self.database.update_account_balance(account_id, new_balance)
|
||||
self.database.add_transaction(None, account_id, amount, "deposit")
|
||||
|
||||
def withdraw(self, account_id, amount):
|
||||
if amount <= 0:
|
||||
raise ValueError("Amount must be positive.")
|
||||
account = self.database.get_account(account_id)
|
||||
if account[3] < amount:
|
||||
raise ValueError("Insufficient funds.")
|
||||
new_balance = account[3] - amount
|
||||
self.database.update_account_balance(account_id, new_balance)
|
||||
self.database.add_transaction(account_id, None, amount, "withdrawal")
|
||||
|
||||
def transfer(self, from_account_id, to_account_id, amount):
|
||||
if amount <= 0:
|
||||
raise ValueError("Amount must be positive.")
|
||||
from_account = self.database.get_account(from_account_id)
|
||||
to_account = self.database.get_account(to_account_id)
|
||||
if from_account[3] < amount:
|
||||
raise ValueError("Insufficient funds in the source account.")
|
||||
self.database.update_account_balance(from_account_id, from_account[3] - amount)
|
||||
self.database.update_account_balance(to_account_id, to_account[3] + amount)
|
||||
self.database.add_transaction(from_account_id, to_account_id, amount, "transfer")
|
||||
Reference in New Issue
Block a user