Friend I am trying to find best code so I can convert cents into minimum format here is my function
Update me if you have idea about it or have shortest code!Code:public static Change getChange(int cents, Change availableChange) { /* * This method should take two parameters 1.cents as an Integer, and 2.availableChange as a Change object (see below) * It should return the same amount, or the maximum amount possible, in dollars and coins given what change is available in the availableChange parameter. * It should use the minimum number of coins possible, given what is available in availableChange. For example: 164 cents = 1 dollar, 2 quarters, 1 dime and 4 cents. * Return null if the parameter is negative. */ int dollars; // 100 cents int quarters; //25 cents int dimes; // 10 cents int nickels; // 5 cents int centbalance; // 1 cent if (cents < 0) return null; if (cents >= 100) quarters = Math.DivRem(cents, 100, out dollars); //if (quarters >= 25) // quarters = Math.DivRem(quarters, 25, out dollars); //availableChange = new Change(dollars, quarters, dimes, nickels, centbalance); return availableChange; } public class Change { private int _dollars; // 100 cents private int _quarters; //25 cents private int _dimes; // 10 cents private int _nickels; // 5 cents private int _cents; // 1 cent public Change(int dollars, int quarters, int dimes, int nickels, int cents) { _dollars = dollars; _quarters = quarters; _dimes = dimes; _nickels = nickels; _cents = cents; } public int getDollars() { return _dollars; } public int getQuarters() { return _quarters; } public int getDimes() { return _dimes; } public int getNickels() { return _nickels; } public int getCents() { return _cents; } }
Shakt




Reply With Quote
