Home > OS >  Problem in converting procedure oriented script to object oriented programing script
Problem in converting procedure oriented script to object oriented programing script

Time:05-09

I want to convert (the original script)Procedure Oriented Script to Object Oriented, but I tried many times in many ways and I have failed every single time I will write the original script then i will write my try

Requirements

  1. Bike attributes (the description and the bike cost and sale price and the bike condition).

  2. Update the bike cost from 500 to 350.

Procedure Oriented:

def update_sale_price(bike, sale_price):
   if bike['sold'] == True:
       print('Action not allowed, Bike has already been sold')
   else:
       bike['sale_price'] = sale_price


def sell(bike):
    bike['sold'] = True


def create_bike(description, cost, sale_price, condition):
    return {
        'description': description,
        'cost': cost,
        'sale_price': sale_price,
        'condition': condition,
        'sold': False
    }


bike1 = create_bike('Univega Alpina, orange', cost=100, sale_price=500, condition=0.5)
update_sale_price(bike1, 350)
sell(bike1)
print(bike1)

Object Oriented (My try):

class create_bike:
    def __init__(self, sale_price, description, cost, condition):
        self.sale_price = sale_price
        self.description = description
        self.cost = cost
        self.condition = condition

class update_sell_price:
    def __init__ (self, bike, sale_price):
        self.bike = bike
        self.sale_price = sale_price


bike1 = create_bike(description='Univega Alpina, orange', cost=100, sale_price=500, condition=0.5)

up = update_sell_price(bike=bike1, sale_price=350)

print(up)

CodePudding user response:

The conceptual "object" in your code is the bike dict -- what you should do is create a single class that contains the data from the bike dict, and make the functions methods of that class.

from dataclasses import dataclass

@dataclass
class Bike:
    description: str
    cost: int
    sale_price: int
    condition: float
    sold = False

    def update_sale_price(self, sale_price):
        if self.sold:
            print('Action not allowed, Bike has already been sold')
        else:
            self.sale_price = sale_price

    def sell(self):
        self.sold = True

    def __str__(self):
        return str(self.__dict__)


bike1 = Bike('Univega Alpina, orange', cost=100, sale_price=500, condition=0.5)
bike1.update_sale_price(350)
bike1.sell()
print(bike1)

CodePudding user response:

According to the Wikipedia article on Object Oriented Programming, "[It] is a programming paradigm based on the concept of "objects", which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods)."

Taken literally, all that means is that you should make any functions you had that affected any of the value in your bike dictionary into methods of a Bike class. In Python that's not strictly true, but understanding why is a somewhat advanced subject and not extremely germane to the core question you're asking.

Here's how to apply the stated principle to your procedurally programming and make it object-oriented. Note that I've add a special __repr__() method that will control how class instances "officially" represent themselves as strings.

class Bike:
    def __init__(self, sale_price, description, cost, condition, sold=False):
        self.sale_price = sale_price
        self.description = description
        self.cost = cost
        self.condition = condition
        self.sold = sold

    def __repr__(self):
        classname = type(self).__name__
        attributes = ', '.join(f'{name}={value!r}' for name, value in vars(self).items())
        return f'{classname}({attributes})'

    def sell(self):
        self.sold = True

    def update_sell_price(self, sale_price):
        self.sale_price = sale_price


bike1 = Bike(description='Univega Alpina, orange', cost=100, sale_price=500, condition=0.5)
bike1.update_sell_price(sale_price=350)
bike1.sell()
print(bike1)
  • Related