Vending Machine

For this question, we ask you to design a vending machine. We divide this question in to three parts, so you can complete them in order.

Part One

For the first part, you must design a Machine class representing the vending machine. Your system must support these following commands:

  • new_product <name> <price>: Creates a new product object with name which is a string, and price which is a non-negative integer.
  • print_products: Prints all products, each product in a line in format <name> <price>, sorted by price from lowest to highest.
  • insert_coin <value>: Addes money to internal state.
  • purchase <name>: Checks if the user inserted enough money and return a boolean.
  • checkout: Prints the money left by the previous user, and clears internal state for the next user.

You may implement these however you like. However, preferably this should be easily expandable to accommodate new requirements.

Example

Input:

instructions = [
  ["new_product", "apple", "4"],
  ["insert_coin", "5"],
  ["purchase", "apple"],
  ["purchase", "apple"],
  ["checkout"],
]

Output:

[
  "true",
  "false",
  "1"
]

Try it yourself

Solution

Title

Script

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

  >>> a = [1, 2, 3]
  >>> a[-1]
  3

Get premium for instant access to all content and solutions

Upgrade