I'm seeking review comments (design, performance, etc.) based on the problem statement:
Write a CashWithDrawal function from an ATM which based on user specified amount dispenses bank notes. Ensure that the following is taken care of
- Minimum number of bank notes are dispensed
- Availability of various denominations in the ATM is maintained
- Code should be flexible to take care of any bank denominations as long as it is a multiple of 10
- Code should support parallel withdrawals i.e. two or more customers can withdraw money simultaneously
- Take care of exceptional situation
package com.assignment.atm;
import java.util.Scanner;
/**
* The Class Main.
*/
public class Main
{
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Amount to be withdrawn: ");
int amount = input.nextInt();
if(amount%10!=0){
System.out.println("Please enter the amount in multiples of 10");
}
else{
ATM atm = new ATM(amount);
ATM.calcTotalCorpus();
Thread t1 = new Thread(atm);
t1.start();
/*ATM.calcTotalCorpus();
Thread t1 = new Thread(new ATM(1200));
Thread t2 = new Thread(new ATM(2400));
t1.start();
t2.start();
try{
t2.sleep(2000);
}
catch(Exception e){
//Sysout Exception trace
}*/
}
}
}
package com.assignment.atm;
/**
* The Class ATM.
*/
class ATM implements Runnable{
/** The Constant Currency Denominations. */
protected static final int[] currDenom = { 1000, 500, 100, 50 , 10 };
/** The Number of Currencies of each type*/
protected static int[] currNo = {1,4,2,2,10};
/** The count. */
protected int[] count = { 0, 0, 0, 0 ,0};
/** The total corpus. */
protected static int totalCorpus = 0;
/** The amount. */
protected int amount=0;
/**
* Instantiates a new aTM.
*
* @param amount The Amount of type int
*/
public ATM(int amount){
this.amount=amount;
}
/**
* Calc total corpus.
*/
public static void calcTotalCorpus(){
for(int i = 0; i < currDenom.length; i++){
totalCorpus=totalCorpus+currDenom[i]*currNo[i];
}
}
/**
* Withdraw cash.
*/
public synchronized void withdrawCash(){
if(amount<=totalCorpus){
for (int i = 0; i < currDenom.length; i++) {
if (currDenom[i] <= amount) {//If the amount is less than the currDenom[i] then that particular denomination cannot be dispensed
int noteCount = amount / currDenom[i];
if(currNo[i]>0){//To check whether the ATM Vault is left with the currency denomination under iteration
//If the Note Count is greater than the number of notes in ATM vault for that particular denomination then utilize all of them
count[i] = noteCount>=currNo[i]?currNo[i]:noteCount;
currNo[i] = noteCount>=currNo[i]?0:currNo[i]- noteCount;
//Deduct the total corpus left in the ATM Vault with the cash being dispensed in this iteration
totalCorpus=totalCorpus-(count[i]*currDenom[i]);
//Calculate the amount that need to be addressed in the next iterations
amount = amount -(count[i]*currDenom[i]);
}
}
}
displayNotes();
displayLeftNotes();
}
else{
System.out.println("Unable to dispense cash at this moment for this big amount");
}
}
/**
*
*
*/
public void run()
{
withdrawCash();
}
/**
* Display notes.
*/
private void displayNotes(){
for (int i = 0; i < count.length; i++) {
if (count[i] != 0) {
System.out.println(currDenom[i] + " * " + count[i] + " = "+ (currDenom[i] * count[i]));
}
}
}
/**
* Display left notes.
*/
private void displayLeftNotes(){
for(int i = 0; i < currDenom.length; i++){
System.out.println("Notes of "+currDenom[i]+" left are "+currNo[i]);
}
}
}