Question

In: Computer Science

Question: An ATM can only dispense bills of the following denominations: $500, $100, $50, $10, $5,...

Question: An ATM can only dispense bills of the following denominations: $500, $100, $50, $10, $5, and $1

When a user withdraws an amount from the ATM, the machine tries to dispense the amount using the least number of bills possible.

For instance, if the user requests $1234, the ATM dispenses 2 bills of $500 + 2 bills of $100 + 3 bills of $10 + 4 bills of $1. On the other hand, if the user requests $1235, the machine dispenses 2 bills of $500 + 2 bills of $100 + 3 bills of $10 + 1 bill of $5

Use the skeleton code provided to create an ATM dispenser that takes the amount and prints the least number of bills that makes up the amount in the following format:

__ bills of $500 + __ bills of $100 + __ bills of $50 + __ bills of $10 + __ bills of $5 + __ bills of $1

Apply chain of responsibility pattern such that each denomination is handled by a separate handler.

Below is the code:

abstract class Handler

{

    Handler successor;

    abstract public String dispense(int amount);

}

class Handler500 extends Handler

{

}

class Handler100 extends Handler

{

}

class Handler50 extends Handler

{

}

class Handler10 extends Handler

{

}

class Handler5 extends Handler

{

}

class Handler1 extends Handler

{

}

class ATM

{

    Handler handler;

    public ATM(Handler handler)

    {

        this.handler = handler;

    }

    public string dispense(int amount)

    {

        return handler.dispense();

    }

}

public class ATMTester

{

    public static void main(String[] args)

    {

                    ATM atm;

                /// TODO - create an ATM object with the appropriate handlers and store it in the variable "atm"

System.out.println(atm.dispense(1234)); // should display: 2 bills of $500 + 2 bills of $100 + 3 bills of $10 + 4 bills of $1

System.out.println(atm.dispense(1235)); // should display: 2 bills of $500 + 2 bills of $100 + 3 bills of $10 + 1 bills of $5

    }

}

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

ATMTester.java

package c15;
abstract class Handler

{

   Handler successor;

   abstract public String dispense(int amount);

}

class Handler500 extends Handler

{

   @Override
   public String dispense(int amount) {
       //successor of the 500 handle will be 100
       successor = new Handler100();
      
       //get number of 500
       int count = amount/500;
      
       //get remaining amount
       amount = amount%500;
      
       //print count
       if(count!=0)
           return count+" bills of $500 + "+successor.dispense(amount);
       else
           return successor.dispense(amount);
   }

}

class Handler100 extends Handler

{

   @Override
   public String dispense(int amount) {
       successor = new Handler50();
       int count = amount/100;
       amount = amount%100;
       if(count!=0)
           return count+" bills of $100 + "+successor.dispense(amount);
       else
           return successor.dispense(amount);
   }}

class Handler50 extends Handler

{

   @Override
   public String dispense(int amount) {
       successor = new Handler10();
       int count = amount/50;
       amount = amount%50;
       if(count!=0)
           return count+" bills of $50 + "+successor.dispense(amount);
       else
           return successor.dispense(amount);
   }
}

class Handler10 extends Handler

{
   @Override
   public String dispense(int amount) {
       successor = new Handler5();
       int count = amount/10;
       amount = amount%10;
       if(count!=0)
           return count+" bills of $10 + "+successor.dispense(amount);
       else
           return successor.dispense(amount);
   }
}


class Handler5 extends Handler

{
   @Override
   public String dispense(int amount) {
       successor = new Handler1();
       int count = amount/5;
       amount = amount%5;
       if(count!=0)
           return count+" bills of $5 "+successor.dispense(amount);
       else
           return successor.dispense(amount);
   }
}

class Handler1 extends Handler

{
   @Override
   public String dispense(int amount) {
       //handle 1 will not have any successor
       if(amount!=0)
           return amount+" + bills of $1 ";
       else
           return "";
   }
}

class ATM

{

   Handler handler;

   public ATM(Handler handler)

   {

       this.handler = handler;

   }


   public String dispense(int amount)

   {

       return handler.dispense(amount);

   }

}


public class ATMTester

{

   public static void main(String[] args)

   {

       //create atm object
       ATM atm;

       //create an ATM object with the appropriate handlers and store it in the variable "atm"
       //500 handle will be the first one
       Handler handle500 = new Handler500();
       atm = new ATM(handle500);

       System.out.println(atm.dispense(1234)); // should display: 2 bills of $500 + 2 bills of $100 + 3 bills of $10 + 4 bills of $1

       System.out.println(atm.dispense(1235)); // should display: 2 bills of $500 + 2 bills of $100 + 3 bills of $10 + 1 bills of $5

   }

}

output


Related Solutions

The Treasury Department auctioned $10 billion in 3-month bills in denominations of $10,000 at a discount...
The Treasury Department auctioned $10 billion in 3-month bills in denominations of $10,000 at a discount rate of 3.000%. What would be the effective rate of interest?
10? + 50? + 20? + 10? = 100 5? + 15? + 75? − 25?...
10? + 50? + 20? + 10? = 100 5? + 15? + 75? − 25? = 200 25a − 15? − 5? = 300 10? + 20? − 30? + 100? = 400 how to do flowchart using gauss elimination and lu decomposition method
10? + 50? + 20? + 10? = 100 5? + 15? + 75? − 25?...
10? + 50? + 20? + 10? = 100 5? + 15? + 75? − 25? = 200 25a − 15? − 5? = 300 10? + 20? − 30? + 100? = 400 how to write coding in matlab using lu decomposition
A box contains forty $1 bills, six $5 bills, three $20 bills, and one $100 bill....
A box contains forty $1 bills, six $5 bills, three $20 bills, and one $100 bill. A bill is randomly selected, let X be the dollar value of the bill. [2a] Construct a probability distribution of X. [2b] Find the expectation of X. [2c] Find the variance and the standard deviation of X.
C++ language code plzzzzz As we know, here're bills with values: 1, 5, 10, 20, 50,...
C++ language code plzzzzz As we know, here're bills with values: 1, 5, 10, 20, 50, 100. Now, suppose you have enough amount for each kind of bill, when given a price, decide how many bills of each kind you should pay so that the total number of bills is minimized. Output the number of each bill you need to pay in the order 1, 5, 10, 20, 50, 100. SAMPLE INPUT 77 SAMPLE OUTPUT 2 1 0 1 1...
Suppose a wallet contains 3 $100 bills and 5 $1 bills. If you randomly choose 4...
Suppose a wallet contains 3 $100 bills and 5 $1 bills. If you randomly choose 4 different bills, what is the probability of taking exactly $202?
Suppose a wallet contains 3 $100 bills and 5 $1 bills. If you randomly choose 4...
Suppose a wallet contains 3 $100 bills and 5 $1 bills. If you randomly choose 4 different bills, what is the probability of taking exactly $202?
An ATM with a service fee of $5 is used by a person 100 times in...
An ATM with a service fee of $5 is used by a person 100 times in a year. What would be the future value in 6 years (use a 4 percent rate) of the annual amount paid in ATM fees. Round FVA factor to 3 decimal places and final answer to 2 decimal places.
Expected Return Std. Deviation X 15% 50% M 10% 20% T-bills 5% 0% The correlation coefficient...
Expected Return Std. Deviation X 15% 50% M 10% 20% T-bills 5% 0% The correlation coefficient between X and M is 2 .2 a)     Draw the opportunity set of securities X and M.   b)     Find the optimal risky portfolio ( O ), its expected return, standard deviation, and Sharpe ratio. Compare with the Sharpe ratio of X and M.   c)      Find the slope of the CAL generated by T-bills and portfolio O. d)    Suppose an investor places 2/9 (i.e., 22.22%)...
Butane (at 75°F and 1 atm) is fed to a boiler at 100 mol/s with 50%...
Butane (at 75°F and 1 atm) is fed to a boiler at 100 mol/s with 50% excess air. 70% of the butane is consumed, and the product gas contains 10 moles CO2 per mol CO. a) Calculate the molar composition of the stack gas on i) wet basis, and ii) dry basis. b) Compute the volume of stack gas at 170°F and 1 atm pressure per ft^3 of Butane.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT