+ 1
Java
This method takes the parameter price and determines whether or not the customer has deposited enough coins to buy the item for the price passed to the method. If they did not deposit enough money, return null. Iâm trying to implement the buy method any suggestions? https://code.sololearn.com/cB5W569D0LOA/?ref=app https://code.sololearn.com/cAEMI4WR19c1/?ref=app
5 Answers
+ 5
I don't have a clear idea of how and where your coins are supposed to go before being "returned" to the buyer (I see a lot of tubes, and methods invoked using Coin, methods whose definition I fail to find). Nonetheless, if your buy() and change() methods do work, the following should print the number and type of coins that should be returned as change. It's up to you where you want them to go:
public int[] buy(int price) {
if (valueOfCoinsDeposited >= price) {
int[] give = change(valueOfCoinsDeposited - price);
System.out.println(give[0] + " quarters");
System.out.println(give[1] + " dimes");
System.out.println(give[2] + " nickels");
}
else return null;
}
0
So what is problem to implement that method. First think from your side that how to implement. If you know the real time example you can do that.
These are two different codes. You can't use them together here.
0
public int[] buy(int price)
{
int changeLeft ;
if ( valueOfCoinsDeposited >= price ) {
changeLeft = valueOfCoinsDeposited - price;
int [] give = change(changeLeft);
for (int i = 0; i < changeTube.length; i++ ) {
if( valueOfCoinsDeposited < price ) {
return null;
}
}
}
}
This is my buy method
0
public int [] change(int changeToGive) {
int [] changeReturn = new int [3];
changeReturn [0] = changeToGive /25;
changeToGive %= 25;
changeReturn[1] = changeToGive/10;
changeToGive %= 10;
changeReturn[2] = changeTogive / 5;
retun changeReturn;
}
This is the method to make change
- 1
First solve error which are coming. As I told you you can't use two Java codes here so you have to write class Coin in a first code.