Skip to content

heikaliskandar/exercise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Order Analytics

A simple PHP program that reads an orders CSV file and outputs:

  • Total revenue (sum of quantity × price)
  • Best-selling SKU (the product with the highest total quantity)

Requirements

  • PHP 7.4 or higher

Check with:

php -v

Project Structure

├── analyze.php                          # Main script (run this)
├── src/
│   └── CsvOrderAnalyzer.php             # Core logic
├── allsome_interview_test_orders.csv     # Sample input data
├── output.json                          # Generated after running
└── README.md

How to Run

cd /path/to/your-project-folder
php analyze.php

To use a different CSV file:

php analyze.php path/to/your_orders.csv

CSV Format

Your CSV file needs these columns (order doesn't matter):

order_id,sku,quantity,price
1001,SKU-A123,2,50.00
1002,SKU-B456,1,75.00

Output

The program prints JSON to the console and saves it to output.json:

{
    "total_revenue": 975,
    "best_selling_sku": {
        "sku": "SKU-C789",
        "total_quantity": 7
    }
}

Error Handling

  • File not found → shows error and exits
  • Missing columns → shows which columns are missing
  • Bad rows (empty SKU, invalid numbers) → row is skipped with a warning, rest of the file still processes
  • No valid data → shows error and exits

About

A PHP CLI tool that reads an orders CSV file and outputs total revenue and the best-selling SKU as JSON

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages