Amazon Interview Question
SDE-2sCountry: United States
Interview Type: In-Person
We can consider few more conditions-
Restaurant has a Menu (Menu class)
Menu has items (Item class)
User can order (Order class)
Order can have many selectedItems(SelectItem class)
User has to pay the BIll (Bill class)
There also be a Payment class(That must be thread safe)
Total - 8 classes
Restaurant
User
Menu
Item
Order
SelectItem
Bill
Payment
Lets see classes one by one -
Restuarant
-----------
- name : String
- location : string
- conatct : string
-----------
+ isOpen() : boolean
+ getName() : String
+ getLoaction() : String
+ getContact() : String
+ setContact()
+ displayMenu()
+ updateMenu)_
+ generateBill()
User
-----------
- uid : String
- contact : String
-----------
+ setContact()
+ getContact()
+ getUid()
+ searchRestauranr()
+ selectRestuarant()
+ newOrder()
+ cancelOrder()
+ trackOrder()
+ payBill()
Menu
---------
- menu : List<Item>
---------
+ displayMenu()
+ updateMenu()
+ addItem()
+ deleteItem()
Item
----------
- itemId : String
- price : float
- name : String
----------
+ setItemId(String)
+ setName(String)
+ setPrice(float)
+ getItemId()
+ getName()
+ getPrice()
Order
-----------
- orderId : String
-----------
+ getStatus() // To track the order
+ updateStatus() // Restaurant admin can update it
+ getOrderId()
+ selectItem()
+ removeItem()
+ updateItem()
SelectItem
-----------
- itemId
- quantity : int
-----------
+ getItem()
+ setItem()
+ getQuantity()
+ setQuantity()
Bill
------------
- billNum
- orderId
------------
+ getBillNum()
+ getTotalAmount()
+ displayBill()
Payment
------------
- pid : String
- pType : String
- billNum : String
-------------
+ successfulyPaid() : boolean
You can divide each use case into micro-services. lets Say:
/search?query //give search result in json format
/restaurant_name //gives detail for particular restaurant in json format
| //reqeust followed by restaurant_name/changemenu
--> PUT for /restaurant_name/changemenu
--> DELETE for /resaurant_name/deletemenu
order/user
|
-->POST for order/user/addItem
-->POST for order/user/addItems
-->DELETE for order/user/deleteItem
-->DELETE for order/user/deleteItems
-->POST or DELETE for order/user/cancelorder
-->GET for order/user/tracklocation
/pay -->POST for /pay redirect to payment gateway
Here i assumed broadly five tables User(u_name, address), Restaurant(r_name, menu),
Order(o_id, u_name, items, total, status), Location(o_id, location_detail)
UI will use these REST endpoint to perform a specific task. For example /search?query to search a restaurant. At server side this /search endpoint calls the database table Restaurant to search for particular restaurant and return all restaurant matching query criteria in json format.
Similarly for other use cases also this REST based micro-service architecture will be use.
My design would be :-
-> Restaurant will be the abstract class which will contain the Map<restaurantName,Restaurant> ( where restaurantName is the name of the restaurant in string format and Restaurant is the instance of the restaurant added.)
public abstract class Restaurant {
protected String restaurantName;
protected String restaurantLocation;
protected Map<Integer, String> restaurantMenu;
public abstract boolean changeMenu();
public abstract FoodItems addItemsFromMenu();
public abstract void trackTheOrder();
/**
* Order cannot be cancelled after the the order has departed
*
* @return
*/
public abstract boolean cancelOrder();
boolean registerRestaurant(String nameOfRestaurant, Restaurant restaurant) {
restaurantMap.put(nameOfRestaurant, restaurant);
return true;
}
}
class Chutneys extends Restaurant {
Chutneys()
{
registerRestaurant("Chutneys",this);
}
...
}
class OliveGarden extends Restaurant{
...
}
To design this app the list of classes we would need are these
1.User
2.Restaurant
3.Item
4.Menu
5.Order
6.Payment
7.Bill
8.Address
User class would have user information basic details
class User {
String UserId;
String Name;
Address Address;
String Mobile;
String Email;
}
Restaurant class would have its basic info and details of Menu available there
class Restaurant {
String ResturentId;
String Name;
Address Address;
String Mobile;
Menu Menu;
}
Menu class contains list of items available
class Menu {
String MenuId;
List<Item> VegItems;
List<Item> NonVegItems;
List<Item> Beverages;
}
Item class contains info about a particulat food
class Item {
String ItemId;
String Name;
decima Price;
}
Order class would have list of all selected food items as well as total price and delivery address
class Order {
String OrderId;
String OrderName;
List<Item> SelectedItems;
decimal TotalPrice;
Address deliveryAddress;
}
Payment class would have details about your order and payment type details
class Payment {
String PaymentId;
String OrderId;
PaymentType PaymentType;
String UserId;
}
class Bill {
String Id;
String UserId;
decimal TotalMoney;
PaymentType PaymentMethod;
}
To design this app the list of classes we would need are these
1.User
2.Restaurant
3.Item
4.Menu
5.Order
6.Payment
7.Bill
8.Address
User class would have user information basic details
class User {
String UserId;
String Name;
Address Address;
String Mobile;
String Email;
}
Restaurant class would have its basic info and details of Menu available there
class Restaurant {
String ResturentId;
String Name;
Address Address;
String Mobile;
Menu Menu;
}
Menu class contains list of items available
class Menu {
String MenuId;
List<Item> VegItems;
List<Item> NonVegItems;
List<Item> Beverages;
}
Item class contains info about a particulat food
class Item {
String ItemId;
String Name;
decima Price;
}
Order class would have list of all selected food items as well as total price and delivery address
class Order {
String OrderId;
String OrderName;
List<Item> SelectedItems;
decimal TotalPrice;
Address deliveryAddress;
}
Payment class would have details about your order and payment type details
class Payment {
String PaymentId;
String OrderId;
PaymentType PaymentType;
String UserId;
}
class Bill {
String Id;
String UserId;
decimal TotalMoney;
PaymentType PaymentMethod;
}
To design this app the list of classes we would need are these
1.User
2.Restaurant
3.Item
4.Menu
5.Order
6.Payment
7.Bill
8.Address
User class would have user information basic details
class User {
String UserId;
String Name;
Address Address;
String Mobile;
String Email;
}
Restaurant class would have its basic info and details of Menu available there
class Restaurant {
String ResturentId;
String Name;
Address Address;
String Mobile;
Menu Menu;
}
Menu class contains list of items available
class Menu {
String MenuId;
List<Item> VegItems;
List<Item> NonVegItems;
List<Item> Beverages;
}
Item class contains info about a particulat food
class Item {
String ItemId;
String Name;
decima Price;
}
Order class would have list of all selected food items as well as total price and delivery address
class Order {
String OrderId;
String OrderName;
List<Item> SelectedItems;
decimal TotalPrice;
Address deliveryAddress;
}
Payment class would have details about your order and payment type details
class Payment {
String PaymentId;
String OrderId;
PaymentType PaymentType;
String UserId;
}
class Bill {
String Id;
String UserId;
decimal TotalMoney;
PaymentType PaymentMethod;
}
You can divide each use case into micro-services. lets Say:
/search?query //give search result in json format
/restaurant_name //gives detail for particular restaurant in json format
| //reqeust followed by restaurant_name/changemenu
--> PUT for /restaurant_name/changemenu
--> DELETE for /resaurant_name/deletemenu
order/user
|
-->POST for order/user/addItem
-->POST for order/user/addItems
-->DELETE for order/user/deleteItem
-->DELETE for order/user/deleteItems
-->POST or DELETE for order/user/cancelorder
-->GET for order/user/tracklocation
/pay -->POST for /pay redirect to payment gateway
Here i assumed broadly five tables User(u_name, address), Restaurant(r_name, menu),
Order(o_id, u_name, items, total, status), Location(o_id, location_detail)
UI will use these REST endpoint to perform a specific task. For example /search?query to search a restaurant. At server side this /search endpoint calls the database table Restaurant to search for particular restaurant and return all restaurant matching query criteria in json format.
Similarly for other use cases also this REST based micro-service architecture will be use.
You can divide each use case into micro-services. lets Say:
/search?query //give search result in json format
/restaurant_name //gives detail for particular restaurant in json format
| //reqeust followed by restaurant_name/changemenu
--> PUT for /restaurant_name/changemenu
--> DELETE for /resaurant_name/deletemenu
order/user
|
-->POST for order/user/addItem
-->POST for order/user/addItems
-->DELETE for order/user/deleteItem
-->DELETE for order/user/deleteItems
-->POST or DELETE for order/user/cancelorder
-->GET for order/user/tracklocation
/pay -->POST for /pay redirect to payment gateway
Here i assumed broadly five tables User(u_name, address), Restaurant(r_name, menu),
Order(o_id, u_name, items, total, status), Location(o_id, location_detail)
UI will use these REST endpoint to perform a specific task. For example /search?query to search a restaurant. At server side this /search endpoint calls the database table Restaurant to search for particular restaurant and return all restaurant matching query criteria in json format.
Similarly for other use cases also this REST based micro-service architecture will be use.
I will use different design patterns for the design of this application.
- GUL MD ERSHAD December 25, 2016Design Patterns involved:
1) Builder Design Pattern (For adding food item and ordering)
2) Interpreter Design Pattern (User can Search Different restaurant)
3) Iterator Pattern (User Sees Menu)
4) Observer Pattern(Track an order in Real Time)
5) Command Pattern(Order or Cancellation of Food)
Please refer to below Code Repository for more details:
https://github.com/gmershad/FoodDeliveryApp