Last Updated on April 5, 2023 by mishou

I. Creating classes

The following code creates an Asset class to represent individual assets, and an AssetManager class to manage a collection of assets.

class Asset:
    def __init__(self, name, category, value):
        self.name = name
        self.category = category
        self.value = value

class AssetManager:
    def __init__(self):
        self.assets = []

    def add_asset(self, name, category, value):
        asset = Asset(name, category, value)
        self.assets.append(asset)

    def remove_asset(self, name):
        for asset in self.assets:
            if asset.name == name:
                self.assets.remove(asset)
                return True
        return False

    def get_assets_by_category(self, category):
        assets = []
        for asset in self.assets:
            if asset.category == category:
                assets.append(asset)
        return assets

    def get_total_value(self):
        total_value = 0
        for asset in self.assets:
            total_value += asset.value
        return total_value

    def get_total_value_by_category(self, category):
        total_value = 0
        for asset in self.assets:
            if asset.category == category:
                total_value += asset.value
        return total_value

II. Instantiating objects

You can create an instance of the AssetManager class, add assets to it, and then use its methods to manage and analyze your assets.

# Create an instance of AssetManager
assets_202212 = AssetManager()
# Add assets
assets_202212.add_asset("Car", "Vehicle",           1_000000)
assets_202212.add_asset("House1", "Property",      10_000000)
assets_202212.add_asset("House2", "Property",      10_000000)
assets_202212.add_asset("BankA", "Deposit",        10_000000)
assets_202212.add_asset("BankB", "Deposit",        10_000000)
assets_202212.add_asset("BankC", "Deposit",        10_000000)

assets_202212.add_asset("BankD", "Deposit",        10_000000)
assets_202212.add_asset("BankE", "Deposit",        10_000000)

assets_202212.add_asset("BankA", "Investment",      8_000000)
assets_202212.add_asset("BankB", "Investment",      1_500000)
assets_202212.add_asset("SecurityA", "Investment",  5_000000)

# Create an instance of AssetManager
assets_202303 = AssetManager()
# Add assets
assets_202303.add_asset("Car", "Vehicle",           1_000000)
assets_202303.add_asset("House1", "Property",      10_000000)
assets_202303.add_asset("House2", "Property",      10_000000)
assets_202303.add_asset("BankA", "Deposit",        10_000000)
assets_202303.add_asset("BankB", "Deposit",        10_000000)
assets_202303.add_asset("BankC", "Deposit",        10_000000)

assets_202303.add_asset("BankA", "Investment",      8_000000)
assets_202303.add_asset("BankB", "Investment",      1_500000)
assets_202303.add_asset("SecurityA", "Investment",  5_000000)

assets_202303.add_asset("SecurityB", "Investment", 10_000000)
assets_202303.add_asset("SecurityC", "Investment", 15_000000)

III. Visualizing assets

pie chart on Google Colaboratory

You can see the code here:

https://colab.research.google.com/drive/1rigReUyxtVXUlVWYjuLeZbxrdhQuanOV?usp=sharing

By mishou

Leave a Reply

Your email address will not be published. Required fields are marked *