-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lukas
authored and
Lukas
committed
May 25, 2014
0 parents
commit cc57a18
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |