I'm suppose return a change to user.

Initially, the machine has
Bills - 0
quarters - 5
dimes - 5
nickels - 5

user puts a money to machine(each coins are sum up to only maxium \$1 i.e, user can only put 10 dimes maximum, or 20 nickels) but only a dollar to machine.

let's say user buys a candy which cost 65 cents. like in real situation vending machine, if the machine does not have enough quarters, then it should give changes in dimes/nickels.

also, if change in machine does not have any chnage or enough change, it should just return whatever user put in previously.

moreover, for a 65 cents candy, the machine does not have any change left and user put
15 nickels(15 x 5= 75cents) then it should return 2 nickels.

## All 2 Replies

Or are you just another lazy homework kiddo that wants real people to do its homework for it because it's too busy playing computer games or watching television?

here when in register, if the machine only has a
\$1 - 0
Quarter -0
dime - 0
nickel -5

and user pays 3 quarters and the change is 10 cents. however we don't have the dime we have to return two nickels.

Now my code just ignore that and keep adding quarters and not displaying nickel changes.

how should i fix it?

``````public CandyBarTx buyCandyBar(Money money) {
//Money changeObj = new Money(0,0,0,0);
int change = (money.toCents())- 65;
int nrBills =0, nrDimes=0, nrNickels=0, nrQuarters=0;

boolean insufficientFunds = false;
boolean noChange = false;
boolean soldOut = false;
CandyBar candy = null;

if (candyDispenser.getSize()<=0){

soldOut = true;  // we don't have a candy in machine
snackCashRegister.removeMoneyregister(money);
return new CandyBarTx( candy,  money,  soldOut,  insufficientFunds, noChange);
}

else if ((snackCashRegister.getCents()-65) < 0 ){
snackCashRegister.removeMoneyregister(money);
return new CandyBarTx( candy,  money,  soldOut,  insufficientFunds, noChange);
}

else if (money.toCents()-65<0){
insufficientFunds =true;
snackCashRegister.removeMoneyregister(money);
return new CandyBarTx( candy,  money,  soldOut,  insufficientFunds, noChange);
}

else if (  ((snackCashRegister.getBills())> 0) ||
((snackCashRegister.getQuarters())> 0) ||
((snackCashRegister.getDimes())> 0) ||
((snackCashRegister.getDimes())> 0)){  //we do have a candy or candies and start find changes!!
while((change >= 100) && (snackCashRegister.getBills())> 0){
snackCashRegister.removeMoneyregister(new Money(0,0,0,COIN));
change -= 100;
nrBills ++;
}
while((change >= 25) && (snackCashRegister.getQuarters())> 0){
snackCashRegister.removeMoneyregister(new Money(0,0,COIN,0));
change -= 25;
nrQuarters++;
}
while((change >= 10) && (snackCashRegister.getDimes())> 0){
snackCashRegister.removeMoneyregister(new Money(0,COIN,0,0));
change -= 10;
nrDimes++;
}
while((change >= 5) && (snackCashRegister.getDimes())> 0){
snackCashRegister.removeMoneyregister(new Money(COIN,0,0,0));
change -= 5;
nrNickels++;
}

candy = candyDispenser.removeBar(); //now removes a Candy Bar.
return new CandyBarTx( candy,  new Money(nrNickels, nrDimes, nrQuarters, nrBills),  soldOut,  insufficientFunds, noChange);
}
else { //change !=0
noChange = true;
return new CandyBarTx( candy,  money,  soldOut,  insufficientFunds, noChange);
}
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.