Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
Ajeet, quite sufficient and detailed interfaces. But some can be merged i think. like:
Class Input, Class KeyPad, Class Key and Class display can be merged into like: UserInterface
Class MoneyValidator can be merged with Class Money itself (this class will have enough info regarding the value of currency and cost should be passed as parameter to validate function)
import java.util.ArrayList;
public class VendingMachine {
private ArrayList<Item> itemlList;
private ArrayList<Item> selectedItems;
private Coin receivedCoin;
public VendingMachine(ArrayList<Item> itemlList) {
this.itemlList = itemlList;
}
public ArrayList<Item> getItemlList() {
return itemlList;
}
public ArrayList<Item> giveCoins(Coin coin){
this.receivedCoin = coin;
return showUserChoiceList();
}
private ArrayList<Item> showUserChoiceList() {
ArrayList<Item> userChoiceList = new ArrayList<Item>();
for(Item item: itemlList){
if(item.getPrice()<=receivedCoin.getValue()){
userChoiceList.add(item);
}
}
return userChoiceList;
}
public Coin getSelectedItem(ArrayList<Item> selectedItems){
this.selectedItems = selectedItems;
return giveSelectedItem();
}
private Coin giveSelectedItem() {
Coin returnCoin=null;
double value = receivedCoin.getValue();
for(Item item: selectedItems){
System.out.println(item.getName()+" | "+item.getPrice());
value=value-item.getPrice();
}
return new Coin(value, receivedCoin.getCurrency());
}
}
Item's availability (more specifically nos. available) is one of the keys information here.
And considering that, storing Items with available count in a structure similar to Map (i.e. Hashtable or Hashmap in Java) makes more sense. This ways, when you implement isAvailable(Item item), you can validate Item's Stock.
here is my design
Interface Iteam{
id
price
discription
inventory
sell(){
//reduce inventory
}
}
Class Menu{
Map<Id,Iteam> iteams;
display(){
return iteams.values();
}
Iteam select(int id){
return items.get(id);
}
}
class vandingMachine{
Menu menu;
start(){
menu.display();
}
getUserInput(id){
Iteam selection = menu.select(id);
Order order = new Order(selection);
order.process();
(order.status==sucess){
dispatch(selectio);
}
}
dispatch(){
}
}
class Order{
Iteam iteam;
PaymentManager payment;
Status
acceptPayment(){
}
process(){
if(payment.isSucess()){
iteam.sell();
}
}
}
PaymentManager{
PaymentStatus {SUCESS,PENDING,FAILED};
charge(double Price){
}
}
below is my code for vendor machine ..there could be additional list of item and some low level plumbering needed:-
import java.util.* ;
public class Vendor {
int choice ;
ArrayList<Item> al ;
ArrayList<Item> al1 ;
HashMap<Integer , ArrayList> hm = new HashMap<Integer , ArrayList>();
HashMap<Integer , Integer> price = new HashMap<Integer , Integer>();
public int get_price(int item_id)
{
return price.get(item_id);
}
public Vendor(ArrayList al , ArrayList al1)
{
this.al = al ;
this.al1 = al1 ;
hm.put(1, al);
hm.put(2, al1);
price.put(1, 20);
price.put(2, 30);
price.put(3, 40);
}
public void dispatch(Menu m)
{
int choice = m.choice();
int rate = price.get(choice);
int coin_recieved = m.coin();
if(coin_recieved<rate)
{
System.out.println("please enter the coin greater than or equal to of amount "+rate+" INR");
System.out.println();
}
else{
int rem = coin_recieved-rate ;
Item str = (Item)hm.get(choice).get(0);
System.out.println("get the item "+str.item_name);
this.al.remove(al.size()-1);
System.out.println("collect your remaining amount of "+rem+" Ruppee");
System.out.println();
}
//m.coin();
}
public static void main(String args[])
{
Item i1 = new Item(1 , "Biscuit" , 20);
Item i2 = new Item(1 , "Biscuit" , 20);
Item i3 = new Item(2 , "Cold Drink" , 30);
Item i4 = new Item(2 , "Cold Drink" , 30);
ArrayList<Item> al = new ArrayList<Item>();
ArrayList<Item> al1 = new ArrayList<Item>();
al.add(i1);
al.add(i2);
al1.add(i3);
al1.add(i4);
Menu m = new Menu();
Vendor v = new Vendor(al , al1 );
while(1==1)
v.dispatch(m);
}
}
class Item
{
int item_id ;
String item_name ;
int item_price ;
public Item(int a , String b , int c)
{
item_id = a ;
item_name = b ;
item_price = c ;
}
}
class Menu
{
public int choice()
{
System.out.println("Biscuit packet : 20 INR Press:1");
System.out.println("Cold Drink : 30 INR Press:2");
System.out.println();
Scanner sc = new Scanner (System.in);
int x = sc.nextInt();
return x ;
}
public int coin()
{
Scanner s = new Scanner(System.in);
int y = s.nextInt();
return y ;
}
}
import java.util.* ;
public class Vendor {
int choice ;
ArrayList<Item> al ;
ArrayList<Item> al1 ;
HashMap<Integer , ArrayList> hm = new HashMap<Integer , ArrayList>();
HashMap<Integer , Integer> price = new HashMap<Integer , Integer>();
public int get_price(int item_id)
{
return price.get(item_id);
}
public Vendor(ArrayList al , ArrayList al1)
{
this.al = al ;
this.al1 = al1 ;
hm.put(1, al);
hm.put(2, al1);
price.put(1, 20);
price.put(2, 30);
price.put(3, 40);
}
public void dispatch(Menu m)
{
int choice = m.choice();
int rate = price.get(choice);
int coin_recieved = m.coin();
if(coin_recieved<rate)
{
System.out.println("please enter the coin greater than or equal to of amount "+rate+" INR");
System.out.println();
}
else{
int rem = coin_recieved-rate ;
Item str = (Item)hm.get(choice).get(0);
System.out.println("get the item "+str.item_name);
this.al.remove(al.size()-1);
System.out.println("collect your remaining amount of "+rem+" Ruppee");
System.out.println();
}
//m.coin();
}
public static void main(String args[])
{
Item i1 = new Item(1 , "Biscuit" , 20);
Item i2 = new Item(1 , "Biscuit" , 20);
Item i3 = new Item(2 , "Cold Drink" , 30);
Item i4 = new Item(2 , "Cold Drink" , 30);
ArrayList<Item> al = new ArrayList<Item>();
ArrayList<Item> al1 = new ArrayList<Item>();
al.add(i1);
al.add(i2);
al1.add(i3);
al1.add(i4);
Menu m = new Menu();
Vendor v = new Vendor(al , al1 );
while(1==1)
v.dispatch(m);
}
}
class Item
{
int item_id ;
String item_name ;
int item_price ;
public Item(int a , String b , int c)
{
item_id = a ;
item_name = b ;
item_price = c ;
}
}
class Menu
{
public int choice()
{
System.out.println("Biscuit packet : 20 INR Press:1");
System.out.println("Cold Drink : 30 INR Press:2");
System.out.println();
Scanner sc = new Scanner (System.in);
int x = sc.nextInt();
return x ;
}
public int coin()
{
Scanner s = new Scanner(System.in);
int y = s.nextInt();
return y ;
}
}
- Ajeet January 17, 2014