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)
Asked In: Adobe , Paytm
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:
- User
- Item
- Vehicle
- Location
- payment details
- Order
- LogisticsSystem
The User class is for users/clients/customers, who will be charged to get their items delivered.
Java
public class User { private int userId; private String name; private Location address; 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 getAddress() { return address; } public void setAddress(Location address) { this .address = address; } 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 that will be shipped. An order will contain a list of items.
Java
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.
Java
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 that will be used to ship/deliver an order.
It will be of two types:
- Bike
- Truck
Java
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.
Java
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).
Java
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:
Java
public enum VehicleStatus { FREE, BUSY, NOT_WORKING; } |
Enum for OrderPriority:
Java
public enum OrderPriority { LOW, MEDIUM, HIGH; } |
Enum for OrderStatus:
Java
public enum OrderStatus { DELIVERED, PROCESSING, CANCELLED; } |
Enum for PaymentMode:
Java
public enum PaymentMode { NET_BANKING, CREDIT_CARD, DEBIT_CARD; } |
Enum for PaymentStatus:
Java
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.
Java
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.
Java
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.
Java
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 cancel 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.
Java
package com.shashi.oodesign; 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); } } |
Please Login to comment...