Tags » Interfaces

Automated Teller Machine

/**Program to withdraw money from ATM so that the number
* of retrieved banknotes is the minimal possible.
*/

package ro.paula.exercises.atm;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

public class AutomatedTellerMachine {

	private TreeMap<Integer, Integer> availableBanknotes;

	public AutomatedTellerMachine() {
		Comparator comparator = new KeyComparator();
		// keep the banknotes sorted in reverse order of banknote value
		availableBanknotes = new TreeMap<Integer, Integer>(comparator);
	}

	public void addBancnote(Integer banknoteType, Integer numberOfBancnotes) {
		if (!availableBanknotes.containsKey(banknoteType)) {
			availableBanknotes.put(banknoteType, numberOfBancnotes);
			return;

		}
		availableBanknotes.put(banknoteType, availableBanknotes.get(banknoteType)
				+ numberOfBancnotes);
	}

	public void displayAvailableBancnotes() {
		for (Map.Entry<Integer, Integer> entry : availableBanknotes.entrySet()) {
			System.out.println(entry.getKey() + " => " + entry.getValue());
		}
	}

	public TreeMap<Integer, Integer> withdrawCash(Integer amount) {

		TreeMap<Integer, Integer> withdrawnBanknotes = new TreeMap<Integer, Integer>();
		for (Map.Entry<Integer, Integer> entry : availableBanknotes.entrySet()) {
			Integer banknoteType = entry.getKey();
			if (banknoteType > amount) {
				// go to the greatest key less than or equal to amount
				continue;
			}
			Integer numberOfBanknotes = amount / banknoteType;
			if (numberOfBanknotes > entry.getValue()) {
				numberOfBanknotes = entry.getValue();
			}

			withdrawnBanknotes.put(banknoteType, numberOfBanknotes);
			amount -= banknoteType * numberOfBanknotes;
		}
		if (amount > 0) {
			rollBackTransaction(withdrawnBanknotes);
			System.out.println("The amount cannot be returned");
			return withdrawnBanknotes;
		}
		return withdrawnBanknotes;
	}

	public void rollBackTransaction(Map<Integer, Integer> amount) {
		for (Map.Entry<Integer, Integer> entry : amount.entrySet()) {
			availableBanknotes.put(entry.getKey(),
					availableBanknotes.get(entry.getKey()) + entry.getValue());
		}
		amount.clear();
	}

	public static void displayWithdrawnBancnotes(Map<Integer, Integer> map) {
		for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
			System.out.println(entry.getKey() + " => " + entry.getValue());
		}
	}
}

public class KeyComparator implements Comparator{

	@Override
	public int compare(Object arg0, Object arg1) {
		//reverse order 
		return (Integer)arg1 - (Integer)arg0;				
	}
}

public class AutomatedTellerMachineRunner {

	public static void main(String[] args) {
		AutomatedTellerMachine myATM = new AutomatedTellerMachine();
		myATM.addBancnote(1, 10);
		myATM.addBancnote(5, 15);
		myATM.addBancnote(10, 6);
		myATM.addBancnote(25, 20);
		myATM.addBancnote(50, 4);
		myATM.addBancnote(100, 7);
		myATM.displayAvailableBancnotes();
		System.out.println("Return Amount");
		AutomatedTellerMachine.displayWithdrawnBancnotes(myATM.withdrawCash(73));		
	}
}
Java

Entity Framework and Interface issues

There is, and has been for quite some time a serious issue with Entity Framework – namely that it doesn’t support complex interfaces.

A simple entity based on an interface such as this is fine… 720 more words

.NET

The future of zip codes

Finally. Z. Zero, zoos, zebras, zip codes. Zip codes is the easiest one since I can use someone else’s work and just add a couple of notes. 245 more words

Technology

How Interfaces, Generics and Extension Methods Can Make Code Cleaner

From personal experience, one of my biggest gripes when working with code is code duplication.  Code duplication makes code appear bloated, harder to read, harder to test and can cause replication of not just the code, but of bugs contain within. 670 more words

C#

Forehead 3D mist projector

Another simple idea. I was watching the 1920s period drama Downton Abbey and Lady Mary was wearing a headband with a large jewel in it. I had an idea based on linking mist projection systems to headbands. 187 more words

Technology

OLED fashion contact lenses

Self explanatory concept, but not connected to my original active contact lens direct retinal projection concept. This one is just fashion stuff and could be done easily tomorrow. 71 more words

Technology