Write an Eventlang program that models a vending machine that dispenses four objects: milk, cookies, chocolate, and
Question:
Write an Eventlang program that models a vending machine that dispenses four objects: milk, cookies, chocolate, and candy. The vending machine accepts an amount, dispenses the appropriate product, and gives change. Milk costs $1, cookies cost $0.75, chocolate costs $0.50, and candy costs $0.25.
The program works as follows:
- When the event CoinInserted is announced with amount inserted as the payload, the handlers for milk, cookies, chocolate, and candy are all run, as well as a handler to prompt for adding more money.
- If the amount inserted is greater than $1, then the handler for milk announces the event Dispense with the string “milk.” It also announces the event Change, with the remaining change as the payload.
- If the amount inserted is greater than $0.75 and less than $1, then the handler for cookies announces the event Dispense with the string “cookies.” It also announces the event Change, with the remaining change as the payload.
- If the amount inserted is greater than $0.5 and less than $0.75, then the handler for chocolate announces the event Dispense with the string “chocolate.” It also announces the event Change, with the remaining change as the payload.
- If the amount inserted is greater than $0.25 and less than $0.5, then the handler for candy announces the event Dispense with the string “candy.” It also announces the event Change, with the remaining change as the payload.
- If the amount inserted is less than $0.25, then the handler for adding more money prints “Add more money.” It also announces the event Change, with the remaining inserted money that is less than $0.25, is returned as the payload.
The handler for the event Change prints the change to be given, and the handler for the event Dispense prints the product that is being dispensed.
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Related Book For
An Experiential Introduction To Principles Of Programming Languages
ISBN: 9780262045452
1st Edition
Authors: Hridesh Rajan
Question Posted: