diff --git a/Test/src/main/Pokladna.java b/Test/src/main/Pokladna.java new file mode 100644 index 0000000..b9c5b7d --- /dev/null +++ b/Test/src/main/Pokladna.java @@ -0,0 +1,65 @@ +package main; + +import java.util.Iterator; +import java.util.TreeSet; + +class Pokladna { + + TreeSet priehradky; + + Pokladna() { + priehradky = new TreeSet(); + } + + public void vydaj(float value) { + + Iterator iterator = priehradky.descendingIterator(); + + float remaining = value; + while (iterator.hasNext() && remaining > 0) { + Priehradka current = iterator.next(); + float returns = current.getCash(remaining); + remaining -= returns; + System.out.println("Returning: " + returns + " in " + current.getValue()); + } + if (remaining >= 0) { + System.out.println("Cant return " + remaining); + } else { + System.out.println("Returned all"); + } + } + + public void addCash(float value, int count) { + boolean found = false; + for (Priehradka current: priehradky) { + if (current.getValue() == value) { + found = true; + current.addCash(count); + } + } + if (!found) { + priehradky.add(new Priehradka(value, count)); + } + } + + public static void main(String[] args) { + + Pokladna pokladna = new Pokladna(); + + pokladna.addCash(5000, 2); + pokladna.addCash(2000, 2); + pokladna.addCash(1000, 4); + pokladna.addCash(500, 5); + pokladna.addCash(200, 4); + pokladna.addCash(100, 10); + pokladna.addCash(50, 9); + pokladna.addCash(20, 20); + pokladna.addCash(10, 5); + pokladna.addCash(2, 10); + pokladna.addCash(1, 50); + + pokladna.vydaj(22043.50f); + + + } +} diff --git a/Test/src/main/Priehradka.java b/Test/src/main/Priehradka.java new file mode 100644 index 0000000..93372bb --- /dev/null +++ b/Test/src/main/Priehradka.java @@ -0,0 +1,63 @@ +package main; + +class Priehradka implements Comparable { + + final float value; + int count; + + Priehradka(float value) { + this.value = value; + } + + Priehradka(float value, int count) { + this.value = value; + this.count = count; + } + + /** + * + * @param count accepts negative values + */ + public void addCash(int count) { + this.count += count; + } + + /** + * + * @param needed value + * @return value that was available + */ + public float getCash(float needed) { + float remaining = needed; + float result = 0; //can be substitued with "return" object that holds value and count of returned coins (if you need to control how many coins can be returned total) + while (remaining >= this.value && count > 0) { + remaining -= this.value; + result += this.value; + count--; + } + return result; + } + + public float getValue() { + return this.value; + } + + @Override + public int hashCode() { + return Float.hashCode(value); + } + + @Override + public boolean equals(Object o) { + if (o instanceof Priehradka) { + return this.value == ((Priehradka) o).value; + } else { + return false; + } + } + + @Override + public int compareTo(Priehradka arg0) { + return Float.compare(this.value, arg0.value); + } +}