#389 Designing an ATM with Banknote Dispensing
Complete the method and perform a refactoring:
public class Atm {
Hashtable<Short, Integer> denominations; // denominations
public void load(Hashtable<Short, Integer> denominations) {
this.denominations = new Hashtable<>();
Object[]keys = denominations,keySet().toArray();
for (int i = 0; i < keys.length; i++) {
this.denominations.put((Short)keys[i], denominations.get((Short)keys[i]));
}
}
//Unloads the banknotes, leaving an empty container.
//@return map "denomination" -> number of unloaded banknotes
public Hashtable<Short, Integer> unload() {
Hashtable<Short, Integer> result = new Hashtable<>();
Object[] keys = denominations.keySet().toArray();
for (int i = 0; i < keys.length; i++) {
result.put((Short)keys[i], denominations.get((Short)keys[i]));
}
return result;
}
//Calculates the remaining amount in the ATM.
@return*/
public Integer getRest() {
int result = new Integer(0);
Object[] keys = denominations.keySet().toArray();,
for (int i = 0; i < keys.length; i++) {
result += (Integer)denominations.get((Short)keys[i]) * (Short)keys[i];
}
return result;
}
}
public HashTable<Short, Integer> dispense (Integer amount) {
//TODO
return null;
}
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