Loading...
Vietnam Geography App
Loading...
Vietnam Geography App
Làm chủ lập trình hướng đối tượng! Tạo class, object, kế thừa, đóng gói dữ liệu và xây dựng hệ thống phần mềm chuyên nghiệp. Đây là chìa khóa để phát triển ứng dụng lớn và hiện đại.
Hệ thống quản lý tài khoản ngân hàng với OOP
class BankAccount:
def __init__(self, account_number, owner_name, initial_balance=0):
self._account_number = account_number
self._owner_name = owner_name
self._balance = initial_balance
self._transaction_history = []
self._add_transaction("Mở tài khoản", initial_balance)
def deposit(self, amount):
if amount <= 0:
print("Số tiền phải > 0!")
return False
self._balance += amount
self._add_transaction("Nạp tiền", amount)
print(f"Đã nạp {amount:,.0f}đ. Số dư: {self._balance:,.0f}đ")
return True
def withdraw(self, amount):
if amount <= 0:
print("Số tiền phải > 0!")
return False
if amount > self._balance:
print("Số dư không đủ!")
return False
self._balance -= amount
self._add_transaction("Rút tiền", -amount)
print(f"Đã rút {amount:,.0f}đ. Số dư: {self._balance:,.0f}đ")
return True
def transfer(self, amount, target_account):
if self.withdraw(amount):
target_account.deposit(amount)
self._add_transaction(f"Chuyển tiền đến {target_account._account_number}", -amount)
target_account._add_transaction(f"Nhận tiền từ {self._account_number}", amount)
print(f"Chuyển thành công {amount:,.0f}đ")
return True
return False
def _add_transaction(self, description, amount):
from datetime import datetime
self._transaction_history.append({
'time': datetime.now(),
'description': description,
'amount': amount,
'balance': self._balance
})
def get_balance(self):
return self._balance
def get_statement(self):
print(f"\n=== SỔ SAO KÊ TÀI KHOẢN {self._account_number} ===")
print(f"Chủ tài khoản: {self._owner_name}")
print(f"Số dư hiện tại: {self._balance:,.0f}đ")
print("\nLịch sử giao dịch:")
for transaction in self._transaction_history[-10:]: # 10 giao dịch gần nhất
time_str = transaction['time'].strftime("%d/%m/%Y %H:%M")
amount = transaction['amount']
amount_str = f"{amount:+,.0f}đ"
print(f"{time_str} | {transaction['description']: <20} | {amount_str: >12} | {transaction['balance']:,.0f}đ")
class SavingsAccount(BankAccount):
def __init__(self, account_number, owner_name, initial_balance=0, interest_rate=0.05):
super().__init__(account_number, owner_name, initial_balance)
self._interest_rate = interest_rate
def calculate_interest(self):
interest = self._balance * self._interest_rate / 12 # Lãi tháng
self.deposit(interest)
print(f"Đã cộng lãi: {interest:,.0f}đ (lãi suất {self._interest_rate*100}%/năm)")
# Demo system
def demo_bank_system():
# Tạo tài khoản
acc1 = BankAccount("001", "Nguyễn Văn A", 1000000)
acc2 = SavingsAccount("002", "Trần Thị B", 2000000, 0.06)
# Thực hiện giao dịch
acc1.deposit(500000)
acc1.withdraw(200000)
acc1.transfer(300000, acc2)
# Tính lãi cho tài khoản tiết kiệm
acc2.calculate_interest()
# In sao kê
acc1.get_statement()
acc2.get_statement()
demo_bank_system()
Software architecture design
Game development với entities
Web framework components
Database ORM models
API client libraries
GUI application structure