Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
public class VendingMachine {
class ItemNode {
int quantity;
int name;
int id;
double price;
public ItemNode ( int quantity , int name , int id , double prices)
{
// Fill it .
}
public String toString(){
return "[ ID: "+id+"name "+name+" prices "+prices+" quantity"+quantity+" ]";
}
}
protected HashMap<String , ItemNode> itemLocater ;
public VendingMachine(){
itemLocater = new HashMap<String , ItemNode>();
}
public void sendInstruction( String buttonId )
{
}
public boolean isAvailable(String buttonId) {
}
public double settleBill(double amount) {
}
protected List<Double> getChange(double amount){
}
protected boolean isChangeAvailable() {
}
private List<Double> greedyChange(double amount ){
}
private List<Double> minimumChange(double amount)
{
}
private List<Double> twentyFiveCentChange(double amount) {
}
public boolean isEmpty(){
}
}
classes
class Item < describes the various items in the vending machine>
class ProductCode < to select which drink/snack you want>
class Coin < to accept coins>
class Refund < to return remainder coins>
class Stock < to keep track of the vending machine stock for each product>
class Bagel, Donut, Energy bar, Chocolates extends class Item
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Scanner;
import java.util.Set;
public class Vending {
LinkedHashMap<String,Double> hm = new LinkedHashMap<String, Double>();
Scanner s = new Scanner(System.in);
private double val;
private String key;
private static int quarters;
private static double dollar;
public void snacks() {
HashSet<String> sm = new HashSet<String>();
hm.put("Lays",2.0);
hm.put("Biscut",1.25);
hm.put("Chocklate", 1.5);
sm.add("Exit");
Set <String> keys = hm.keySet();
for(String s : keys) {
System.out.println(s+":"+hm.get(s));
}
for(String s : sm) {
System.out.println(s);
}
System.out.print("Enter Your Snack Item Name:");
key = s.next();
System.out.println("You have selected"+" "+key);
val = dollar - hm.get(key);
System.out.println(val);
if(key == "Exit") {
item();
}
}
public void drinks() {
HashSet<String> ms = new HashSet<String>();
hm.put("Coke", 3.50);
hm.put("Sprite", 2.5);
hm.put("Pepsi", 2.0);
ms.add("Exit");
Set <String> keys = hm.keySet();
for(String s : keys) {
System.out.println(s+":"+hm.get(s));
}
for(String s : ms) {
System.out.println(s);
}
System.out.print("Enter your Drink Item Name:");
key = s.next();
System.out.println("You have selected"+" "+key);
val = dollar - hm.get(key);
if(key == "Exit") {
item();
}
}
public void item() {
HashSet<String> hs = new HashSet<String>();
hs.add("Snacks<1>");
hs.add("Drinks<2>");
hs.add("Exit<3>");
for(String s : hs) {
System.out.println(s);
}
System.out.print("Enter Your Option:");
int m = s.nextInt();
switch (m) {
case 1:
snacks();
break;
case 2:
drinks();
break;
case 3:
System.out.print("Take your Money $"+dollar);
System.exit(0);
}
if(val == 0){
System.out.println("Thank you for coming have fun");
}
else if(val < 0){
System.out.println("You have Insfficient funds");
}
else if(val > 0){
System.out.println(" Balance:$"+val);
repeat();
}
}
private void repeat() {
System.out.print("Do you Want Anything Else(Y/N):");
char c = s.next(".").charAt(0);
if(c == 'y'){
if(val<0){
System.out.println("Insufficient funds");
}
else { dollar = val;}
item(); repeat(); }
else if(c == 'n'){
System.out.println("Take your Balance:$"+val);}
System.exit(0);
}
public static void main(String[] args) {
Vending ven = new Vending();
System.out.println("Welcome to the Locket Vending Machine");
System.out.print("Insert Quarters :");
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);
Vending.quarters = s.nextInt();
Vending.dollar = 0.25*quarters;
System.out.println("You Have Inserted $"+Vending.dollar);
ven.item();
}
}
// Amount $1 - 99
public int getCoins(int amount){
int total=0,quater=0,dimes=0,nickle=0,cent=0;
if(amount==0){
return 0;
}
quater = amount/25;
dimes = amount%25/10;
nickle = amount%25%10/5;
cent = amount%25%20%5;
System.out.println("quater:"+quater);
System.out.println("dimes:"+dimes);
System.out.println("nickle:"+nickle);
System.out.println("cent:"+cent);
total = quater+dimes+nickle+cent;
System.out.println("total no.of required coins:"+total);
return total;
}
- nitesh.kedia5 June 13, 2015