public final class SimpleRegister implements ICashRegister { //(value of coin, number of coins) private Map<Integer, Integer> moneyBox; //store the log of transactions for auditing StringBuilder log; /** * Constructs an empty register */ public SimpleRegister() { moneyBox = new TreeMap<Integer, Integer>(); moneyBox.put(1, 0); moneyBox.put(5, 0); moneyBox.put(10, 0); moneyBox.put(25, 0); moneyBox.put(100, 0); moneyBox.put(500, 0); moneyBox.put(1000, 0); log = new StringBuilder(); } @Override public void addPennies(int num) { moneyBox.put(1, moneyBox.get(1) + num); String auditMessage = String.format("Deposit: $%.02f\n", num * 1 / 100.0f); log.append(auditMessage); } @Override public void addNickels(int num) { moneyBox.put(5, moneyBox.get(5) + num); String auditMessage = String.format("Deposit: $%.02f\n", num * 5 / 100.0f); log.append(auditMessage); } @Override public void addDimes(int num) { moneyBox.put(10, moneyBox.get(10) + num); String auditMessage = String.format("Deposit: $%.02f\n", num * 10 / 100.0f); log.append(auditMessage); } @Override public void addQuarters(int num) { moneyBox.put(25, moneyBox.get(25) + num); String auditMessage = String.format("Deposit: $%.02f\n", num * 25 / 100.0f); log.append(auditMessage); } @Override public void addOnes(int num) { moneyBox.put(100, moneyBox.get(100) + num); String auditMessage = String.format("Deposit: $%.02f\n", num * 100 / 100.0f); log.append(auditMessage); } @Override public void addFives(int num) { moneyBox.put(500, moneyBox.get(500) + num); String auditMessage = String.format("Deposit: $%.02f\n", num * 500 / 100.0f); log.append(auditMessage); } @Override public void addTens(int num) { moneyBox.put(1000, moneyBox.get(1000) + num); String auditMessage = String.format("Deposit: $%.02f\n", num * 1000 / 100.0f); log.append(auditMessage); } /** * This implementation attempts to return the optimal number of * coins/notes for the amount, if possible. * It works as follows: * <ul> * <li>Compute required value in cents.</li> * <li>Start from highest denomination.</li> * <li>Find the highest number of coins/notes of current * denomination such that their value is less than required * value.</li> * <li>Include those many notes/coins in the output, and * reduce the required value by the appropriate amount.</li> * <li>If the required value is 0, you are done, else go to step * 3 </li> * <li>If there are no more denominations, throw an exception * because the amount cannot be dispensed with what the * register contains.</li> * </ul> * * @param dollars the dollar amount to be withdrawn * @param cents the cent amount to be withdrawn * @return the dispensed currency as map of * <value of coin/note, number of coins/notes> * @throws IllegalArgumentException if required amount cannot be * dispensed using contents of the register */ @Override public Map<Integer, Integer> makeChange(int dollars, int cents) throws InsufficientCashException { if ((dollars < 0) || (cents < 0)) throw new IllegalArgumentException("Negative dollar or cent value"); int[] denom = {1000, 500, 100, 25, 10, 5, 1}; Map<Integer, Integer> ret = new TreeMap<Integer, Integer>(); int totalvalue = 100 * dollars + cents; for (int i = 0; i < denom.length; i++) { int num = totalvalue / denom[i]; if (moneyBox.get(denom[i]) < num) //there aren't enough coins of this value { num = moneyBox.get(denom[i]); } totalvalue = totalvalue - num * denom[i]; ret.put(denom[i], num); moneyBox.put(denom[i],moneyBox.get(denom[i])-num); } if (totalvalue > 0) { throw new InsufficientCashException("Cannot dispense change."); } String auditMessage = String.format("Withdraw: $%.02f\n", dollars + cents / 100.0f); log.append(auditMessage); return ret; } @Override public Map<Integer, Integer> getContents() { return moneyBox; } public String getAuditLog() { return log.toString(); } }
@Test public void testTwo() throws InsufficientCashException { ICashRegister reg1 = new SimpleRegister(); ICashRegister reg2 = new SimpleRegister(); reg1.addDimes(10); reg1.addOnes(5); reg1.addNickels(5); reg2.addQuarters(10); Map<Integer,Integer> change1 = reg1.makeChange(2,50); Map<Integer,Integer> change2 = reg2.makeChange(2,50); int value1=0; for (Map.Entry<Integer,Integer> s:change1.entrySet()) { value1 += (s.getKey()*s.getValue()); } int value2=0; for (Map.Entry<Integer,Integer> s:change2.entrySet()) { value2 += (s.getKey()*s.getValue()); } assertEquals(value1,value2); }
Question: Look at the test testTwo(). Explain in 1--2 sentences what this is testing for, and why it is an important test. We are not looking for a line-by-line explanation of what its code does: explain the purpose of the test.
The testTwo class checks whether the implemented iCashcRegister and check whether SimpleRegister class holds the functionslity of iCashRegister class completely or not.
Two objects have been created to test two separate instances and both the objects have been called for different functions.
The test class is designed in such a way that it can also handle
exceptions.
The class has used Map to test the implementation of designed class
with the help of key value functions.
and last but not least, the assertEquals() function, which checks the functionality and desired output of the Map implemented for 2 different instances.
Overall, the testTwo class checks whether the inheritance of one class to another class works perfectly or not with i addition with some other separate functionality of SimpleRegister class.
I hope it helps.
Get Answers For Free
Most questions answered within 1 hours.