From d2d439a687dbdf3b0ee40a803ea18301e5541b8e Mon Sep 17 00:00:00 2001 From: Kola-Aderoju Date: Sat, 14 Jun 2025 12:33:23 +0100 Subject: [PATCH] Solution to the First semester examination project by Kola-Aderoju --- .vscode/settings.json | 11 ++ Sales_report.py | 90 +++++++++++++++ Student_data.py | 99 ++++++++++++++++ _Task 5-- Total orders_.csv | 2 + ..._specific_customer_SELECT_202506141126.csv | 3 + ...m_all_orders_SELECT_SUM_p_202506141130.csv | 2 + ...details_including_custome_202506141131.csv | 9 ++ ...ducts_and_any_related_ord_202506141131.csv | 6 + first-semester-projects | 1 + postgres | 0 second_examination.sql | 106 ++++++++++++++++++ 11 files changed, 329 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 Sales_report.py create mode 100644 Student_data.py create mode 100644 _Task 5-- Total orders_.csv create mode 100644 _Task_4_Retrieving_all_orders_made_by_a_specific_customer_SELECT_202506141126.csv create mode 100644 _Task_6_Calculate_the_total_revenue_from_all_orders_SELECT_SUM_p_202506141130.csv create mode 100644 _Task_7_Using_INNER_JOIN_to_show_order_details_including_custome_202506141131.csv create mode 100644 _Task_8_Using_LEFT_JOIN_to_show_all_products_and_any_related_ord_202506141131.csv create mode 160000 first-semester-projects create mode 100644 postgres create mode 100644 second_examination.sql diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..65d939f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "python.testing.unittestArgs": [ + "-v", + "-s", + ".", + "-p", + "*test.py" + ], + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true +} \ No newline at end of file diff --git a/Sales_report.py b/Sales_report.py new file mode 100644 index 0000000..5ea5b46 --- /dev/null +++ b/Sales_report.py @@ -0,0 +1,90 @@ +# Python Tasks Implementation + +# 1. Create lists/dictionaries to simulate new order data +new_orders = [ + { + 'customer': {'id': 6, 'name': 'Bola Adekunle', 'email': 'bola@example.com'}, + 'order_date': '2025-06-05', + 'items': [ + {'product_id': 1, 'name': 'Phone', 'price': '2.99', 'quantity': 3}, + {'product_id': 5, 'name': 'Chicken', 'price': '5.99', 'quantity': 2} + ] + }, + { + 'customer': {'id': 7, 'name': 'Yemi Alade', 'email': 'yemi@example.com'}, + 'order_date': '2025-06-06', + 'items': [ + {'product_id': 3, 'name': 'Eggs', 'price': '3.49', 'quantity': 5} + ] + } +] + +# 2. Use if, and, or to identify customers who placed large orders +print("Customers with large orders:") +for order in new_orders: + total = sum(float(item['price']) * item['quantity'] for item in order['items']) + if total > 15 or (order['customer']['name'].startswith('B') and total > 10): + print(f"{order['customer']['name']} placed a large order worth ${total:.2f}") + +# 3. Convert product prices from string to float and apply discounts +print("\nApplying 10% discount to all products:") +for order in new_orders: + for item in order['items']: + original_price = float(item['price']) + discounted_price = original_price * 0.9 # 10% discount + item['price'] = str(discounted_price) + print(f"{item['name']}: ${original_price:.2f} → ${discounted_price:.2f}") + +# 4. Create a dictionary report summarizing sales +def generate_sales_report(orders): + report = { + 'total_products_sold': 0, + 'products': {}, + 'customers': {}, + 'most_popular_product': {'name': '', 'quantity': 0}, + 'total_revenue': 0.0 + } + + for order in orders: + customer_name = order['customer']['name'] + if customer_name not in report['customers']: + report['customers'][customer_name] = {'total_spent': 0.0, 'items': 0} + + for item in order['items']: + # Update product stats + product_name = item['name'] + quantity = item['quantity'] + price = float(item['price']) + + if product_name not in report['products']: + report['products'][product_name] = 0 + report['products'][product_name] += quantity + + # Update customer stats + report['customers'][customer_name]['total_spent'] += price * quantity + report['customers'][customer_name]['items'] += quantity + + # Update totals + report['total_products_sold'] += quantity + report['total_revenue'] += price * quantity + + # Check for most popular product + if report['products'][product_name] > report['most_popular_product']['quantity']: + report['most_popular_product'] = {'name': product_name, 'quantity': report['products'][product_name]} + + return report + +# Generate and display the report +sales_report = generate_sales_report(new_orders) +print("\nSales Report:") +print(f"Total products sold: {sales_report['total_products_sold']}") +print(f"Most popular product: {sales_report['most_popular_product']['name']} ({sales_report['most_popular_product']['quantity']} units)") +print(f"Total revenue: ${sales_report['total_revenue']:.2f}") + +print("\nRevenue by customer:") +for customer, data in sales_report['customers'].items(): + print(f"{customer}: ${data['total_spent']:.2f} (for {data['items']} items)") + +print("\nProducts sold:") +for product, quantity in sales_report['products'].items(): + print(f"{product}: {quantity} units") \ No newline at end of file diff --git a/Student_data.py b/Student_data.py new file mode 100644 index 0000000..88345ee --- /dev/null +++ b/Student_data.py @@ -0,0 +1,99 @@ +# Initialize a global list to store all students +students = [] + +def add_student(): + """ + Adds students to the database until user enters 'stop' + """ + print("\nAdd Student Mode (enter 'stop' as name to finish)") + + while True: + print("\nPlease enter student information:") + name = input("Name: ") + + if name.lower() == 'stop': + print("\nStudent registration completed!") + break + + age = input("Age: ") + + # Validate grade input + while True: + grade = input("Grade (0-100): ") + try: + grade = float(grade) + if 0 <= grade <= 100: + break + else: + print("Grade must be between 0 and 100!") + except ValueError: + print("Please enter a valid number for grade!") + + # Add student to global list + students.append({ + "name": name, + "age": age, + "grade": grade + }) + + print(f"\nStudent {name} added successfully!") + print(f"Total students: {len(students)}") + +def view_students(): + """ + Displays all students in the database + """ + if not students: + print("\nNo students in the database!") + return + + print("\nSTUDENT DATABASE") + print("=" * 40) + for i, student in enumerate(students, 1): + print(f"\nStudent #{i}") + print(f"Name: {student['name']}") + print(f"Age: {student['age']}") + print(f"Grade: {student['grade']}") + print("\n" + "=" * 40) + print(f"Total students: {len(students)}") + +def get_average_grade(): + """ + Calculates and returns the average grade + """ + if not students: + return 0.0 + + total = sum(student['grade'] for student in students) + return total / len(students) + +def main_menu(): + """ + Main program interface + """ + while True: + print("\n" + "=" * 30) + print("STUDENT MANAGEMENT SYSTEM") + print("=" * 30) + print("\n1. Add Students") + print("2. View All Students") + print("3. View Average Grade") + print("4. Exit") + + choice = input("\nEnter your choice (1-4): ") + + if choice == '1': + add_student() + elif choice == '2': + view_students() + elif choice == '3': + avg = get_average_grade() + print(f"\nAverage Grade: {avg:.2f}") + elif choice == '4': + print("\nThank you for using the system. Goodbye!") + break + else: + print("\nInvalid choice! Please enter 1-4") + +if __name__ == "__main__": + main_menu() \ No newline at end of file diff --git a/_Task 5-- Total orders_.csv b/_Task 5-- Total orders_.csv new file mode 100644 index 0000000..14ff9ae --- /dev/null +++ b/_Task 5-- Total orders_.csv @@ -0,0 +1,2 @@ +"total_orders" +5 diff --git a/_Task_4_Retrieving_all_orders_made_by_a_specific_customer_SELECT_202506141126.csv b/_Task_4_Retrieving_all_orders_made_by_a_specific_customer_SELECT_202506141126.csv new file mode 100644 index 0000000..6c31c99 --- /dev/null +++ b/_Task_4_Retrieving_all_orders_made_by_a_specific_customer_SELECT_202506141126.csv @@ -0,0 +1,3 @@ +"order_id","order_date" +1,"2025-06-01" +3,"2025-06-03" diff --git a/_Task_6_Calculate_the_total_revenue_from_all_orders_SELECT_SUM_p_202506141130.csv b/_Task_6_Calculate_the_total_revenue_from_all_orders_SELECT_SUM_p_202506141130.csv new file mode 100644 index 0000000..31f1664 --- /dev/null +++ b/_Task_6_Calculate_the_total_revenue_from_all_orders_SELECT_SUM_p_202506141130.csv @@ -0,0 +1,2 @@ +"total_revenue" +44.830000000000005 diff --git a/_Task_7_Using_INNER_JOIN_to_show_order_details_including_custome_202506141131.csv b/_Task_7_Using_INNER_JOIN_to_show_order_details_including_custome_202506141131.csv new file mode 100644 index 0000000..cdffc1a --- /dev/null +++ b/_Task_7_Using_INNER_JOIN_to_show_order_details_including_custome_202506141131.csv @@ -0,0 +1,9 @@ +"customer_name","order_date","product_name","quantity","price","item_total" +Kola Aderoju,"2025-06-01",Phone,2,2.99,5.98 +Kola Aderoju,"2025-06-01",Cars,1,1.99,1.99 +Ishola Rokibat,"2025-06-02",Eggs,1,3.49,3.49 +Ishola Rokibat,"2025-06-02",Corn,5,0.99,4.95 +Kola Aderoju,"2025-06-03",Chicken,2,5.99,11.98 +Oladele Joseph,"2025-06-03",Phone,3,2.99,8.97 +Omotosho Mubarak,"2025-06-04",Cars,2,1.99,3.98 +Omotosho Mubarak,"2025-06-04",Eggs,1,3.49,3.49 diff --git a/_Task_8_Using_LEFT_JOIN_to_show_all_products_and_any_related_ord_202506141131.csv b/_Task_8_Using_LEFT_JOIN_to_show_all_products_and_any_related_ord_202506141131.csv new file mode 100644 index 0000000..2f3e38e --- /dev/null +++ b/_Task_8_Using_LEFT_JOIN_to_show_all_products_and_any_related_ord_202506141131.csv @@ -0,0 +1,6 @@ +"product_name","price","total_quantity_sold","total_revenue" +Cars,1.99,3,5.97 +Chicken,5.99,2,11.98 +Corn,0.99,5,4.95 +Eggs,3.49,2,6.98 +Phone,2.99,5,14.950000000000001 diff --git a/first-semester-projects b/first-semester-projects new file mode 160000 index 0000000..38d49c1 --- /dev/null +++ b/first-semester-projects @@ -0,0 +1 @@ +Subproject commit 38d49c1346319b9564bb3845ed0b04811399dec5 diff --git a/postgres b/postgres new file mode 100644 index 0000000..e69de29 diff --git a/second_examination.sql b/second_examination.sql new file mode 100644 index 0000000..106a221 --- /dev/null +++ b/second_examination.sql @@ -0,0 +1,106 @@ +-- Task 1: Creating the tables +CREATE TABLE customers ( + customer_id SERIAL PRIMARY KEY, + name TEXT NOT NULL, + email TEXT, + join_date DATE NOT NULL +); + +CREATE TABLE products ( + product_id INT PRIMARY KEY, + name TEXT NOT NULL, + price REAL NOT NULL, + category TEXT +); + +CREATE TABLE orders ( + order_id INT PRIMARY KEY, + customer_id INT NOT NULL, + order_date DATE NOT NULL, + FOREIGN KEY (customer_id) REFERENCES customers(customer_id) +); + +CREATE TABLE order_items ( + item_id INT PRIMARY KEY, + order_id INT, + product_id INT, + quantity INT, + FOREIGN KEY (order_id) REFERENCES orders(order_id), + FOREIGN KEY (product_id) REFERENCES products(product_id) +); + +-- Task 2: Altering one of the tables to add a new column +ALTER TABLE products ADD COLUMN stock INTEGER DEFAULT 0; + +-- Task 3: Inserting at 5 rows into each table + +-- Insert customers +INSERT INTO customers VALUES +(1, 'Kola Aderoju', 'kolaaderoju@example.com', '2025-01-15'), +(2, 'Ishola Rokibat', 'isholarokibat@example.com', '2025-02-20'), +(3, 'Oladele Joseph', 'josephola@example.com', '2025-03-10'), +(4, 'Omotosho Mubarak', 'omotoshomubarak@example.com', '2025-04-05'), +(5, 'Abiade Zainab', 'abiadezainab@example.com', '2025-05-12'); + +-- Insert products +INSERT INTO products VALUES +(1, 'Phone', 2.99, 'Telecommunications', 50), +(2, 'Cars', 1.99, 'Automobiles', 100), +(3, 'Eggs', 3.49, 'Dairy', 75), +(4, 'Corn', 0.99, 'Cereals', 200), +(5, 'Chicken', 5.99, 'Meat', 30); + +-- Insert orders +INSERT INTO orders VALUES +(1, 1, '2025-06-01'), +(2, 2, '2025-06-02'), +(3, 1, '2025-06-03'), +(4, 3, '2025-06-03'), +(5, 4, '2025-06-04'); + +-- Insert order items +INSERT INTO order_items VALUES +(1, 1, 1, 2), +(2, 1, 2, 1), +(3, 2, 3, 1), +(4, 2, 4, 5), +(5, 3, 5, 2), +(6, 4, 1, 3), +(7, 5, 2, 2), +(8, 5, 3, 1); + +-- Task 4: Retrieving all orders made by a specific customer +SELECT o.order_id, o.order_date +FROM orders o +JOIN customers c ON o.customer_id = c.customer_id +WHERE c.name = 'Kola Aderoju'; + +-- Task 5: Get the total number of orders +SELECT COUNT(*) AS total_orders FROM orders; + +-- Task 6: Calculate the total revenue from all orders +SELECT SUM(p.price * oi.quantity) AS total_revenue +FROM order_items oi +JOIN products p ON oi.product_id = p.product_id; + +-- Task 7: Using INNER JOIN to show order details including customer and product names +SELECT c.name AS customer_name, + o.order_date, + p.name AS product_name, + oi.quantity, + p.price, + (p.price * oi.quantity) AS item_total +FROM order_items oi +JOIN orders o ON oi.order_id = o.order_id +JOIN customers c ON o.customer_id = c.customer_id +JOIN products p ON oi.product_id = p.product_id; + +-- Task 8: Using LEFT JOIN to show all products and any related order (even if not sold) +SELECT p.name AS product_name, + p.price, + COALESCE(SUM(oi.quantity), 0) AS total_quantity_sold, + COALESCE(SUM(p.price * oi.quantity), 0) AS total_revenue +FROM products p +LEFT JOIN order_items oi ON p.product_id = oi.product_id +GROUP BY p.name, p.price; +