From 1a1a67eed559b128de74c762cd476721d0414ade Mon Sep 17 00:00:00 2001 From: Rumeysa Yakar Date: Tue, 18 Oct 2022 22:33:02 +0200 Subject: [PATCH 1/3] created classes for solutions, but needs improvement. --- bank_account.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ rectangle.py | 25 ++++++++++++++ school.py | 38 +++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 bank_account.py create mode 100644 rectangle.py create mode 100644 school.py diff --git a/bank_account.py b/bank_account.py new file mode 100644 index 0000000..3c4711a --- /dev/null +++ b/bank_account.py @@ -0,0 +1,89 @@ +# ## 3. BankAccount + +# 1. Create a Python class called `BankAccount` which represents a bank account, having as attributes: `accountNumber`, `name` , `balance`. +# 2. Create a **constructor** with parameters: `accountNumber`, `name`, `balance`. +# 3. Create a `deposit()` method which manages the deposit actions. (deposit() method will take parameter d and you will increase the balance with the amount d) +# 4. Create a `withdrawal()` method which manages withdrawals actions. (withdrawal() method will take parameter w, you will reduce the amount of balance with w, if w is larger than the balance: then print `Impossible operation! Insufficient balance!"`) +# 5. Create a `bankFees()` method to apply the bank fees with a percentage of 5% of the balance account. (When this method is called, the balance amount should reduce 5%) +# 6. Create a `display()` method to display account details. + +class BankAccount: + def __init__(self, accountNumber, name, balance): + self.accountNumber = accountNumber + self.name = name + self.balance = balance + + def deposit(self): + amount = float(input('Enter amount to be deposited: ')) + self.balance += amount + + def withdrawal(self): + amount = float(input('Enter amount to be withdrawn: ')) + if self.balance < amount : + print('Impossible operation! Insufficient balance!') + else: + self.balance -= amount + + def bankFees(self): + self.balance = (95/100)* self.balance + + def display(self): + print('\nAccount Number :' , self.accountNumber) + print('Account Name :' , self.name) + print('Account Balance :' , self.balance , '€') + +print('Hello! Welcome to Deposit & Withdrawal Machine') +new_account = BankAccount(accountNumber=int(input('Account Number: ')), name=input('Account Name: '), balance=int(input('Account Balance: '))) +new_account.withdrawal() +new_account.deposit() +new_account.display() + + + + + + + + + + + + + + + + + + +# class BankAccount: +# def __init__(self, accountNumber, name, balance): +# self.accountNumber = accountNumber +# self.name = name +# self.balance = balance +# print('Hello! Welcome to the Deposit & Withdrawal Machine') + +# def deposit(self): +# amount = float(input('Enter amount to be deposited: ')) +# self.balance += amount + +# def withdrawal(self): +# amount = float(input('Enter amount to be withdrawn: ')) +# if self.balance < amount : +# print('Impossible operation! Insufficient balance!') +# else: +# self.balance -= amount + +# def bankFees(self): +# self.balance = (95/100)* self.balance + +# def display(self): +# print('Account Number :' , self.accountNumber) +# print('Account Name :' , self.name) +# print('Account Balance :' , self.balance , '€') + +# print('Please enter your account information: ') +# new_account = BankAccount() +# new_account.withdrawal() +# new_account.deposit() +# new_account.display() + diff --git a/rectangle.py b/rectangle.py new file mode 100644 index 0000000..40dbd8d --- /dev/null +++ b/rectangle.py @@ -0,0 +1,25 @@ +## 2. Rectangle + +# 1. Write a `Rectangle` class, allowing you to build a rectangle with `length` and `width` attributes. +# 2. Create a `perimeter()` method to calculate the perimeter of the rectangle and an `area()` method to calculate the area of ​​the rectangle. +# 3. Create a method `display()` that displays the length, width, perimeter and area of an object created using an instantiation on `Rectangle` class. + +class Rectangle: + def __init__(self, length, width): + self.length = length + self.width = width + + def Perimeter(self): + return 2*(self.length + self.width) + + def Area(self): + return self.length * self.width + + def display(self): + print('The length of rectangle is :', self.length) + print("The width of rectangle is: ", self.width) + print("The perimeter of rectangle is: ", self.Perimeter()) + print("The area of rectangle is: ", self.Area()) + +my_rectangle = Rectangle(length=int(input('Length: ')), width=int(input('Width: '))) +my_rectangle.display() diff --git a/school.py b/school.py new file mode 100644 index 0000000..f125626 --- /dev/null +++ b/school.py @@ -0,0 +1,38 @@ +class School: + capacity = 1 + dict_of_class = {} + + +class Student(): + def __init__(self, firstname, lastname, age, gender = 'Unknown'): + self.firstname = firstname + self.lastname = lastname + self.age = age + self.gender = gender + + def add_student(self, capacity): + self.capacity = School.capacity + School.capacity += 1 + if School.capacity < 6: + School.dict_of_class[self.firstname + self.lastname] = [self.firstname, self.lastname, self.age, self.gender] + + else: + print('Error! Class is full! ') + + + def __str__(self): + return (f'\nStudent name: {self.firstname} {self.lastname}\nStudent Age: {self.age}\nStudent Gender: {self.gender}') + +first_class = School() +student1 = Student('Rumi', 'Yakar', 25, 'Female') +student1.add_student(first_class.capacity) +student2 = Student('Said', 'Bostan', 26, 'Male') +student2.add_student(first_class.capacity) +student3 = Student('Sami', 'Yakar', 24, 'Male') +student3.add_student(first_class.capacity) +student4 = Student('Ahmet', 'Yakar', 10, 'Male') +student4.add_student(first_class.capacity) +print(student2) +print(student2.__dict__) +print(first_class.dict_of_class) + From 61394da2de84bcf00f63fe1d88b39b0bd088b346 Mon Sep 17 00:00:00 2001 From: Rumeysa Yakar Date: Tue, 18 Oct 2022 22:41:34 +0200 Subject: [PATCH 2/3] created classes for solutions --- bank_account.py | 78 +++++++++---------------------------------------- 1 file changed, 14 insertions(+), 64 deletions(-) diff --git a/bank_account.py b/bank_account.py index 3c4711a..9c41600 100644 --- a/bank_account.py +++ b/bank_account.py @@ -1,22 +1,19 @@ -# ## 3. BankAccount - -# 1. Create a Python class called `BankAccount` which represents a bank account, having as attributes: `accountNumber`, `name` , `balance`. -# 2. Create a **constructor** with parameters: `accountNumber`, `name`, `balance`. -# 3. Create a `deposit()` method which manages the deposit actions. (deposit() method will take parameter d and you will increase the balance with the amount d) -# 4. Create a `withdrawal()` method which manages withdrawals actions. (withdrawal() method will take parameter w, you will reduce the amount of balance with w, if w is larger than the balance: then print `Impossible operation! Insufficient balance!"`) -# 5. Create a `bankFees()` method to apply the bank fees with a percentage of 5% of the balance account. (When this method is called, the balance amount should reduce 5%) -# 6. Create a `display()` method to display account details. +#Python program to create BankAccount class +#with both a deposit() and withdraw function. +#create the constructor with parameters: account_number, name and balance class BankAccount: - def __init__(self, accountNumber, name, balance): - self.accountNumber = accountNumber + def __init__(self, account_number, name, balance): + self.account_number = account_number self.name = name self.balance = balance - + + #create deposit() method def deposit(self): amount = float(input('Enter amount to be deposited: ')) self.balance += amount + #create withdrawal() method def withdrawal(self): amount = float(input('Enter amount to be withdrawn: ')) if self.balance < amount : @@ -24,66 +21,19 @@ def withdrawal(self): else: self.balance -= amount + #create bankFees() method def bankFees(self): self.balance = (95/100)* self.balance + #create display() method def display(self): - print('\nAccount Number :' , self.accountNumber) + print('\nAccount Number :' , self.account_number) print('Account Name :' , self.name) print('Account Balance :' , self.balance , '€') + print('Hello! Welcome to Deposit & Withdrawal Machine') -new_account = BankAccount(accountNumber=int(input('Account Number: ')), name=input('Account Name: '), balance=int(input('Account Balance: '))) +new_account = BankAccount(account_number=int(input('Account Number: ')), name=input('Account Name: '), balance=int(input('Account Balance: '))) new_account.withdrawal() new_account.deposit() -new_account.display() - - - - - - - - - - - - - - - - - - -# class BankAccount: -# def __init__(self, accountNumber, name, balance): -# self.accountNumber = accountNumber -# self.name = name -# self.balance = balance -# print('Hello! Welcome to the Deposit & Withdrawal Machine') - -# def deposit(self): -# amount = float(input('Enter amount to be deposited: ')) -# self.balance += amount - -# def withdrawal(self): -# amount = float(input('Enter amount to be withdrawn: ')) -# if self.balance < amount : -# print('Impossible operation! Insufficient balance!') -# else: -# self.balance -= amount - -# def bankFees(self): -# self.balance = (95/100)* self.balance - -# def display(self): -# print('Account Number :' , self.accountNumber) -# print('Account Name :' , self.name) -# print('Account Balance :' , self.balance , '€') - -# print('Please enter your account information: ') -# new_account = BankAccount() -# new_account.withdrawal() -# new_account.deposit() -# new_account.display() - +new_account.display() \ No newline at end of file From 1243f5fed417c95b1641759e32c05e7e2059e8ab Mon Sep 17 00:00:00 2001 From: Rumeysa Yakar Date: Tue, 18 Oct 2022 22:42:42 +0200 Subject: [PATCH 3/3] created classes for solutions --- rectangle.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rectangle.py b/rectangle.py index 40dbd8d..5f2f658 100644 --- a/rectangle.py +++ b/rectangle.py @@ -1,20 +1,20 @@ -## 2. Rectangle - -# 1. Write a `Rectangle` class, allowing you to build a rectangle with `length` and `width` attributes. -# 2. Create a `perimeter()` method to calculate the perimeter of the rectangle and an `area()` method to calculate the area of ​​the rectangle. -# 3. Create a method `display()` that displays the length, width, perimeter and area of an object created using an instantiation on `Rectangle` class. - +# Python program to create Rectangle class class Rectangle: + #define constructor with attributes: lenght and width def __init__(self, length, width): self.length = length self.width = width + #create perimeter() method def Perimeter(self): return 2*(self.length + self.width) + + #create Area() method def Area(self): return self.length * self.width + #create display() method def display(self): print('The length of rectangle is :', self.length) print("The width of rectangle is: ", self.width)