Tags » Interfaces

Applying creative thinking and problem solving to your Business and Finance Systems Equals Success

Applying creative thinking and problem solving to your business and finance Systems Equals Success

This is what I Bring to Your Organisation
• Independent Non Aligned Advice… 129 more words

Finance Systems

Abstraction vs abstract

Abstraction is very important in Java. And there’s the keyword abstract. But the use of abstract is not abstraction. It’s a common misconception that the  521 more words

Java

Where's the @ key? An unintuitive interface on a photocopier / scanner

This is the display shown when a user (most recently me) is trying to register an email address so that documents can be scanned and sent by email as an attachment, without having to type in the address each time. 37 more words

Non CHI+MED

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.

Technology

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