Python Ex8
Python Ex8
:
Classes and Objects in Python URK23CS7103
12/03/2024
Aim:
To Design a class to represent a rectangle with length and breadth as instance
attributes. Create two rectangle objects, r1 and r2. Initialize the attributes
using the constructor and do the following operations.
Program:
class Rectangle:
def init (self, length, breadth):
self.length = length
self.breadth = breadth
def add (self, other):
return Rectangle(self.length + other.length, self.breadth + other.breadth)
def str (self):
return f"Length is {self.length} and Breadth is {self.breadth}"
def eq (self, other):
return self.length * self.breadth == other.length * other.breadth
def lt (self, other):
return self.length * self.breadth < other.length * other.breadth
def gt (self, other):
return self.length * self.breadth > other.length * other.breadth
def ge (self, other):
return self.length * self.breadth >= other.length * other.breadth
def le (self, other):
return self.length * self.breadth <= other.length * other.breadth
r1 = Rectangle(10, 5)
r2 = Rectangle(20, 6)
r3 = r1 + r2
print(r3)
print("r1 == r2:", r1 == r2)
print("r1 < r2:", r1 < r2)
print("r1 > r2:", r1 > r2)
print("r1 >= r2:", r1 >= r2)
print("r1 <= r2:", r1 <= r2)
Output(Screenshots):
Result:
Hence the program is verified and executed successfully.
b.Write a menu driven application to maintain the employee payroll details using
Python. Your application must contain the following functionalities. Use
constructors, getter and setter functions.
a. For each employee your application must have the details such
as name, empid, department, designation, experience, basicPay,
DA(10% BP), HRA(5%BP),EPF(5%BP), Tax(10% of BP)
Net salary= BP+DA+HRA-EPF-Tax
b. Get the employee details from user(admin)
c. In the menu give the user options to add, edit, delete or display
the employee details.
Aim:
To Write a menu driven application to maintain the employee payroll details using
Python. Your application must contain the following functionalities. Use
constructors, getter and setter functions.
a. For each employee your application must have the details such
as name, empid, department, designation, experience, basicPay,
DA(10% BP), HRA(5%BP),EPF(5%BP), Tax(10% of BP)
Net salary= BP+DA+HRA-EPF-Tax
b. Get the employee details from user(admin)
c. In the menu give the user options to add, edit, delete or display
the employee details.
Algorithm:
1. Define the Employee class with attributes and methods to manage employee details.
2. Create functions to add, edit, delete, and display employee details.
3.Initialize an empty list to store Employee objects.
4. Implement a main loop to display a menu and handle user input.
5. Inside the loop, based on the user's choice, call the corresponding function to perform the desired
operation on employee details.
Program:
class Employee:
def init (self, name, empid, department, designation, experience, basic_pay):
self.name = name
self.empid = empid
self.department = department
self.designation = designation
self.experience = experience
self.basic_pay = basic_pay
self.da = 0.1 * basic_pay
self.hra = 0.05 * basic_pay
self.epf = 0.05 * basic_pay
self.tax = 0.1 * basic_pay
def get_net_salary(self):
return self.basic_pay + self.da + self.hra - self.epf - self.tax
def str (self):
return f"Name: {self.name}\nEmployee ID: {self.empid}\nDepartment: {self.department}\n" \
f"Designation: {self.designation}\nExperience: {self.experience} years\n" \
f"Basic Pay: {self.basic_pay}\nDA: {self.da}\nHRA: {self.hra}\nEPF: {self.epf}\n" \
f"Tax: {self.tax}\nNet Salary: {self.get_net_salary()}"
def add_employee():
name = input("Enter employee name: ")
empid = input("Enter employee ID: ")
department = input("Enter department: ")
designation = input("Enter designation: ")
experience = int(input("Enter experience in years: "))
basic_pay = float(input("Enter basic pay: "))
emp = Employee(name, empid, department, designation, experience, basic_pay)
employees.append(emp)
print("Employee added successfully!")
def edit_employee():
empid = input("Enter employee ID to edit details: ")
for emp in employees:
if emp.empid == empid:
emp.basic_pay = float(input("Enter new basic pay: "))
emp.da = 0.1 * emp.basic_pay
emp.hra = 0.05 * emp.basic_pay
emp.epf = 0.05 * emp.basic_pay
emp.tax = 0.1 * emp.basic_pay
print("Employee details updated successfully!")
print(emp)
return
print("Employee not found!")
def delete_employee():
empid = input("Enter employee ID to delete: ")
for emp in employees:
if emp.empid == empid:
employees.remove(emp)
print("Employee deleted successfully!")
return
print("Employee not found!")
def display_employee_details():
for emp in employees:
print(emp)
print(" ----------")
employees = []
while True:
print("\nMenu:")
print("1. Add Employee")
print("2. Edit Employee Details")
print("3. Delete Employee")
print("4. Display All Employee Details")
print("5. Exit")
choice = input("Enter your choice (1-5): ")
if choice == "1":
add_employee()
elif choice == "2":
edit_employee()
elif choice == "3":
delete_employee()
elif choice == "4":
display_employee_details()
elif choice == "5":
print("Exiting the program. . ")
break
else:
print("Invalid choice! Please enter a number between 1 and 5.")
Output(Screenshots):
Result:
Hence the program is verified and executed successfully.
.