#540 Implementation of ATM Cash Withdrawal
The Hardware interface for interacting with the ATM hardware is given:
/**
* Interface for interacting with the ATM hardware.
*/
interface Hardware {
/**
* Returns an array containing the number of banknotes for denominations
* 50, 100, 500, 1000, and 5000.
* The method is slow and creates noise.
*
* @return an array where every element corresponds to the number of banknotes
* of a specific denomination.
* For example, [10, 20, 30, 40, 50] means:
* - 10 banknotes of 50 rubles
* - 20 banknotes of 100 rubles
* - 30 banknotes of 500 rubles
* - 40 banknotes of 1000 rubles
* - 50 banknotes of 5000 rubles
*/
fun billsCounts(): IntArray
/**
* Loads the specified banknotes into the cash-dispensing box.
*
* @param billsCounts an array containing the number of banknotes for
* denominations [50, 100, 500, 1000, 5000].
* For example, [0, 1, 0, 2, 0] means:
* - 0 banknotes of 50 rubles
* - 1 banknote of 100 rubles
* - 0 banknotes of 500 rubles
* - 2 banknotes of 1000 rubles
* - 0 banknotes of 5000 rubles
*/
fun giveBills(billsCounts: IntArray)
}
Implement the MyATM class responsible for ATM business logic.
/**
* Class implementing ATM business logic.
* Write the code here.
*/
class MyATM {
/**
* ATM hardware.
*/
var hardware: Hardware? = null
val nominals = [50, 100, 500, 1000, 5000]
fun calcTotal(): Int
fun giveMoney(req: OperationRequest): OperationResponse
data class OperationRequest(val sum: Int)
data class OperationResponse(val status: OperationStatus)
}
The calcTotal() method must return the total amount of money currently stored in the ATM.
The giveMoney(req) method must attempt to dispense the requested amount.
If the amount can be dispensed using the available banknotes, call hardware.giveBills(...) and return a successful status.
If the amount cannot be dispensed, return the corresponding error status.
Keep in mind that hardware.billsCounts() is slow and noisy, so it must not be called unnecessarily.
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor