└── OOP Codes ├── abstract class.py ├── classes.py ├── encapsulation.py ├── final_project.py ├── inheritance.py ├── overriding.py ├── polymorphism.py ├── proj_2_inheritance.py ├── project_1_calculator.py └── project_codes ├── main.py └── rent.py /OOP Codes/abstract class.py: -------------------------------------------------------------------------------- 1 | from abc import ABC, abstractmethod 2 | 3 | class Animal(ABC): # super class 4 | 5 | @abstractmethod 6 | def walk(self): pass 7 | 8 | def run(self): pass 9 | 10 | class Bird(Animal): # sub class 11 | 12 | def __init__(self): 13 | print("bird") 14 | 15 | def walk(self): 16 | print("walk") 17 | 18 | 19 | b1 = Bird() -------------------------------------------------------------------------------- /OOP Codes/classes.py: -------------------------------------------------------------------------------- 1 | # integer = 10 2 | # string = "messi" "barcelona football team" 3 | # %% 4 | integer1 = 33 5 | string1 = "messi" 6 | 7 | # %% classes 8 | 9 | employee1_name = "messi" 10 | employee1_age = 33 11 | employee1_address = "asdasdas" 12 | 13 | class Employee: 14 | # attribute = age, address, name 15 | # behaviour = pass 16 | pass 17 | 18 | employee1 = Employee() 19 | 20 | # %% attribute 21 | 22 | class Footballer: 23 | 24 | football_club = "barcelona" 25 | age = 30 26 | 27 | f1 = Footballer() 28 | 29 | print(f1) 30 | print(f1.age) 31 | print(f1.football_club) 32 | 33 | f1.football_club = "real madrid" 34 | 35 | print(f1.football_club) 36 | 37 | # %% methods 38 | class Square(object): 39 | 40 | edge = 5 # meter 41 | area = 0 42 | 43 | def area1(self): 44 | self.area = self.edge * self.edge # 5*5 45 | print("Area: ",self.area) 46 | 47 | ############### 48 | s1 = Square() 49 | 50 | print(s1) 51 | print(s1.edge) 52 | 53 | print(s1.area1()) 54 | 55 | s1.edge = 7 56 | s1.area1() 57 | 58 | # %% methods vs funtions 59 | 60 | class Emp(object): 61 | 62 | age = 25 # 63 | salary = 1000 # $ 64 | 65 | def ageSalaryRatio(self): 66 | a = self.age / self.salary 67 | print("method: ", a) 68 | 69 | e1 = Emp() 70 | e1.ageSalaryRatio() 71 | #╦ ------------------------------------------------------ 72 | # function 73 | def ageSalaryRatio(age, salary): 74 | a = age / salary 75 | print("function: ",a) 76 | 77 | ageSalaryRatio(30, 3000) 78 | 79 | # 80 | def findArea(a, b): # a = pi, b = r 81 | area = a*b**2 82 | # print(area) 83 | return area 84 | 85 | pi = 3.14 86 | r = 5 87 | 88 | result1 = findArea(pi, r) 89 | print(result1) 90 | result2 = findArea(pi, 10) 91 | 92 | print(result1 + result2) 93 | 94 | # %% initializer or contructor 95 | 96 | class Animal(object): 97 | 98 | def __init__(self, a, b): # ( name, age) = ("dog", 2) = (a, b) 99 | self.name = a 100 | self.age = b 101 | 102 | def getAge(self): 103 | print("") 104 | return self.age 105 | 106 | def getName(self): 107 | print(self.name) 108 | 109 | a1 = Animal("dog", 2) 110 | a2 = Animal("cat",4) 111 | a3 = Animal("bird", 6) 112 | 113 | -------------------------------------------------------------------------------- /OOP Codes/encapsulation.py: -------------------------------------------------------------------------------- 1 | class BankAccount(object): 2 | 3 | def __init__(self, name, money, address): 4 | self.name = name # global 5 | self.__money = money # private 6 | self.address = address 7 | 8 | # get and set global 9 | def getMoney(self): 10 | return self.__money 11 | 12 | def setMoney(self, amount): 13 | self.__money = amount 14 | 15 | # private 16 | def __increase(self): 17 | self.__money = self.__money + 500 18 | 19 | # 20 | p1 = BankAccount("messi", 1000, "barcelona") 21 | p2 = BankAccount("neymar", 2000, "paris") 22 | 23 | print("get method:",p1.getMoney()) 24 | # print("get method:",p1.__money) 25 | p1.setMoney(5000) 26 | print("after set method:",p1.getMoney()) 27 | 28 | #p1.__increase() 29 | #print("after raise: ",p1.getMoney()) 30 | -------------------------------------------------------------------------------- /OOP Codes/final_project.py: -------------------------------------------------------------------------------- 1 | """ 2 | OOP: Object Oriented Programming 3 | - class/object 4 | - attributes/methods 5 | - encapsulation/ abstraction 6 | - inheritance 7 | - overriding/polymorphism 8 | """ 9 | from abc import ABC, abstractmethod 10 | # inheritance 11 | class Shape(ABC): 12 | """ 13 | Shape = super class / abstract class 14 | 15 | """ 16 | # abstract method 17 | @abstractmethod 18 | def area(self): pass 19 | @abstractmethod 20 | def perimeter(self): pass 21 | 22 | # overriding and polymorphism 23 | def toString(self): pass 24 | 25 | # child 26 | class Square(Shape): 27 | "sub class" 28 | def __init__(self, edge): 29 | self.__edge = edge # encapsulation private attribute 30 | 31 | def area(self): 32 | result = self.__edge**2 33 | print("Square area: ",result) 34 | 35 | def perimeter(self): 36 | result = self.__edge*4 37 | print("Square perimeter: ",result) 38 | 39 | # override and polymorphism 40 | def toString(self): 41 | print("Square edge: ",self.__edge) 42 | 43 | # child 44 | class Circle(Shape): 45 | "circle class" 46 | 47 | # constant variable 48 | PI = 3.14 49 | 50 | def __init__(self, radius): 51 | self.__radius = radius 52 | 53 | def area(self): 54 | "" 55 | result = self.PI*self.__radius**2 # pi*r^2 56 | print("Circle area: ",result) 57 | 58 | def perimeter(self): 59 | result = 2*self.PI*self.__radius # 2*pi*r 60 | print("Circle perimeter: ",result) 61 | 62 | # override and polymorphism 63 | def toString(self): 64 | print("Circle radius: ",self.__radius) 65 | 66 | 67 | c = Circle(5) 68 | c.area() 69 | c.perimeter() 70 | c.toString() 71 | 72 | 73 | s = Square(5) 74 | s.area() 75 | s.perimeter() 76 | s.toString() -------------------------------------------------------------------------------- /OOP Codes/inheritance.py: -------------------------------------------------------------------------------- 1 | # parent 2 | class Animal: 3 | def __init__(self): 4 | print("animal is created") 5 | 6 | def toString(self): 7 | print("animal") 8 | 9 | def walk(self): 10 | print("animal walk") 11 | 12 | # child 13 | class Monkey(Animal): 14 | def __init__(self): 15 | super().__init__() # use init of parent(animal) class 16 | print("monkey is created") 17 | 18 | def toString(self): 19 | print("monkey") 20 | 21 | def climb(self): 22 | print("monkey can climb") 23 | 24 | class Bird(Animal): 25 | def __init__(self): 26 | super().__init__() 27 | print("bird is created") 28 | 29 | def fly(self): 30 | print("fly") 31 | # 32 | m1 = Monkey() 33 | m1.toString() 34 | m1.walk() 35 | m1.climb() 36 | print("----") 37 | b1 = Bird() 38 | b1.walk() 39 | # b1.climb() 40 | b1.fly() -------------------------------------------------------------------------------- /OOP Codes/overriding.py: -------------------------------------------------------------------------------- 1 | class Animal: # parent 2 | 3 | def toString(self): 4 | print("animal") 5 | 6 | class Monkey(Animal): 7 | 8 | def toString(self): 9 | print("monkey") 10 | 11 | a1 = Animal() 12 | a1.toString() 13 | 14 | m1 = Monkey() 15 | m1.toString() # monkey calls overriding method -------------------------------------------------------------------------------- /OOP Codes/polymorphism.py: -------------------------------------------------------------------------------- 1 | class Employee: 2 | 3 | def raisee(self): 4 | raise_rate = 0.1 5 | result = 100 + 100 * raise_rate 6 | print("Employee: ",result) 7 | 8 | class CompEng(Employee): 9 | 10 | def raisee(self): 11 | raise_rate = 0.2 12 | result = 100 + 100 * raise_rate 13 | print("CompEng: ",result) 14 | 15 | class EEE(Employee): 16 | 17 | def raisee(self): 18 | raise_rate = 0.3 19 | result = 100 + 100 * raise_rate 20 | print("EEE: ",result) 21 | 22 | e1 = Employee() 23 | 24 | ce = CompEng() 25 | 26 | eee = EEE() 27 | 28 | employee_list = [ce, eee] 29 | 30 | for employee in employee_list: 31 | employee.raisee() 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /OOP Codes/proj_2_inheritance.py: -------------------------------------------------------------------------------- 1 | class Website: 2 | "parent" 3 | def __init__(self, name, surname): 4 | self.name = name 5 | self.surname = surname 6 | 7 | def loginInfo(self): 8 | print(self.name + " "+ self.surname ) 9 | 10 | class Website1(Website): 11 | "child" 12 | def __init__(self, name, surname, ids): 13 | Website.__init__(self, name, surname) 14 | self.ids = ids 15 | 16 | def login(self): 17 | print(self.name + " "+ self.surname + " "+self.ids) 18 | class Website2(Website): 19 | 20 | def __init__ (self, name, surname, email): 21 | Website.__init__(self,name,surname) 22 | self.email = email 23 | 24 | def login(self): 25 | print(self.name + " "+ self.surname + " "+self.email) 26 | 27 | p1 = Website("name","surname") 28 | p2 = Website1("name","surname", "123") 29 | p3 = Website2("name","surname", "email@") -------------------------------------------------------------------------------- /OOP Codes/project_1_calculator.py: -------------------------------------------------------------------------------- 1 | # calculator project 2 | # class -> init -> method/attribute -> funct vs method 3 | 4 | class Calc(object): 5 | "calculator" 6 | # init metodu 7 | def __init__(self, a, b): 8 | "initialize values" 9 | 10 | # attribute 11 | self.value1 = a 12 | self.value2 = b 13 | 14 | def add(self): 15 | "addition a+b = result -> return result" 16 | return self.value1 + self.value2 17 | 18 | def multiply(self): 19 | "multiplication a*b = result -> return result" 20 | return self.value1 * self.value2 21 | 22 | def division(self): 23 | "division a/b = result -> return result" 24 | return self.value1 / self.value2 25 | 26 | print("Choose add(1), multiply(2), div(3)") 27 | selection = input("select 1 or 2 or 3") 28 | 29 | v1 = int(input("first value")) 30 | v2 = int(input("second value")) 31 | 32 | c1 = Calc(v1,v2) 33 | if selection == "1": 34 | add_result = c1.add() 35 | print("Add: {}".format(add_result)) 36 | elif selection == "2": # else if = elif 37 | multiply_result = c1.multiply() 38 | print("Multiply: {}".format( multiply_result)) 39 | elif selection == "3": 40 | div_result = c1.division() 41 | print("Div: {}".format(div_result)) 42 | else: 43 | print("Error there is no proper selection") -------------------------------------------------------------------------------- /OOP Codes/project_codes/main.py: -------------------------------------------------------------------------------- 1 | from rent import CarRent, BikeRent, Customer 2 | 3 | bike = BikeRent(100) 4 | car = CarRent(10) 5 | customer = Customer() 6 | 7 | main_menu = True 8 | 9 | while True: 10 | 11 | if main_menu: 12 | print(""" 13 | ***** Vehicle Rental Shop***** 14 | A. Bike Menu 15 | B. Car Menu 16 | Q. Exit 17 | """) 18 | main_menu = False 19 | 20 | choice = input("Enter choice: ") 21 | 22 | if choice == "A" or choice == "a": 23 | 24 | print(""" 25 | ****** BIKE MENU***** 26 | 1. Display available bikes 27 | 2. Request a bike on hourly basis $ 5 28 | 3. Request a bike on daily basis $ 84 29 | 4. Return a bike 30 | 5. Main Menu 31 | 6. Exit 32 | """) 33 | choice = input("Enter choice: ") 34 | 35 | try: 36 | choice = int(choice) 37 | except ValueError: 38 | print("It is not integer") 39 | continue 40 | 41 | if choice == 1: 42 | bike.displayStock() 43 | choice = "A" 44 | elif choice == 2: 45 | customer.rentalTime_b = bike.rentHourly(customer.requestVehicle("bike")) 46 | customer.rentalBasis_b = 1 47 | main_menu = True 48 | print("----------------") 49 | elif choice == 3: 50 | customer.rentalTime_b = bike.rentDaily(customer.requestVehicle("bike")) 51 | customer.rentalBasis_b = 2 52 | main_menu = True 53 | print("----------------") 54 | elif choice == 4: 55 | customer.bill = bike.returnVehicle(customer.returnVehicle("bike"),"bike") 56 | customer.rentalBasis_b, customer.rentalTime_b, customer.bikes = 0,0,0 57 | main_menu = True 58 | elif choice == 5: 59 | main_menu = True 60 | elif choice ==6: 61 | break 62 | else: 63 | print("Invalid input. Please enter a number between 1-6 ") 64 | main_menu = True 65 | 66 | elif choice == "B" or choice == "b": 67 | 68 | print(""" 69 | ****** CAR MENU***** 70 | 1. Display available cars 71 | 2. Request a car on hourly basis $ 10 72 | 3. Request a car on daily basis $ 192 73 | 4. Return a car 74 | 5. Main Menu 75 | 6. Exit 76 | """) 77 | choice = input("Enter choice: ") 78 | 79 | try: 80 | choice = int(choice) 81 | except ValueError: 82 | print("It is not integer") 83 | continue 84 | 85 | if choice == 1: 86 | car.displayStock() 87 | choice = "B" 88 | elif choice == 2: 89 | customer.rentalTime_c = car.rentHourly(customer.requestVehicle("car")) 90 | customer.rentalBasis_c = 1 91 | main_menu = True 92 | print("----------------") 93 | elif choice == 3: 94 | customer.rentalTime_c = car.rentDaily(customer.requestVehicle("car")) 95 | customer.rentalBasis_c = 2 96 | main_menu = True 97 | print("----------------") 98 | elif choice == 4: 99 | customer.bill = car.returnVehicle(customer.returnVehicle("car"),"car") 100 | customer.rentalBasis_c, customer.rentalTime_c, customer.cars = 0,0,0 101 | main_menu = True 102 | elif choice == 5: 103 | main_menu = True 104 | elif choice ==6: 105 | break 106 | else: 107 | print("Invalid input. Please enter a number between 1-6 ") 108 | main_menu = True 109 | 110 | elif choice == "Q" or choice == "q": 111 | break 112 | 113 | else: 114 | print("Invalid Input. Please Enter A-B-Q") 115 | main_menu = True 116 | print("Thank you for using the vehicle rental shop") 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /OOP Codes/project_codes/rent.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | # parent class 4 | class VehicleRent: 5 | 6 | def __init__(self,stock): 7 | self.stock = stock 8 | self.now = 0 9 | 10 | def displayStock(self): 11 | """ 12 | display stock 13 | """ 14 | print("{} vehicle available to rent".format(self.stock)) 15 | return self.stock 16 | 17 | def rentHourly(self, n): 18 | """ 19 | rent hourly 20 | """ 21 | if n <= 0: 22 | print("Number should be positive") 23 | return None 24 | elif n > self.stock: 25 | print("Sorry, {} vehicle available to rent".format(self.stock)) 26 | return None 27 | else: 28 | self.now = datetime.datetime.now() 29 | print("Rented a {} vehicle for hourly at {} hours".format(n,self.now.hour)) 30 | 31 | self.stock -= n 32 | 33 | return self.now 34 | 35 | def rentDaily(self, n): 36 | """ 37 | rent daily 38 | """ 39 | if n <= 0: 40 | print("Number should be positive") 41 | return None 42 | elif n > self.stock: 43 | print("Sorry, {} vehicle available to rent".format(self.stock)) 44 | return None 45 | else: 46 | self.now = datetime.datetime.now() 47 | print("Rented a {} vehicle for daily at {} hours".format(n,self.now.hour)) 48 | 49 | self.stock -= n 50 | 51 | return self.now 52 | 53 | def returnVehicle(self, request, brand): 54 | """ 55 | return a bill 56 | """ 57 | car_h_price = 10 58 | car_d_price = car_h_price*8/10*24 59 | bike_h_price = 5 60 | bike_d_price = bike_h_price*7/10*24 61 | 62 | rentalTime, rentalBasis, numOfVehicle = request 63 | bill = 0 64 | 65 | if brand == "car": 66 | if rentalTime and rentalBasis and numOfVehicle: 67 | self.stock += numOfVehicle 68 | now = datetime.datetime.now() 69 | rentalPeriod = now - rentalTime 70 | 71 | if rentalBasis == 1: # hourly 72 | bill = rentalPeriod.seconds/3600*car_h_price*numOfVehicle 73 | 74 | elif rentalBasis == 2: # daily 75 | bill = rentalPeriod.seconds/(3600*24)*car_d_price*numOfVehicle 76 | 77 | if (2 <= numOfVehicle): 78 | print("You have extra 20% discount") 79 | bill = bill*0.8 80 | 81 | print("Thank you for returning your car") 82 | print("Price: $ {}".format(bill)) 83 | return bill 84 | elif brand == "bike": 85 | if rentalTime and rentalBasis and numOfVehicle: 86 | self.stock += numOfVehicle 87 | now = datetime.datetime.now() 88 | rentalPeriod = now - rentalTime 89 | 90 | if rentalBasis == 1: # hourly 91 | bill = rentalPeriod.seconds/3600*bike_h_price*numOfVehicle 92 | 93 | elif rentalBasis == 2: # daily 94 | bill = rentalPeriod.seconds/(3600*24)*bike_d_price*numOfVehicle 95 | 96 | if (4 <= numOfVehicle): 97 | print("You have extra 20% discount") 98 | bill = bill*0.8 99 | 100 | print("Thank you for returning your bike") 101 | print("Price: $ {}".format(bill)) 102 | return bill 103 | else: 104 | print("You do not rent a vehicle") 105 | return None 106 | 107 | # child class 1 108 | class CarRent(VehicleRent): 109 | 110 | global discount_rate 111 | discount_rate = 15 112 | 113 | def __init__(self, stock): 114 | super().__init__(stock) 115 | 116 | def discount(self, b): 117 | """ 118 | discount 119 | """ 120 | bill = b - (b*discount_rate)/100 121 | return bill 122 | 123 | # child class 2 124 | class BikeRent(VehicleRent): 125 | 126 | def __init__(self, stock): 127 | super().__init__(stock) 128 | 129 | # customer 130 | class Customer: 131 | 132 | def __init__(self): 133 | self.bikes = 0 134 | self.rentalBasis_b = 0 135 | self.rentalTime_b = 0 136 | 137 | self.cars = 0 138 | self.rentalBasis_c = 0 139 | self.rentalTime_c = 0 140 | 141 | def requestVehicle(self, brand): 142 | """ 143 | take a request bike or car from customer 144 | """ 145 | if brand == "bike": 146 | bikes = input("How many bikes would you like to rent?") 147 | 148 | try: 149 | bikes = int(bikes) 150 | except ValueError: 151 | print("Number should be Number") 152 | return -1 153 | 154 | if bikes < 1: 155 | print("Number of Bikes should be greater than zero") 156 | return -1 157 | else: 158 | self.bikes = bikes 159 | return self.bikes 160 | 161 | elif brand == "car": 162 | cars = input("How many cars would you like to rent?") 163 | 164 | try: 165 | cars = int(cars) 166 | except ValueError: 167 | print("Number should be Number") 168 | return -1 169 | 170 | if cars < 1: 171 | print("Number of cars should be greater than zero") 172 | return -1 173 | else: 174 | self.cars = cars 175 | return self.cars 176 | 177 | else: 178 | print("Request vehicle error") 179 | 180 | def returnVehicle(self, brand): 181 | """ 182 | return bikes or cars 183 | """ 184 | if brand == "bike": 185 | if self.rentalTime_b and self.rentalBasis_b and self.bikes: 186 | return self.rentalTime_b, self.rentalBasis_b, self.bikes 187 | else: 188 | return 0,0,0 189 | elif brand == "car": 190 | if self.rentalTime_c and self.rentalBasis_c and self.cars: 191 | return self.rentalTime_c, self.rentalBasis_c, self.cars 192 | else: 193 | return 0,0,0 194 | else: 195 | print("Return vehicle Error") 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | --------------------------------------------------------------------------------