Design a Logistics System

Design a Logistics System (Object Oriented Design). Tell about the different classes and their relationships with each-other. It is not a System Design question, so scope of this question is only to define different classes (with it’s attributes and methods)

Solution: Let’s assume we want to design a Logistics System with following basic functionality:

• The system can take an order to deliver it to a given destination.
• The order will be a list of items and there is a cost of each order to process.
• User has to register himself / herself to use this system.
• User can track his / her order.
• Orders will be shipped by bike or truck, but only a single order will be shipped by a single vehicle.

These type of questions are asked in interviews to Judge the Object Oriented Design skill of a candidate. So, first of all we should think about the classes.
The main classes will be:

1. User
2. Item
3. Vehicle
4. Location
5. PaymentDetails
6. Order
7. LogisticsSystem

The User class is for users/clients/customers, who will be charged to get their items delivered.
public class User {

    private int userId;
    private String name;
    private Location adress;
    private String mobNo;
    private String emailId;

    public int getUserId()
    {
        return userId;
    }

    public void setUserId(int userId)
    {
        this.userId = userId;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public Location getAdress()
    {
        return adress;
    }

    public void setAdress(Location adress)
    {
        this.adress = adress;
    }

    public String getMobNo()
    {
        return mobNo;
    }

    public void setMobNo(String mobNo)
    {
        this.mobNo = mobNo;
    }

    public String getEmailId()
    {
        return emailId;
    }

    public void setEmailId(String emailId)
    {
        this.emailId = emailId;
    }
}
Item class represents an item which will be shipped. An order will contain a list of items.

public class Item {

    private String name;
    private int price;
    private int volume;
    private int weight;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getPrice()
    {
        return price;
    }

    public void setPrice(int price)
    {
        this.price = price;
    }

    public int getVolume()
    {
        return volume;
    }

    public void setVolume(int volume)
    {
        this.volume = volume;
    }

    public int getWeight()
    {
        return weight;
    }

    public void setWeight(int weight)
    {
        this.weight = weight;
    }
}
Location class simply represents the address.
public class Location {

    private Double longitude;
    private Double latitude;

    public Double getLongitude()
    {
        return longitude;
    }

    public void setLongitude(Double longitude)
    {
        this.longitude = longitude;
    }

    public Double getLatitude()
    {
        return latitude;
    }

    public void setLatitude(Double latitude)
    {
        this.latitude = latitude;
    }
}
The Vehicle class represents the vehicle which will be used to ship/deliver an order.
It will be of two types: 1. Bike and 2. Truck
public class Vehicle {

    private int id;
    private String vehicleNo;
    private int capacity;
    private Location currentPosition;
    private VehicleStatus currentStatus;

    public Vehicle(int id, String vehicleNo, int capacity)
    {
        this.id = id;
        this.vehicleNo = vehicleNo;
        this.capacity = capacity;
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getVehicleNo()
    {
        return vehicleNo;
    }

    public void setVehicleNo(String vehicleNo)
    {
        this.vehicleNo = vehicleNo;
    }

    public int getCapacity()
    {
        return capacity;
    }

    public void setCapacity(int capacity)
    {
        this.capacity = capacity;
    }

    public Location getCurrentPosition()
    {
        return currentPosition;
    }

    public void setCurrentPosition(Location currentPosition)
    {
        this.currentPosition = currentPosition;
    }

    public VehicleStatus getCurrentStatus()
    {
        return currentStatus;
    }

    public void setCurrentStatus(VehicleStatus currentStatus)
    {
        this.currentStatus = currentStatus;
    }
}
The bike has only 10 unit of capacity.
public class Bike extends Vehicle {

    private final static int capacityofBike = 10;

    public Bike(int id, String vehicleNo)
    {

        super(id, vehicleNo, capacityofBike);
    }
}
The truck has only 100 unit of capacity (10 times more than bike).
public class Truck extends Vehicle {

    private final static int capacityofTruck = 100;

    public Truck(int id, String vehicleNo)
    {
        super(id, vehicleNo, capacityofTruck);
    }
}
Enum for current status of a vehicle:
public enum VehicleStatus {
    FREE,
    BUSY,
    NOT_WORKING;
}
Enum for OrderPriority:
public enum OrderPriority {
    LOW,
    MEDIUM,
    HIGH;
}
Enum for OrderStatus:
public enum OrderStatus {
    DELIVERED,
    PROCESSING,
    CANCELLED;
}
Enum for PaymentMode:
public enum PaymentMode {

    NET_BANKING,
    CREDIT_CARD,
    DEBIT_CARD;
}
Enum for PaymentStatus:
public enum PaymentStatus {
    PAID,
    UNPAID;
}
The PaymentDetails class contains the information of payment related things so that in any case the order is cancelled, it can be refunded easily.
public class PaymentDetails {
    private PaymentMode paymentMode;
    private String transactionId;
    private int amount;
    private PaymentStatus paymentStatus;
    private String cardNumber;

    public PaymentMode getPaymentMode()
    {
        return paymentMode;
    }

    public void setPaymentMode(PaymentMode paymentMode)
    {
        this.paymentMode = paymentMode;
    }

    public String getTransactionId()
    {
        return transactionId;
    }

    public void setTransactionId(String transactionId)
    {
        this.transactionId = transactionId;
    }

    public int getAmount()
    {
        return amount;
    }

    public void setAmount(int amount)
    {
        this.amount = amount;
    }

    public PaymentStatus getPaymentStatus()
    {
        return paymentStatus;
    }

    public void setPaymentStatus(PaymentStatus paymentStatus)
    {
        this.paymentStatus = paymentStatus;
    }

    public String getCardNumber()
    {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber)
    {
        this.cardNumber = cardNumber;
    }
}
The Order class contains all the information of an order. All fields are self-explanatory.

import java.util.Date;
import java.util.List;

public class Order {

    private int orderId;
    private OrderPriority priority_of_order;
    private User sender;
    private Location destination;
    private PaymentDetails paymentDetails;
    private List<Item> items;
    private Long totalWeight;
    private OrderStatus currentStatus;
    private Date timeOfOrderPlaced;
    private Date timeOfOrderDelivery;
    private Vehicle vehicleOfThisOrder;

    public int getOrderId()
    {
        return orderId;
    }

    public void setOrderId(int orderId)
    {
        this.orderId = orderId;
    }

    public OrderPriority getPriority_of_order()
    {
        return priority_of_order;
    }

    public void setPriority_of_order(OrderPriority priority_of_order)
    {
        this.priority_of_order = priority_of_order;
    }

    public User getSender()
    {
        return sender;
    }

    public void setSender(User sender)
    {
        this.sender = sender;
    }

    public Location getDestination()
    {
        return destination;
    }

    public void setDestination(Location destination)
    {
        this.destination = destination;
    }

    public PaymentDetails getPaymentDetails()
    {
        return paymentDetails;
    }

    public void setPaymentDetails(PaymentDetails paymentDetails)
    {
        this.paymentDetails = paymentDetails;
    }

    public List<Item> getItems()
    {
        return items;
    }

    public void setItems(List<Item> items)
    {
        this.items = items;
    }

    public Long getTotalWeight()
    {
        return totalWeight;
    }

    public void setTotalWeight(Long totalWeight)
    {
        this.totalWeight = totalWeight;
    }

    public OrderStatus getCurrentStatus()
    {
        return currentStatus;
    }

    public void setCurrentStatus(OrderStatus currentStatus)
    {
        this.currentStatus = currentStatus;
    }

    public Date getTimeOfOrderPlaced()
    {
        return timeOfOrderPlaced;
    }

    public void setTimeOfOrderPlaced(Date timeOfOrderPlaced)
    {
        this.timeOfOrderPlaced = timeOfOrderPlaced;
    }

    public Date getTimeOfOrderDelivery()
    {
        return timeOfOrderDelivery;
    }

    public void setTimeOfOrderDelivery(Date timeOfOrderDelivery)
    {
        this.timeOfOrderDelivery = timeOfOrderDelivery;
    }

    public Vehicle getVehicleOfThisOrder()
    {
        return vehicleOfThisOrder;
    }

    public void setVehicleOfThisOrder(Vehicle vehicleOfThisOrder)
    {
        this.vehicleOfThisOrder = vehicleOfThisOrder;
    }
}
The main class (LogisticsSystem) which stores all the information of users, orders and vehicles. It has the methods like takeAnOrder(), processOrder() etc.

import java.util.ArrayList;
import java.util.List;

public class LogisticsSystem {

    private List<Order> orders;
    private List<Vehicle> vehicles;
    private List<User> users;

    public LogisticsSystem()
    {
        this.orders = new ArrayList<Order>();
        this.vehicles = new ArrayList<Vehicle>();
        this.users = new ArrayList<User>();
    }

    public void takeAnOrder(Order order)
    {
        System.out.println("Adding an order to the system");
        orders.add(order);
    }

    public void processOrder(Order order)
    {
        System.out.println("Processing an order of the system");
    }

    public Location trackOrder(int orderId)
    {
        System.out.println("Tracking an order of the system");
        Location location = null;
        // location = findLocationOfGivenOrder();
        return location;
    }

    public void cacelOrder(Order order)
    {
        System.out.println("Going to cancell an order of the system");
    }

    public void registerNewUser(User user)
    {
        System.out.println("Registering a new user to the system");
        users.add(user);
    }

    public List<Order> getOrders()
    {
        return orders;
    }

    public void setOrders(List<Order> orders)
    {
        this.orders = orders;
    }

    public List<Vehicle> getVehicles()
    {
        return vehicles;
    }

    public void setVehicles(List<Vehicle> vehicles)
    {
        this.vehicles = vehicles;
    }

    public List<User> getUsers()
    {
        return users;
    }

    public void setUsers(List<User> users)
    {
        this.users = users;
    }
}
This is the test class to test the application.

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Apptest {

    public static void main(String[] args)
    {

        User user = new User();
        user.setUserId(1);
        user.setName("Shashi");
        user.setEmailId("shashi@gmail.com");

        Item item1 = new Item();
        item1.setName("item1");
        item1.setPrice(20);

        Item item2 = new Item();
        item2.setName("item2");
        item2.setPrice(40);

        List<Item> items = new ArrayList<Item>();
        items.add(item1);
        items.add(item2);

        PaymentDetails paymentDetails = new PaymentDetails();
        paymentDetails.setAmount(100);
        paymentDetails.setPaymentMode(PaymentMode.CREDIT_CARD);
        paymentDetails.setCardNumber("12345678");

        Location destination = new Location();
        destination.setLatitude(73.23);
        destination.setLongitude(132.34);

        Order order = new Order();
        order.setOrderId(1);
        order.setItems(items);
        order.setCurrentStatus(OrderStatus.PROCESSING);
        order.setDestination(destination);
        order.setPaymentDetails(paymentDetails);
        order.setTimeOfOrderDelivery(new Date());

        LogisticsSystem logisticsSystem = new LogisticsSystem();
        logisticsSystem.registerNewUser(user);
        logisticsSystem.takeAnOrder(order);
        logisticsSystem.processOrder(order);
    }

Comments

Popular posts from this blog

Very Basic Java Programs

[08 Feb 2020] Minimum number of platforms required for a railway station

Interview Questions asked in Goldman sach interview