You are on page 1of 7

# PROGRAMMING ASSIGNMENT

from datetime import datetime


import json
class QuestionOne:

def capture_employee_detail(self):
# employee_details dictionary stores employee details while the key
is the grade number
# and the keys is the list of employee_details
employee_details = {0:[],1:[],2:[],3:[],4:[],5:[]}

# we give the employees grading from 0-5, hourly rate increase with
higher grading
# CALCULATE THE TOTAL EMPLOYEES CAPTURED AND LET THEM BE LESS THAN
OR EQUAL 20
total_employees = len(employee_details[0])+
len(employee_details[1]) + len(employee_details[2])+
len(employee_details[4])+len(employee_details[5])
while total_employees < 20:
print("============FIRST QUESTION PART a================")
emp_id = input("ENTER EMPLOYEE ID :")
emp_address = input("ENTER EMPLOYEE ADDRESS :")
emp_grade = int(input("ENTER EMPLOYEE GRADE (VALUE BETWN 0 AND
5) :"))
emp_hours = float(input("ENTER EMPLOYEE HOURS WORKED :"))
emp_leave_days = int(input("ENTER EMPLOYEE LEAVE DAYS :"))
print("===========FIRST QUESTION PART b================")

if emp_grade ==0:
if len(employee_details[0]) ==0:
employee_details[0] = list()
hourly_rate = 6.50
print("=============HOUR RATE===============")
# salary per day is = hourly_rate*emp_hours_worked
# SALARY PER DAY
salary_per_day = hourly_rate*emp_hours
# salary per week , remove leave days
salary_per_week = salary_per_day*7 -
hourly_rate*emp_hours*emp_leave_days
# salary per month, 4 weeks, remove salary for leave hours
salary_per_month = salary_per_week*4 -
salary_per_week*(emp_leave_days//7)

employee_details[0].append({"emp_id":emp_id,"emp_address":emp_address,"emp_
grade":emp_grade,"emp_hours":emp_hours,"emp_leave_days":emp_leave_days,"sal
ary":salary_per_month})
print(employee_details)
qn = input("Do you want to capture more employee details
(yes/no) :").lower()
if qn =="yes":
continue
else:
break
elif emp_grade ==1:
hourly_rate = 7.50
# salary per day is = hourly_rate*emp_hours_worked
# SALARY PER DAY
salary_per_day = hourly_rate*emp_hours
# salary per week , remove leave days
salary_per_week = salary_per_day*7 -
hourly_rate*emp_hours*emp_leave_days
# salary per month, 4 weeks, remove salary for leave hours
salary_per_month = salary_per_week*4 -
salary_per_week*(emp_leave_days//7)

employee_details[0].append({"emp_id":emp_id,"emp_address":emp_address,"emp_
grade":emp_grade,"emp_hours":emp_hours,"emp_leave_days":emp_leave_days,"sal
ary":salary_per_month})
print(employee_details)
qn = input("Do you want to capture more employee details
(yes/no) :").lower()
if qn =="yes":
continue
else:
break
elif emp_grade ==2:
hourly_rate = 8.50
# salary per day is = hourly_rate*emp_hours_worked
# SALARY PER DAY
salary_per_day = hourly_rate*emp_hours
# salary per week , remove leave days
salary_per_week = salary_per_day*7 -
hourly_rate*emp_hours*emp_leave_days
# salary per month, 4 weeks, remove salary for leave hours
salary_per_month = salary_per_week*4 -
salary_per_week*(emp_leave_days//7)

employee_details[0].append({"emp_id":emp_id,"emp_address":emp_address,"emp_
grade":emp_grade,"emp_hours":emp_hours,"emp_leave_days":emp_leave_days,"sal
ary":salary_per_month})
print(employee_details)
qn = input("Do you want to capture more employee details
(yes/no) :").lower()
if qn =="yes":
continue
else:
break
elif emp_grade ==3:
hourly_rate = 9.50
# salary per day is = hourly_rate*emp_hours_worked
# SALARY PER DAY
salary_per_day = hourly_rate*emp_hours
# salary per week , remove leave days
salary_per_week = salary_per_day*7 -
hourly_rate*emp_hours*emp_leave_days
# salary per month, 4 weeks, remove salary for leave hours
salary_per_month = salary_per_week*4 -
salary_per_week*(emp_leave_days//7)

employee_details[0].append({"emp_id":emp_id,"emp_address":emp_address,"emp_
grade":emp_grade,"emp_hours":emp_hours,"emp_leave_days":emp_leave_days,"sal
ary":salary_per_month})
print(employee_details)
qn = input("Do you want to capture more employee details
(yes/no) :").lower()
if qn =="yes":
continue
else:
break
elif emp_grade ==4:
hourly_rate = 10.50
# salary per day is = hourly_rate*emp_hours_worked
# SALARY PER DAY
salary_per_day = hourly_rate*emp_hours
# salary per week , remove leave days
salary_per_week = salary_per_day*7 -
hourly_rate*emp_hours*emp_leave_days
# salary per month, 4 weeks, remove salary for leave hours
salary_per_month = salary_per_week*4 -
salary_per_week*(emp_leave_days//7)

employee_details[0].append({"emp_id":emp_id,"emp_address":emp_address,"emp_
grade":emp_grade,"emp_hours":emp_hours,"emp_leave_days":emp_leave_days,"sal
ary":salary_per_month})
print(employee_details)
qn = input("Do you want to capture more employee details
(yes/no) :").lower()
if qn =="yes":
continue
else:
break
elif emp_grade ==5:
hourly_rate = 11.50
# salary per day is = hourly_rate*emp_hours_worked
# SALARY PER DAY
salary_per_day = hourly_rate*emp_hours
# salary per week , remove leave days
salary_per_week = salary_per_day*7 -
hourly_rate*emp_hours*emp_leave_days
# salary per month, 4 weeks, remove salary for leave hours
salary_per_month = salary_per_week*4 -
salary_per_week*(emp_leave_days//7)

employee_details[0].append({"emp_id":emp_id,"emp_address":emp_address,"emp_
grade":emp_grade,"emp_hours":emp_hours,"emp_leave_days":emp_leave_days,"sal
ary":salary_per_month})
print(employee_details)
qn = input("Do you want to capture more employee details
(yes/no) :").lower()
if qn =="yes":
continue
else:
break
else:
print("======MAXIMUM EMPLOYEE DETAILS CAPTURED, PLEASE TRY
NEXT DAY=========")

# PRINT EMPLOYEE DETAILS


print(employee_details)

class QuestionTwo:
def __init__(self):
self.sales_details = []
self.purchases_details = []
self.product_details = {}
self.users = []
self.capture_purchase_details()
self.capture_sales_details()
self.rating_facility()
self.profit_losses()
self.storing_sales()

def capture_purchase_details(self):
while True:
product_barcode = input("Enter product barcode : ")
product_name = input("Enter Product Name : ")
product_quantity = int(input("Enter Product Quantity :"))
product_unitprice = float(input("Enter Product Unit Price :"))
product_timestamp = str(datetime.now())
# put purchases in a purchases details list

self.purchases_details.append(dict({"product_barcode":product_barcode,"prod
uct_name":product_name,"product_quantity":product_quantity,"product_unitpri
ce":product_unitprice,"product_timestamp":product_timestamp}))
#update product details
print("=========UPDATING PRODUCT DETAILS==========")
code = str(product_barcode)
if code in self.product_details.keys():
self.product_details[code] += product_quantity
else:
self.product_details[code] =product_quantity
print("=========UPDATED PRODUCT DETAILS===========")
qn = input("--DO YOU WANT TO CAPTURE ANOTHER PRODUCT
PURCHASE--- (yes/no) :")
if qn =="yes":
continue
else:
break
def capture_sales_details(self):
print("===========CAPTURING PRODUCT SALES=============")
while True:
product_barcode = input("Enter product barcode : ")
product_quantity = int(input("Enter Product Quantity To Be
Sold: "))
product_unitprice = float(input("Enter Product Unit Selling
Price : "))
product_timestamp = str(datetime.now())
# put purchases in a purchases details list

self.sales_details.append({"product_barcode":product_barcode,"product_quant
ity":product_quantity,"product_unitprice":product_unitprice,"product_timest
amp":product_timestamp})
#update product details
print("=========UPDATING PRODUCT DETAILS==========")
if self.product_details[product_barcode]:
self.product_details[product_barcode] -= product_quantity
else:
print("===========SORRY THIS PRODUCT DOES NOT
EXIST============")
qn = input("--DO YOU WANT TO CAPTURE ANOTHER PRODUCT SALE---
(yes/no) :")
if qn =="yes":
continue
else:
break
def rating_facility(self):
print("==========RATING SERVICES================")
username = input("Please Enter Username :")
password = input("Please Enter Password :")
user_found = False
for user in self.users:

if user["username"] ==username and user["password"] ==


password:
user_found = True
print("====AUTHENTICATED=====")
rating = int(input("How Much Do You Rate The Services
Offered out of Five (0-5) :"))
# UPDATE THE USER DETAILS WITH THEIR RATING
user["rating"] = rating

if user_found:
break
else:
continue
else:

self.users.append({"username":username,"password":password})
print("====AUTHENTICATED=====")
rating = int(input("How Much Do You Rate The Services
Offered out of Five (0-5) :"))
# UPDATE THE USER DETAILS WITH THEIR RATING
user["rating"] = rating

def profit_losses(self):
print("=========CAPTURING SERVICES RATING===============")
# profit = total_sales - #total_purchases
total_sales = 0
for sale in self.sales_details:
total_sales +=
(sale["product_quantity"]*sale["product_unitprice"])
total_purchases = 0
for purchase in self.purchases_details:
total_purchases +=
(purchase["product_quantity"]*purchase["product_unitprice"])
profit = total_sales - total_purchases
if profit >0:
print(f"=====A PROFIT OF {profit} WAS MADE=======")
else:
print(f"=====A LOSS OF {profit} WAS MADE==========")

def storing_sales(self):
print("===========STORING SALES IN A TXT FILE===============")
# here we store and retrieve sales details in a txt file
with open("sales.txt","w") as f:
for sale in self.sales_details:
f.write(json.dumps(sale))

#read sales details from the file using todays date


print("==========PRINTING RETRIVED SALES FILES BASED ON TODAYS
DATE============")
current_date = str(datetime.now()).split()[0].split("-")[2]
with open("sales.txt","r") as f:
# loop through the returned sales dictionary
retrieved_sales = []
for sale in f:
if json.loads(sale)["product_timestamp"].split()
[0].split("-")[2] == current_date:
retrieved_sales.append(json.loads(sale))
print(retrieved_sales)

QuestionOne().capture_employee_detail()

You might also like