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
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
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
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
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
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