简体   繁体   English

我能否以某种方式合并这些类,这样我就不必一次又一次地编写它,并且我的 python 代码不会变得多余

[英]Can I somehow merge these classes such that I don't have to write this again and again and my python code doesn't get redundant

import datetime

# I AM CREATING A BIKE RENTAL SYSTEM USING OOP IN PYTHON AND SOMEHOW I SEE MYSELF REPEATING SAME CONTENTS IN METHODS, CAN I SOMEHOW WRITE THIS IN BETTER FORMAT WHICH ISNT REDUNDANT? (I AM NEW TO OOP)

class BikeRental:

    def __init__(self, stock=0):
        #creating instances of bike rental shop
        self.stock = stock

    def displayStock(self):
        #displaying currently available bikes to rent
        print(f'We currently have {self.stock} bikes available for rent')
        return self.stock

    def BikeOnHourly(self, input):
        #rents bike on hourly basis
        if input < 0:
            print('Number of bikes should be positive!')
            return None

        elif input > self.stock:
            print(f'Sorry we have {self.stock} bikes available right now!')
            return None

        else:
            now = datetime.datetime.now()
            print(f'you have rented {input} bike/bikes on hourly basis today'
                  f'at {now.hour} on {now.date()}')
            print('you will be charged 5$ per bike per hour.')
            print('Have a great and healthy day!')

            self.stock -= input
            return now

    def BikeOnDailyBasis(self, n):

        if n< 0:
            print('Number of bikes should be positive!')
            return None
        elif n >self.stock:
            print(f'Sorry we have {self.stock} bikes available right now!')
        else:
            now = datetime.datetime.now()
            print(f'you have rented {n} bike/bikes on hourly basis today'
                  f'at {now.hour} on {now.date()}')
            print('you will be charged 5$ per bike per hour.')
            print('Have a great and healthy day!')

            self.stock -= n
            return now

I'm assuming you are asking about repeating the function, not the class.我假设您要问的是重复 function,而不是 class。 If so, something like this might be helpful, with the idea being to have different functions call the one BikeOnBasis function with a varying parameter basis.如果是这样,这样的事情可能会有所帮助,其想法是让不同的函数调用具有不同参数基础的 BikeOnBasis function。 Also see polymorphism for info on doing this type of thing in more structured ways.另请参阅多态性以获取有关以更结构化的方式执行此类操作的信息。

import datetime

# I AM CREATING A BIKE RENTAL SYSTEM USING OOP IN PYTHON AND SOMEHOW I SEE MYSELF REPEATING SAME CONTENTS IN METHODS, CAN I SOMEHOW WRITE THIS IN BETTER FORMAT WHICH ISNT REDUNDANT? (I AM NEW TO OOP)

class BikeRental:

    def __init__(self, stock=0):
        #creating instances of bike rental shop
        self.stock = stock

    def displayStock(self):
        #displaying currently available bikes to rent
        print(f'We currently have {self.stock} bikes available for rent')
        return self.stock

    def BikeOnHourly(self, n):
        self.BikeOnBasis(n, 'hourly')

    def BikeOnDailyBasis(self, n):
        self.BikeOnBasis(n, 'daily')

    def BikeOnBasis(self, n, basis):
        if n< 0:
            print('Number of bikes should be positive!')
            return None
        elif n > self.stock:
            print(f'Sorry we have {self.stock} bikes available right now!')
        else:
            now = datetime.datetime.now()
            print(f'you have rented {n} bike/bikes on {basis} basis today'
                  f'at {now.hour} on {now.date()}')
            print('you will be charged 5$ per bike per hour.')
            print('Have a great and healthy day!')

            self.stock -= n
            return now

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写代码以创建多个函数但我不必一次又一次地编写相同的代码? - How can I write the code in such a way that multiple functions get created but I don't have to write the same code again and again? 如何编辑我的 VS Code 环境,以便我可以为我的 Python 代码预设输入数据,这样我就不必一次又一次地输入数据 - How do I edit my VS Code environment so that I can preset Input data for my Python Code so that I don't have to input data again and again Python3我的代码不想再次输入 - Python3 My Code Doesn't want input again 我的python代码运行了几次,但是一旦我关闭计算机或执行其他操作,它就不会再次运行 - My python code runs a few times but as soon as I close my computer or do something else, it doesn't run again 我不得不帮助我理解我做错了(Python,再次) - I can't but help get the idea I'm doing it all wrong (Python, again) 如何让python代码一遍又一遍地运行? - How can I get a python code to run over and over again? 我的代码以某种方式乘以 0.5 倍,我不知道为什么 - My code somehow multiplies by a factor of 0.5 and I don't know why 我如何从树莓派获取代码并将其存储在较小的芯片上,所以我不必一遍又一遍地使用pi - How do I take code from a raspberry pi and store it onto a smaller chip so I don't have to use the pi over and over again 我无法在 Python 3.9 代码中读取/写入文件 - I can't read/write a file in my Python 3.9 code 为什么python线程一旦完成就不能再次运行? - Why can't python threads run again once they have finished?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM