Python

초보자를 위한 파이썬 300제 / 11. 파이썬 클래스 / 271 ~ 280

TTwY 2020. 9. 22. 16:08
728x90
반응형

11. 파이썬 클래스 / 271 ~ 280

 

# 파이썬 300제 _ 271번 ~ 280번
import random

class Account:
    account_count = 0
    
    def __init__(self, name, balance):
        self.deposit_count = 0
        self.deposit_log = []
        self.withdraw_log = []
        
        self.name = name
        self.balance = balance
        self.bank = "SC은행"

        num1 = random.randint(0, 999)
        num2 = random.randint(0, 99)
        num3 = random.randint(0, 999999)

        num1 = str(num1).zfill(3)
        num2 = str(num2).zfill(2)
        num3 = str(num3).zfill(6)

        self.account_number = num1 + "-" + num2 + "-" + num3
        Account.account_count += 1

    def deposit(self, amount):
        if amount >= 1:
            self.deposit_log.append(amount)
            self.balance += amount

            self.deposit_count += 1

            if self.deposit_count % 5 == 0:
                self.balance = (self.balance * 1.01)
    
    def withdraw(self, amount):
        if self.balance > amount:
            self.withdraw_log.append(amount)
            self.balance -= amount

    def display_info(self):
        print("은행이름 : {0}".format(self.bank))
        print("예금주 : {0}".format(self.name))
        print("계좌번호 : {0}".format(self.account_number))
        print("잔고 : {0}".format(self.balance))
    
    def deposit_history(self):
        for amount in self.deposit_log:
            print(amount)
    
    def withdraw_history(self):
        for amount in self.withdraw_log:
            print(amount)

    @classmethod
    def get_account_num(cls):
        print(cls.account_count)

# data = []
# p = Account("PARK", 1000000)
# k = Account("KIM", 10000)
# j = Account("JUNG", 50000000)

# data.append(p)
# data.append(k)
# data.append(j)

# for i in data:
#     if i.balance >= 1000000:
#         i.display_info()
k = Account("Kim", 1000)
k.deposit(100)
k.deposit(200)
k.deposit(300)
k.deposit_history()

k.withdraw(100)
k.withdraw(200)
k.withdraw_history()
728x90
반응형