Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas authored and Lukas committed May 25, 2014
0 parents commit cc57a18
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Test/src/main/Pokladna.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main;

import java.util.Iterator;
import java.util.TreeSet;

class Pokladna {

TreeSet<Priehradka> priehradky;

Pokladna() {
priehradky = new TreeSet<Priehradka>();
}

public void vydaj(float value) {

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


}
}
63 changes: 63 additions & 0 deletions Test/src/main/Priehradka.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main;

class Priehradka implements Comparable<Priehradka> {

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

0 comments on commit cc57a18

Please sign in to comment.