I need a coin removal function written in java that can take a price and a bank
ID: 3591853 • Letter: I
Question
I need a coin removal function written in java that can take a price and a bank with values for quarters, dimes, nickels, and pennies and determine what coins are available to pay for an item and deduct them from the balance. function declared like public void removeCoins(Bank currentBank, float price) price is price of object that is being paid for via bank in format of float ie $2.50 would be 2.5f currentBank has currentBank.quarters, currentBank.dimes, currentBank.nickels, currentBank.pennies. This is a literal count of the number of quarters, dimes, etc..
Please only make the coin removal function, the rest of the program is already written. All I need is a function to calculate the transaction. This should be possible with just the number of each coin and the price of the item. Also, please make sure to consider that when an item cost $1.00 and there is more than enough coins of various denominations. IE 5 quarters, 3 dimes, 2 nickels, and 3 pennies for a $1.00 transaction
Explanation / Answer
Assumption: Enough coins of each denomination available.
Comments: Please remove errors if found as this was not an executed code. Comment if any extra functionality required. Thanks
Code:
public void removeCoins(Bank currentBank, float price){
int quarters, dimes, nickels, pennies;
int coins = (int) price*100; //converting into cents
//counting and removing quarters
quarters = (int)(coins/25);
currentBank.quarters = currentBank.quarters - quarters;
coins = coins - (25*quarters);
//counting and removing dimes
dimes = (int)(coins/10);
currentBank.dimes = currentBank.dimes - dimes;
coins = coins - (10*dimes);
//counting and removing nickels
nickels = (int)(coins/5);
currentBank.nickels = currentBank.nickels - nickels;
coins = coins - (5*nickels);
//counting and removing pennies
pennies = (int)(coins/1);
currentBank.pennies = currentBank.pennies - pennies;
coins = coins - (1*pennies);
System.out.println("Price paid successfully. With "+quarters+" quarters, "+dimes+" dimes, "+nickels+" nickels and "+pennies+" pennies.");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.