In: Computer Science
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
}
}
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
