Amazon Interview Question
SDE-2sTeam: AWS Infrastructure Planning, Analysis and Optimization
Country: United States
Interview Type: In-Person
Good design. I write Java code to implement your idea. This is just a simple example.
public abstract class Item{
protected int itemId;
protected String itemName;
protected double price;
protected int quantity;
public int getItemId(){
return itemId;
}
public String getItemName(){
return itemName;
}
public double getTotal(){
return quantity * price;
}
public abstract void prepare();
}
public class Sandwich extends Item{
private static final int ID = 10082;
private static final String NAME = "sandwich";
private static final double PRICE = 2.99;
public Sandwich(int qty){
itemId = ID;
itemName = NAME;
price = PRICE;
quantity = qty;
}
public void prepare(){
processBread();
processMeat();
packSandwich();
}
private void processBread(){...}
private void processMeat(){...}
private void packSandwich(){...}
}
public class Order{
private Customer customer;
private List<Item> items;
public Order(Customer customer){
this.customer = customer;
}
public Customer getCustomer(){
return customer;
}
public addItem(Item item){
items.add(item);
}
public cancelItem(Item item){
int index = list.indexOf(item);
list.remove(index);
}
public prepare(){
for(int i = 0; i < items.size(); i++){
Item item = items.get(i);
item.prepare();
}
}
public double getTotal(){
double total = 0.0;
for(int i = 0; i < items.size(); i++){
Item item = items.get(i);
total += item.getTotal();
}
return total;
}
}
public class Customer{
private int customerId;
private double balance;
public Customer(int customerId, double initBalance){
this.customerId = customerId;
this.balance = initBalance;
}
public boolean charge(double amount){
if(balance >= amount){
balance -= amount;
return true;
}
else{
return false;
}
}
public void refund(double amount){
balance += amount;
}
}
public class Robot{
private int robotId;
private List<Order> orders;
public Robot(int robotId = 89757){
this.robotId = robotId;
orders = new List<Order>();
}
public boolean takeOrder(Order order){
Customer customer = order.getCustomer();
boolean isSuccess = customer.charge(order.getTotal());
if(isSuccess){
orders.add(order);
}
return isSuccess;
}
public cancelOrder(Order order){
int index = list.indexOf(order);
list.remove(index);
Customer customer = order.getCustomer();
customer.refund(order.getTotal());
}
public prepare(){
for(int i = 0; i < orders.size(); i++){
Order order = order.get(i);
order.prepare();
}
}
}
// sample use of the robot
public class test{
public static void main(String[] args){
Robot robot = new Robot(89757); // make a robot
Customer customerA = new Customer(1, 25.0); // comes in a customer
Order order = new Order(customerA); // make a order
Sandwich sandwich = new Sandwich(2);
order.addItem(sandwich);
robot.takeOrder(order); // take the order
Customer customerB = new Customer(2, 12.0); // comes in a customer
order = new Order(customerB); // make a order
Sandwich sandwich = new Sandwich(5);
order.addItem(sandwich);
robot.takeOrder(order); // take the order
robot.prepare(); // prepare the food
}
}
I would consider using template method pattern: www . newthinktank . com/2012/10/template-method-design-pattern-tutorial/
I think 3 design patterns will be used:
1. Observer: Robot subscribes to user's commands, also when meal is ready.
2. Factory Method: Robot provides separate interface to user, and prepares meal with help of separate class at backend.
3. Command: Takes command to edit menu. Add item, remove item etc
1. Create Abstract class Item(it will have price and abstract prepare() method)
- Anonymous January 20, 20152. Create Sawdwich class,It will be extending Item class
3. Create Order class,It will have List<item> ,Customer Info
4. Robat as client,for accepting order,it will have list of orders,And will prepare items calling item's prepare method.