Question

public final class SimpleRegister implements ICashRegister { //(value of coin, number of coins) private Map<Integer, Integer>...

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 
     *         &lt;value of coin/note, number of coins/notes&gt;
     * @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.

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list code and can't figure out how to complete it: Below is the code Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. /////////////////////////////////////////////////////////////////////////////////////////////////////////// Grocery Class (If this helps) package Shopping; public class Grocery implements Comparable<Grocery> { private String name; private String category; private int aisle; private float price; private int quantity;...
Which method is correct to access the value of count? public class Person { private String...
Which method is correct to access the value of count? public class Person { private String name; private int age; private static int count = 0; } A. private int getCount() {return (static)count;} B. public static int getCount() {return count;} C. public int getCount() {return static count;} D. private static int getCount() {return count;} How can you print the value of count? public class Person { private String name; private int age; private static int count=0; public Person(String a, int...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
package Week7_Quiz; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.control.Label;
package Week7_Quiz; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.control.Label; import javafx.stage.Stage; public class Week7_Quiz extends Application { private TextField tf1; private Label stckr; private int revrsdNum; private Button btn; int num = 0; int reverse; public static void main(String[] args) {       launch (args); } @Override public void start(Stage stage) throws Exception { tf1 = new TextField(); tf1.setLayoutX(10); tf1.setLayoutY(50);    stckr =new Label ("Result: "); stckr.setLayoutX(12); stckr.setLayoutY(100);    btn = new Button("Reverse");...
I'm generating a random number every 10 second and Storing and Updating the number every 10...
I'm generating a random number every 10 second and Storing and Updating the number every 10 second when I generate new number in UpdateInt method. However, In my main method when I called UpdateInt method it generate every 10 second new number but it doesn't update and store in the arraylist. ***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the arraylist that has the update number*** import java.util.ArrayList; import...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT