Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

19th Oct 2019, 5:41 AM
Beanie
Beanie - avatar
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; }
19th Oct 2019, 6:24 AM
Hatsy Rei
Hatsy Rei - avatar
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.
19th Oct 2019, 5:54 AM
A͢J
A͢J - avatar
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
19th Oct 2019, 5:58 AM
Beanie
Beanie - avatar
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
19th Oct 2019, 5:59 AM
Beanie
Beanie - avatar
- 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.
19th Oct 2019, 6:01 AM
A͢J
A͢J - avatar