Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Water_Quality_Monitoring/water_quality_monitoring
Submodule water_quality_monitoring added at d8e01f
54 changes: 51 additions & 3 deletions month1_student_management/student_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,64 @@ def add_student():
TODO: Prompt the user to enter student name, age, and grade.
Append the student as a dictionary to the students list.
"""
pass
name = input("Enter student name: ")
age_input = input("Enter student age: ")
grade_input = input("Enter student grade: ")

try:
age = int(age_input)
grade = float(grade_input)
except ValueError:
print("Invalid input. Age must be an integer and grade must be a number.")
return

student = {"name": name, "age": age, "grade": grade}
students.append(student)
print(f"{name} added successfully!\n")


def view_students():
"""
TODO: Loop through the students list and print each student's info.
"""
pass
if not students:
print("No students in the list.\n")
return

for i, student in enumerate(students, start=1):
print(f"{i}. Name: {student['name']}, Age: {student['age']}, Grade: {student['grade']}")
print() # blank line


def get_average_grade():
"""
TODO: Return the average grade of all students.
"""
pass
if not students:
print("No students to calculate average.\n")
return 0

total = sum(student["grade"] for student in students)
average = total / len(students)
return average

# Menu loop
if __name__ == "__main__":
while True:
print("1. Add student")
print("2. View students")
print("3. Get average grade")
print("4. Exit")
choice = input("Choose an option: ")

if choice == "1":
add_student()
elif choice == "2":
view_students()
elif choice == "3":
avg = get_average_grade()
print(f"Average grade: {avg}\n")
elif choice == "4":
break
else:
print("Invalid option. Try again.\n")
Binary file not shown.
Binary file not shown.
45 changes: 45 additions & 0 deletions month2_minimart_analysis/python/main.py
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
# Entry point for the MiniMart data reporting system
from utils import load_data, apply_discount, convert_currency, flag_large_order
from report_generator import generate_report
import pandas as pd
import json

def main():
# Load base data
customers, products, orders = load_data()

# ✅ Simulate new orders using a list of dicts
new_orders = [
{"order_id": 6, "customer_id": 2, "product_id": 1, "quantity": 3},
{"order_id": 7, "customer_id": 3, "product_id": 2, "quantity": 5},
{"order_id": 8, "customer_id": 1, "product_id": 3, "quantity": 10},
]
new_orders_df = pd.DataFrame(new_orders)

# Merge new orders into existing ones
orders = pd.concat([orders, new_orders_df], ignore_index=True)

# ✅ Apply discounts and currency conversion on products
products["discounted_price"] = products["price"].apply(apply_discount)
products["price_in_kes"] = products["discounted_price"].apply(convert_currency)

# ✅ Flag large orders (> $100 before conversion)
orders_with_prices = orders.merge(products, on="product_id")
orders_with_prices["order_total"] = orders_with_prices["discounted_price"] * orders_with_prices["quantity"]
orders_with_prices["large_order"] = orders_with_prices["order_total"].apply(flag_large_order)

# Generate report
report = generate_report(customers, products, orders)

# Print results
print("==== MiniMart Report ====")
for key, value in report.items():
print(f"{key}: {value}")

# Save report to JSON
with open("report.json", "w") as f:
json.dump(report, f, indent=4)

print("\n✅ Report saved to report.json")

if __name__ == "__main__":
main()
11 changes: 11 additions & 0 deletions month2_minimart_analysis/python/report.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"total_products_sold": 30,
"most_popular_product": "Bread Loaf",
"revenue_per_customer": {
"1": 23.0,
"2": 6.5,
"3": 17.5,
"4": 5.6,
"5": 3.6
}
}
25 changes: 25 additions & 0 deletions month2_minimart_analysis/python/report_generator.py
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
# Code to generate dictionary summary reports
def generate_report(customers, products, orders):
"""Generate a dictionary report with key metrics."""

# Merge orders with products
orders_with_prices = orders.merge(products, on="product_id")
orders_with_prices["revenue"] = orders_with_prices["price"] * orders_with_prices["quantity"]

# ✅ Total products sold
total_products_sold = int(orders["quantity"].sum())

# ✅ Most popular product
popular_product_id = orders.groupby("product_id")["quantity"].sum().idxmax()
popular_product = products.loc[products["product_id"] == popular_product_id, "product_name"].values[0]

# ✅ Revenue per customer
revenue_per_customer = (
orders_with_prices.groupby("customer_id")["revenue"].sum().to_dict()
)

report = {
"total_products_sold": total_products_sold,
"most_popular_product": popular_product,
"revenue_per_customer": revenue_per_customer
}
return report
20 changes: 20 additions & 0 deletions month2_minimart_analysis/python/utils.py
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
# Utility functions for data conversion and filtering
import pandas as pd

def load_data():
"""Load customers, products, and orders CSVs from sql folder."""
customers = pd.read_csv("../sql/customers_data.csv")
products = pd.read_csv("../sql/products_data.csv")
orders = pd.read_csv("../sql/orders_data.csv")
return customers, products, orders

def apply_discount(price, threshold=50, discount=0.1):
"""Apply discount if price exceeds threshold."""
return price * (1 - discount) if price > threshold else price

def convert_currency(price, rate=150):
"""Convert product price to another currency (e.g., USD→KES)."""
return price * rate

def flag_large_order(total, threshold=100):
"""Flag orders exceeding a given total threshold."""
return total > threshold
37 changes: 37 additions & 0 deletions month2_minimart_analysis/sql/create_tables.sql
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
-- create databse first
CREATE DATABASE minimart;
USE minimart;

-- SQL script to create necessary tables
-- Customers table
CREATE TABLE customers (
customer_id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);

-- Products table
CREATE TABLE products (
product_id SERIAL PRIMARY KEY,
product_name VARCHAR(100),
price DECIMAL(10,2)
);

-- Orders table
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
customer_id INT,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

-- Order details table (to link orders and products)
CREATE TABLE order_details (
detail_id SERIAL 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)
);


6 changes: 6 additions & 0 deletions month2_minimart_analysis/sql/customers_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
customer_id,name,email,join_date
1,"Alice Johnson",alice.johnson@example.com,2024-01-15
2,"Brian Smith",brian.smith@example.com,2024-02-10
3,"Catherine Lee",catherine.lee@example.com,2024-03-05
4,"David Kim",david.kim@example.com,2024-03-20
5,"Emily Davis",emily.davis@example.com,2024-04-01
22 changes: 22 additions & 0 deletions month2_minimart_analysis/sql/insert_data.sql
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
-- SQL script to insert sample data
INSERT INTO customers (name, email, join_date) VALUES
('Alice Johnson', 'alice.johnson@example.com', '2024-01-15'),
('Brian Smith', 'brian.smith@example.com', '2024-02-10'),
('Catherine Lee', 'catherine.lee@example.com', '2024-03-05'),
('David Kim', 'david.kim@example.com', '2024-03-20'),
('Emily Davis', 'emily.davis@example.com', '2024-04-01');

INSERT INTO products (product_name, category, price) VALUES
('Coca Cola 500ml', 'Drinks', 1.50),
('Pepsi 500ml', 'Drinks', 1.40),
('Bread Loaf', 'Bakery', 2.00),
('Apples 1kg', 'Fruits', 3.50),
('Milk 1L', 'Dairy', 1.80);


INSERT INTO orders (customer_id, product_id, quantity, order_date) VALUES
(1, 1, 2, '2024-04-05'), -- Alice buys 2 Coca Cola
(2, 3, 1, '2024-04-06'), -- Brian buys 1 Bread
(3, 4, 3, '2024-04-06'), -- Catherine buys 3 kg Apples
(4, 2, 4, '2024-04-07'), -- David buys 4 Pepsi
(5, 5, 2, '2024-04-07'); -- Emily buys 2 Milk

6 changes: 6 additions & 0 deletions month2_minimart_analysis/sql/orders_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
order_id,customer_id,product_id,quantity,order_date
4,4,2,4,2024-04-07
5,5,5,2,2024-04-07
2,2,3,1,2024-04-06
3,3,4,3,2024-04-06
1,1,1,2,2024-04-05
6 changes: 6 additions & 0 deletions month2_minimart_analysis/sql/products_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
product_id,product_name,category,price
1,"Coca Cola 500ml",Drinks,1.50
2,"Pepsi 500ml",Drinks,1.40
3,"Bread Loaf",Bakery,2.00
4,"Apples 1kg",Fruits,3.50
5,"Milk 1L",Dairy,1.80
57 changes: 57 additions & 0 deletions month2_minimart_analysis/sql/queries.sql
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
-- SQL queries for retrieving insights
SELECT *
FROM customers;

SELECT *
FROM products;

SELECT product_id, product_name, price
FROM products
WHERE category = 'Drinks';

SELECT p.product_name,
SUM(p.price * o.quantity) AS total_revenue
FROM orders o
JOIN products p ON o.product_id = p.product_id
GROUP BY p.product_name;

SELECT AVG(price) AS average_price
FROM products;


SELECT *
FROM orders
ORDER BY order_date DESC;

SELECT COUNT(*) AS total_orders
FROM orders;

SELECT o.order_id,
c.name AS customer_name,
c.email,
p.product_name,
p.category,
o.quantity,
o.order_date,
(p.price * o.quantity) AS total_price
FROM orders o
INNER JOIN customers c ON o.customer_id = c.customer_id
INNER JOIN products p ON o.product_id = p.product_id;

SELECT c.customer_id,
c.name,
c.email,
o.order_id,
o.order_date,
o.quantity
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id;

-- left join for all data
SELECT p.product_id,
p.product_name,
p.category,
p.price,
o.order_id,
o.quantity
FROM products p
LEFT JOIN orders o ON p.product_id = o.product_id;