def test_to_and_from_json_inverse(loaded_machine):
    """
    Test case to ensure the machine is json serializable
    and will load to its previous state if reloaded
    immediately.
    """

    dumped = loaded_machine.to_json()

    loaded = VendingMachine.from_json(dumped)
    assert loaded == loaded_machine
Exemple #2
0
def view_purchases():
    """
    Views all purchases. If no machine exists, error is thrown.
    """
    if not os.path.isfile(STATE_FILE_LOCATION):
        fancy_print(LOG_ERROR, "Please initialize the vending machine first.")
        return None

    with open(STATE_FILE_LOCATION) as file:
        loaded = json.load(file)

    machine = VendingMachine.from_json(loaded)
    machine.view_purchases()

    return None
Exemple #3
0
def view_balance():
    """
    Views the current balance you have in the machine. If no machine exists, error is thrown.
    """

    if not os.path.isfile(STATE_FILE_LOCATION):
        fancy_print(LOG_ERROR, "Please initialize the vending machine first.")
        return None

    with open(STATE_FILE_LOCATION) as file:
        loaded = json.load(file)

    machine = VendingMachine.from_json(loaded)
    fancy_print(LOG_SUCCESS, f"Your current balance is {machine.balance}.")

    return None
Exemple #4
0
def view_items(column: str = None, row: int = None, position: str = None):
    """
    Viewing what items are in the machine. Filters are additive.
    """

    if not os.path.isfile(STATE_FILE_LOCATION):
        fancy_print(LOG_ERROR, "Please initialize the vending machine first.")
        return None

    with open(STATE_FILE_LOCATION) as file:
        loaded = json.load(file)

    machine = VendingMachine.from_json(loaded)
    if position:
        machine.view_items(position[0], position[1])
    else:
        machine.view_items(column, row)

    return None
Exemple #5
0
def dispense_change():
    """
    Removing the money from the existing vending machine. If no machine exists, error is thrown.
    """

    if not os.path.isfile(STATE_FILE_LOCATION):
        fancy_print(LOG_ERROR, "Please initialize the vending machine first.")
        return None

    with open(STATE_FILE_LOCATION) as file:
        loaded = json.load(file)

    machine = VendingMachine.from_json(loaded)
    machine.dispense_change()

    with open(STATE_FILE_LOCATION, "w") as file:
        json.dump(machine.to_json(), file)

    return None
Exemple #6
0
def add_money(amount: float):
    """
    Adding money to the existing vending machine. If no machine exists, error is thrown.
    """

    if not os.path.isfile(STATE_FILE_LOCATION):
        fancy_print(LOG_ERROR, "Please initialize the vending machine first.")
        return None

    with open(STATE_FILE_LOCATION) as file:
        loaded = json.load(file)

    machine = VendingMachine.from_json(loaded)
    machine.deposit(Decimal(amount))

    with open(STATE_FILE_LOCATION, "w") as file:
        json.dump(machine.to_json(), file)

    return None