Tags » Interfaces

Interfaces, generics, and covariant return types

Suppose I have an interface as follows:

public class Foo<T> {
    T doSomething();

Now, are both the following allowed?

public class Bar implements Foo<Number> { ... 72 more words
Recent Questions - Stack Overflow

Laser spirit level with marked line

Another day, another idea. It probably¬†already exists but I couldn’t find one. If it isn’t already patented, feel free to develop it.


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);

		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
			Integer numberOfBanknotes = amount / banknoteType;
			if (numberOfBanknotes > entry.getValue()) {
				numberOfBanknotes = entry.getValue();

			withdrawnBanknotes.put(banknoteType, numberOfBanknotes);
			amount -= banknoteType * numberOfBanknotes;
		if (amount > 0) {
			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.get(entry.getKey()) + entry.getValue());

	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{

	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);
		System.out.println("Return Amount");

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


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


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