88 */
99class Troco {
1010
11- protected int [] papelMoeda ;
12- protected int [] valorPapelMoeda = {2 , 5 , 10 , 20 , 50 , 100 };
11+ protected PapelMoeda [] papeisMoeda ;
1312
1413 public Troco (int valor ) {
15- papelMoeda = new int [6 ];
16- for (int i = 0 ; i < papelMoeda .length ; i ++) {
17- papelMoeda [0 ] = 0 ;
18- }
14+ papeisMoeda = new PapelMoeda [6 ];
15+ int count = 0 ;
1916 while (valor % 100 != 0 ) {
20- papelMoeda [ 5 ] ++;
17+ count ++;
2118 }
19+ papeisMoeda [5 ] = new PapelMoeda (100 , count );
20+ count = 0 ;
2221 while (valor % 50 != 0 ) {
23- papelMoeda [ 4 ] ++;
22+ count ++;
2423 }
24+ papeisMoeda [4 ] = new PapelMoeda (50 , count );
25+ count = 0 ;
2526 while (valor % 20 != 0 ) {
26- papelMoeda [ 3 ] ++;
27+ count ++;
2728 }
29+ papeisMoeda [3 ] = new PapelMoeda (20 , count );
30+ count = 0 ;
2831 while (valor % 10 != 0 ) {
29- papelMoeda [ 2 ] ++;
32+ count ++;
3033 }
34+ papeisMoeda [2 ] = new PapelMoeda (10 , count );
35+ count = 0 ;
3136 while (valor % 5 != 0 ) {
32- papelMoeda [ 1 ] ++;
37+ count ++;
3338 }
39+ papeisMoeda [1 ] = new PapelMoeda (5 , count );
40+ count = 0 ;
3441 while (valor % 2 != 0 ) {
35- papelMoeda [ 1 ] ++;
42+ count ++;
3643 }
44+ papeisMoeda [1 ] = new PapelMoeda (2 , count );
3745 }
3846
39- public Iterator <Integer > getIterator () {
47+ public Iterator <PapelMoeda > getIterator () {
4048 return new TrocoIterator (this );
4149 }
4250
43- class TrocoIterator implements Iterator <Integer > {
51+ class TrocoIterator implements Iterator <PapelMoeda > {
4452
4553 protected Troco troco ;
4654
@@ -51,20 +59,20 @@ public TrocoIterator(Troco troco) {
5159 @ Override
5260 public boolean hasNext () {
5361 for (int i = 6 ; i >= 0 ; i ++) {
54- if (troco .papelMoeda [i ] != 0 ) {
62+ if (troco .papeisMoeda [i ] != null ) {
5563 return true ;
5664 }
5765 }
5866 return false ;
5967 }
6068
6169 @ Override
62- public Integer next () {
63- Integer ret = null ;
70+ public PapelMoeda next () {
71+ PapelMoeda ret = null ;
6472 for (int i = 6 ; i >= 0 && ret != null ; i ++) {
65- if (troco .papelMoeda [i ] != 0 ) {
66- troco .papelMoeda [i ]-- ;
67- ret = troco .valorPapelMoeda [i ];
73+ if (troco .papeisMoeda [i ] != null ) {
74+ ret = troco .papeisMoeda [i ];
75+ troco .papeisMoeda [i ] = null ;
6876 }
6977 }
7078 return ret ;
0 commit comments