ข้ามไปที่เนื้อหาหลัก

OOP 22023

 https://github.com/omiejung01/PythonOO2_2023/tree/master01

https://github.com/omiejung01/PythonOO2_2023/tree/master02


Master 1

PythonOO2_2023/walletapp/data.py


class Account:
    def __init__(self,account_name,opening_balance):
        self.account_name = account_name
        self.balance = opening_balance
    #Constructor

    def display(self):
        # Balance Inquiry
        return self.account_name + ' ' + f"{self.balance:,.2f}"

    def setAccountName(self, new_name):
        self.account_name = new_name

class Character:
    def __init__(self, character_name, vision, weapon_type):
        self.__character_name = character_name
        self.__vision = vision
        self.__weapon = None
        self.__weapon_type = weapon_type


    def display(self):
        #self.__weapon.display()
        weapon_name = ''
        if self.__weapon is not None:
            weapon_name = self.__weapon.getWeaponName()

        print(self.__character_name + ' ' + self.__vision + ' ' + weapon_name)

    def setCharacterName(self, new_name):
        self.__character_name = new_name

    def getCharacterName(self):
        return self.__character_name

    def getWeaponType(self):
        return self.__weapon_type

    def setWeapon(self, weapon):
        if weapon.getWeaponType() == self.__weapon_type:
            self.__weapon = weapon
            print('Equipped!!!')
        else:
            print(self.__character_name + ' cannot use ' + weapon.getWeaponName())



class Weapon:
    def __init__(self,weapon_name, weapon_type):
        self.__weapon_name = weapon_name
        self.__weapon_type = weapon_type
        self.__level = 1

    def display(self):
        print(self.__weapon_name + ' ' + self.__weapon_type +' ' + str(self.__level))

    def getWeaponType(self):
        return self.__weapon_type

    def getWeaponName(self):
        return self.__weapon_name


def run():
    dull_blade = Weapon('Dull Blade','Sword')
    #dull_blade.display()

    #raiden_character = Character('Raiden Shogun', 'Electro', dull_blade)
    #raiden_character.display()  # Display “Raiden Shogun Electro” on the screen

    #raiden_character.__character_name = 'Yae Miko'
    # This is not working because encapsulation (Information Hiding)
    #raiden_character.setCharacterName('Yae Miko')

    raiden_character = Character('Raiden Shogun', 'Electro', 'Polearm')

    amos_bow = Weapon('Amos Bow','Bow')

    raiden_character.setWeapon(amos_bow)
    raiden_character.display()

    engulfing_lightning = Weapon('Engulfing Lightning','Polearm')
    raiden_character.setWeapon(engulfing_lightning)
    raiden_character.display()

    #raiden_character.display()
    #print(raiden_character.getCharacterName())

    # raiden_character.character_name = 'Yae Miko'
    # raiden_character.display()

    # List
    my_list = ['Dehya', 'Candace']
    print (len(my_list))
    my_list.append('Cyno')
    print(len(my_list))

    # 1st Element
    print(my_list[0])

    # Last element
    print(my_list[len(my_list)-1])


    navia_character = {
        "Name": "Navia",
        "weapon_type": "Claymore",
        "vision": 'Geo'
    }

    print(navia_character['Name'])
    print(navia_character['weapon_type'])


PythonOO2_2023/main.py

import walletapp.data as data

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    data.run()

# See PyCharm help at https://www.jetbrains.com/help/pycharm/



Master 2

PythonOO2_2023/walletapp/app.py


from datetime import datetime

class Account:
    def __init__(self, account_name, opening_balance):
        self.__account_name = account_name
        self.__balance = opening_balance
        self.__created_date = datetime.now()
        self.__activated = True

    def display(self):
        print(self.__account_name + ' has an account balance: ' + str(self.__balance))

    def balanceInquiry(self):
        return self.__balance


class CreditCard(Account):
    def __init__(self, credit, account_name, opening_balance):
        self.__credit_limit = credit
        self.__due_date = None
        self.__extended_limit = 0
        self.__extended_until_date = None
        super().__init__(account_name, opening_balance)

    def display(self):
        #print('Hello World2')
        super().display()
        print('credit limit: ' + str(self.__credit_limit))
        #print(super().__account_name + ' has an account balance: ' + str(super().__balance))

    def display_extended(self):
        print('This is for extended account')


def run():
    bank_account01 = Account('Mr.A', 15000.00)
    bank_account01.display()

    cc_account01 = CreditCard(20000,'Mr.B',0)
    cc_account01.display()

    #Polymorph #1

    print ('A' + 'B' + 'C')
    print (1 + 2 + 3)

    v1 = 1
    v2 = '2'
    v3 = 3

    #print (v1 + int(v2) + v3)

    s1 = 'Hello Universe'
    s2 = [1,2,3,'aaa',5,8,9]

    print(len(s1))
    print(len(s2))

class A:
    def __init__(self,name,last_name):
        self.__name = name
        self.__last_name = last_name

    def display(self):
        print(self.__name + ' ' + self.__last_name)


class B:
    def __init__(self, name, credit):
        self.__name = name
        self.__credit = credit

    def display(self):
        print(self.__name + ' ' + str(self.__credit))


def run2():
    bank_account01 = Account('Mr.A', 15000.00)

    cc_account01 = CreditCard(20000,'Mr.B',0)

    list1 = [bank_account01, cc_account01]
    for i in list1:
        i.display()

    list2 = [A('Mr.C','Henman'), B('Mr.M',2000)]
    for j in list2:
        j.display()

def my_sum(args):
    # input can be string, list of int, or dictionary of int
    # Polymorphism #2 (data type)
    sum = 0
    if type(args) == str:
        numbers = args.split()
        for n in numbers:
            sum += int(n)

    if type(args) == list:
        for n in args:
            sum += n

    if type(args) == dict:
        vals = args.values()
        for n in vals:
            sum += n

    return sum


def run3():
    print(my_sum('10 20 30'))
    print(my_sum([10, 20, 31]))
    num = {
        '#1': 10,
        '#2': 20,
        '#3': 32
    }
    print(my_sum(num))


def display_name(firstname, lastname = '', title = ''):
    # this method can have one, two and three arguments !!
    # Polymorphism #3 (default argument)
    if title == '':
        title = 'Khun'

    if lastname == '':
        return title + ' ' + firstname + '\n'
    else:
        return title + ' ' + firstname + ' ' + lastname + '\n'

def run4():
    print(display_name('Albedo'))
    print(display_name('Eula', 'Lawrence'))
    print(display_name('Prinzessin', 'der Verurteilung!', 'mein Fräuleins'))






ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

OOPT01

 Lists: Lists are ordered collections that can contain elements of different types. They are mutable, meaning you can change their elements. Example: python Copy code my_list = [1, 2, "apple", True] my_list.append(5)  # Adding an element print(my_list[2])  # Accessing an element by index my_list.remove("apple")  # Removing an element print(my_list)  # Printing the modified list Dictionary: Dictionaries are unordered collections of key-value pairs. They use keys to access their associated values and are mutable. Example: python Copy code my_dict = {"name": "Alice", "age": 25, "city": "New York"} print(my_dict["age"])  # Accessing value by key my_dict["occupation"] = "Engineer"  # Adding a new key-value pair del my_dict["city"]  # Deleting a key-value pair print(my_dict)  # Printing the modified dictionary Ranges: Ranges are sequences of numbers used for iterating over a sequence...

Object Final

 Final Examination OBJECT You will be given the "project name" and django project name. Inheritance 1 question Polymorphism 1 question (main.py) Django database 1 question Rest API → List items 1 question Rest API → Item detail 1 question Rest API → Transaction 1 question (Wallet App) ----------------------------------------------------------------------------------------------------------------------------- pip install django django-admin startproject mysite_project py manage.py startapp catalog_app  Create the database models in terminal py manage.py makemigrations py manage.py migrate py manage.py createsuperuser Rest API pip install djangorestframework pip install markdown       pip install django-filter pip install virtualenv --------------------------------------------------------------------------------------------------------------------------- พยายามติดตั้ง  py manage.py makemigrations py manage.py migrate ทุกครั้งหลังจาก เปลี่ยนรูปแบบโค้ด ตัว...

OPPTH

 https://github.com/kongruksiamza/python-oop