Design a Package class

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-03-31

Problem Statement

Design a Package class that represents a physical parcel in a delivery or logistics system (like Amazon, FedEx, or DHL).


Each package moves through various stages of shipment — from creation, pickup, transit, and delivery — and contains information such as sender, recipient, dimensions, weight, tracking status, etc.


The goal is to model the Package class and its interactions cleanly using object-oriented design.


🎯 Goal


Design a class (or set of classes) that can:


•Represent the core data of a package.
•Track the package’s delivery lifecycle.
•Handle status updates, delivery events, and cost calculation.
•Be extensible to support different shipping types or delivery modes.


✅ Functional Requirements


Package Creation

•A package should have:
•Unique tracking ID
•Sender details
•Recipient details
•Dimensions (length, width, height)
•Weight
•Source and destination addresses

Status Tracking

•Package goes through different statuses:
1 2CREATED → PICKED_UP → IN_TRANSIT → OUT_FOR_DELIVERY → DELIVERED → RETURNED
•System should allow updating and retrieving the current status.
•Keep a history of all status changes (timestamped events).

Shipping Type & Cost

•Package can be shipped through:

Standard, Express, or Overnight delivery.

•Each shipping type has a base rate + per kg cost.
•System should calculate total shipping cost dynamically.

Delivery Events

•Record timestamps for:
•Pickup
•Transit
•Delivery
•Should allow estimation of delivery time based on distance and type.

Error Handling

•Prevent invalid transitions (e.g., can’t go from CREATED → DELIVERED directly).
•Handle lost or damaged package scenarios.


⚙️ Non-Functional Requirements


•Thread-safe updates for status and event logs.
•Extensible to handle international shipping, customs, or insurance details.
•Immutable tracking ID once assigned.


🧱 Key Classes to Design


ClassResponsibility


Package


Represents the parcel; holds core info, current status, and history.


Address


Represents a location (street, city, state, pincode, country).


Person


Represents sender/receiver details (name, contact).


ShipmentType (enum)


Represents types of shipping (Standard, Express, Overnight).


PackageStatus (enum)


Represents delivery state (CREATED, PICKED_UP, etc.).


ShipmentEvent


Logs each change in status with timestamp and location.


CostCalculator


Calculates total cost based on weight and shipment type.


🧩 Example Class Relationships

1 2Package 3 ├── trackingId 4 ├── sender (Person) 5 ├── recipient (Person) 6 ├── sourceAddress (Address) 7 ├── destinationAddress (Address) 8 ├── weight 9 ├── dimensions (length, width, height) 10 ├── shipmentType (STANDARD / EXPRESS / OVERNIGHT) 11 ├── status (PackageStatus) 12 ├── List<ShipmentEvent> 13 ├── cost 14 15ShipmentEvent 16 ├── status (PackageStatus) 17 ├── timestamp 18 ├── location 19 ├── remarks


🔄 Example Flow

1.Package Creation
1 2new Package(sender, recipient, addresses, weight, EXPRESS) 3→ Status = CREATED
1.Pickup
1 2updateStatus(PICKED_UP) 3→ Event logged with timestamp and location
1.Transit
1 2updateStatus(IN_TRANSIT)
1.Delivery
1 2updateStatus(DELIVERED) 3→ Final cost calculated


🧪 Example Input / Output


Input:

1 2Package: 2 kg, EXPRESS delivery 3From: Mumbai 4To: Delhi


Output:

1 2Tracking ID: PKG1029 3Status: PICKED_UP 4Estimated Delivery: 1 Day 5Total Cost: ₹180


🧠 Possible Extensions


•Add insurance details or declared value.
•Add QR/barcode tracking.
•Add integration with delivery partner APIs.
•Add ETA prediction using distance and route data.
•Support return shipments or failed delivery attempts.


✅ Final Summary:


Design a Package class that:


•Stores sender/recipient and physical details,
•Tracks the package lifecycle (status transitions + event logs),
•Calculates delivery cost,
•Supports extensibility for multiple delivery types.


Focus on clean class structure, valid state transitions, and reusable components.


Solution

Comments