Last active
October 16, 2023 14:44
-
-
Save rept0id/a08a1ae6a36e55e17f677bc785840dcf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace gr.simplecode.javavendingmachine; | |
// imports | |
import java.io.*; | |
import java.util.*; | |
// START OF : Description | |
/* Java vending machine */ | |
/* | |
░░█ ▄▀█ █░█ ▄▀█ █░█ █▀▀ █▄░█ █▀▄ █ █▄░█ █▀▀ █▀▄▀█ ▄▀█ █▀▀ █░█ █ █▄░█ █▀▀ | |
█▄█ █▀█ ▀▄▀ █▀█ ▀▄▀ ██▄ █░▀█ █▄▀ █ █░▀█ █▄█ █░▀░█ █▀█ █▄▄ █▀█ █ █░▀█ ██▄ | |
*/ | |
/* Author : Rantouan Achmet <[email protected]> */ | |
// END OF : Description | |
// START OF : Exceptions | |
class OutOfStockException extends Exception { | |
public OutOfStockException(String message) { | |
super(message); | |
} | |
} | |
class InsufficientMoneyException extends Exception { | |
public InsufficientMoneyException() { | |
super("Insufficient money"); | |
} | |
} | |
// END OF : Exceptions | |
// START OF : Models | |
class ServeDrinkSummary { | |
Drink servedDrink; | |
int change; | |
public ServeDrinkSummary(Drink servedDrink, int change) { | |
this.servedDrink = servedDrink; | |
this.change = change; | |
} | |
} | |
interface Drink { | |
int getPrice(); | |
String getName(); | |
int getQuantityLeft(); | |
void serveDrink(); | |
} | |
class Coke implements Drink { | |
private int price; | |
private String name; | |
private int quantityLeft; | |
public Coke(int price, String name, int quantityLeft) { | |
this.price = price; | |
this.name = name; | |
this.quantityLeft = quantityLeft; | |
} | |
public int getPrice() { | |
return this.price; | |
} | |
public String getName() { | |
return this.name; | |
} | |
public int getQuantityLeft() { | |
return this.quantityLeft; | |
} | |
public void serveDrink() { | |
if (this.quantityLeft > 0) | |
this.quantityLeft -= 1; | |
} | |
} | |
class Fanta implements Drink { | |
private int price; | |
private String name; | |
private int quantityLeft; | |
public Fanta(int price, String name, int quantityLeft) { | |
this.price = price; | |
this.name = name; | |
this.quantityLeft = quantityLeft; | |
} | |
public int getPrice() { | |
return this.price; | |
} | |
public String getName() { | |
return this.name; | |
} | |
public int getQuantityLeft() { | |
return this.quantityLeft; | |
} | |
public void serveDrink() { | |
if (this.quantityLeft > 0) | |
this.quantityLeft -= 1; | |
} | |
} | |
class Sprite implements Drink { | |
private int price; | |
private String name; | |
private int quantityLeft; | |
public Sprite(int price, String name, int quantityLeft) { | |
this.price = price; | |
this.name = name; | |
this.quantityLeft = quantityLeft; | |
} | |
public int getPrice() { | |
return this.price; | |
} | |
public String getName() { | |
return this.name; | |
} | |
public int getQuantityLeft() { | |
return this.quantityLeft; | |
} | |
public void serveDrink() { | |
if (this.quantityLeft > 0) | |
this.quantityLeft -= 1; | |
} | |
} | |
// END OF : Models | |
// START OF : Controllers | |
class VendingMachine { | |
private Map<Integer, Drink> drinkMap = new HashMap<>(); | |
void registerDrink(int buttonIdx, Drink drink) { | |
drinkMap.put(buttonIdx, drink); | |
} | |
ServeDrinkSummary dispatch(int buttonPressed, int money) | |
throws OutOfStockException, InsufficientMoneyException { | |
Drink selectedDrink = drinkMap.get(buttonPressed); | |
if (selectedDrink == null) { | |
throw new OutOfStockException("Invalid drink selection"); | |
} | |
int price = selectedDrink.getPrice(); | |
int quantityLeft = selectedDrink.getQuantityLeft(); | |
// Business logic : if you look carefully, the assignment, silently, requires "Out of money > Out of stock" | |
// Else it should had been opposite | |
if (money < price) { | |
throw new InsufficientMoneyException(); | |
} | |
if (quantityLeft <= 0) { | |
throw new OutOfStockException("Out of stock for " + selectedDrink.getName()); | |
} | |
selectedDrink.serveDrink(); | |
int change = money - price; | |
return new ServeDrinkSummary(selectedDrink, change); | |
} | |
} | |
public class Solution { | |
public static void main(String[] args) throws IOException { | |
int quantity[] = new int[3], pricePerUnit[] = new int[3], buttonAssigned[] = new int[3]; | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
PrintWriter out = new PrintWriter(System.out); | |
String[] arr = br.readLine().split(" "); | |
for (int i = 0; i < 3; i++) { | |
quantity[i] = Integer.parseInt(arr[i]); | |
} | |
arr = br.readLine().split(" "); | |
for (int i = 0; i < 3; i++) { | |
pricePerUnit[i] = Integer.parseInt(arr[i]); | |
} | |
arr = br.readLine().split(" "); | |
for (int i = 0; i < 3; i++) { | |
buttonAssigned[i] = Integer.parseInt(arr[i]); | |
} | |
VendingMachine vendingMachine = new VendingMachine(); | |
Drink coke = new Coke(pricePerUnit[0], "Coke", quantity[0]); | |
Drink fanta = new Fanta(pricePerUnit[1], "Fanta", quantity[1]); | |
Drink sprite = new Sprite(pricePerUnit[2], "Sprite", quantity[2]); | |
vendingMachine.registerDrink(buttonAssigned[0], coke); | |
vendingMachine.registerDrink(buttonAssigned[1], fanta); | |
vendingMachine.registerDrink(buttonAssigned[2], sprite); | |
out.println("Vending machine set up"); | |
int totalNumberOfRequests = Integer.parseInt(br.readLine().trim()); | |
while (totalNumberOfRequests-- > 0) { | |
arr = br.readLine().split(" "); | |
int buttonPressed = Integer.parseInt(arr[0]); | |
int money = Integer.parseInt(arr[1]); | |
// Business logic : if you look carefully, the assignment, silently, requires "Out of money > Out of stock" | |
// Else it should had been opposite | |
try { | |
ServeDrinkSummary serveDrinkSummary = vendingMachine.dispatch(buttonPressed, money); | |
serveDrinkSummary.servedDrink.serveDrink(); | |
out.println(serveDrinkSummary.servedDrink.getName() + " " + serveDrinkSummary.change); | |
} catch (InsufficientMoneyException e) { | |
out.println("Insufficient money"); | |
} catch (OutOfStockException e) { | |
out.println(e.getMessage()); | |
} | |
} | |
out.flush(); | |
out.close(); | |
} | |
} | |
// END OF : Controllers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment