×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Vending Machine Model Assignment Solutions

July 08, 2024
Prof. Dominic Walto
Prof. Dominic
🇬🇧 United Kingdom
Programming
Prof. Dominic Walton, an esteemed programming exam taker, hails from the University of Glasgow, UK. With a distinguished 15-year tenure and a Ph.D. under his belt, his expertise ensures unparalleled excellence in exam execution.
Tip of the day
Ensure you understand the kinematics and dynamics principles governing robotic motion. Use flowcharts and diagrams to plan the system's behavior and debug efficiently. Document every step for clarity.
News
Stanford University has revamped its Code in Place course, emphasizing large-scale interactive programming education, while Coursera now offers specialized certificates in game design and mobile app development with integrated AI tools like Generative AI​

Implement a class that models a vending machine.

The vending machines we model shall be quite simple. They hold a single kind of drink, assume a bottle of water. They can accept money (the US, coins, but not pennies) and can dispense drinks and return change. 

Solution:

# File: VendingMachine.py # # Create a class that models a vending machine. # # # Name: # EID: # Email: # Grader: # # On my honor, , this programming assignment is my own work # and I have not provided this code to any other student. import random # CS303e Students. Complete the VendingMachine class below as # specified in the assignment handout. Delete this comment afterwards. """Models a Vending Machine. All transactions are in cents. """ class VendingMachine: def __init__(self, numDrinks, price: int, capacity): if numDrinks > capacity: raise KeyError() if price <= 0: raise KeyError() self.capacity = capacity self.price = price self.money: int = 0 self.numDrinks = numDrinks def insert_money(self, money): self.money += money def can_dispense(self): return self.numDrinks > 0 and self.money >= self.price def dispense(self): if self.numDrinks <= 0: return 'EMPTY' elif self.money < self.price: return 'INSUFFICIENT FUNDS' else: self.money = self.money - self.price self.numDrinks -= 1 return 'ENJOY' def get_change(self): money = self.money self.money = 0 return money def refill(self): self.numDrinks = self.capacity def main(): """# Run a simulation using the Vending Machine objects.""" if input('Run simple tests? ') == 'y': simple_tests() stress_tests() def simple_tests(): """Simple operations with a single Vending Machine. The VendingMachine has a capacity of 5 drinks, currently has 2 drinks, and it costs 50 cents per drink. """ print('***** SIMPLE TESTS *****') vend1 = VendingMachine(2, 50, 5) vend1.insert_money(25) vend1.insert_money(10) vend1.insert_money(10) print('can dispense:', vend1.can_dispense()) print('dispense:', vend1.dispense()) vend1.insert_money(10) print('can dispense:', vend1.can_dispense()) print('dispense:', vend1.dispense()) print('can dispense:', vend1.can_dispense()) print('get change:', vend1.get_change()) print('get change:', vend1.get_change()) vend1.insert_money(100) vend1.insert_money(25) print('can dispense:', vend1.can_dispense()) print('dispense:', vend1.dispense()) print('can dispense:', vend1.can_dispense()) print('dispense:', vend1.dispense()) vend1.refill() print('can dispense:', vend1.can_dispense()) print('dispense:', vend1.dispense()) print('can dispense:', vend1.can_dispense()) print('dispense:', vend1.dispense()) print('get change:', vend1.get_change()) print() def stress_tests(): """Run stress tests. Given the same number of machines, the same number of operations, and the same initial seed, output will be the same. """ # Get the seed, number of operations, # and number of machines from the user. random.seed(int(input('Enter random seed: '))) num_operations = int(input('Enter the number of operations: ')) num_machines = int(input('Enter the number of machines: ')) print('\n***** STRESS TESTS *****') # Create the required number of machines and run the tests. machines = create_machines(num_machines, 10) perform_stress_tests_ops(machines, num_operations) def perform_stress_tests_ops(machines, num_operations): """# Run the actual operations for the stress tests.""" # Only allow US coins, excluding pennies! # (We should get rid of pennies and probably nickels too!) coins = [5, 10, 25, 50, 100] last_coin_index = len(coins) - 1 last_machine_index = len(machines) - 1 # Pick a random operation. # 55% chance of insert_money. # 20% chance of can dispense. # 21% chance of dispense. (If we dispense, there is a 80% # chance dispense is called directly after.) # 3% chance of get_change. # 1% chance of refill. for i in range(1, num_operations + 1): print('Operation =', i) machine_number = random.randint(0, last_machine_index) print('Machine =', machine_number) machine = machines[machine_number] random_op = random.randint(1, 100) if random_op <= 55: coin = coins[random.randint(0, last_coin_index)] print('Insert money, amount =', coin) machine.insert_money(coin) elif random_op <= 75: print('Can dispense:', machine.can_dispense()) elif random_op <= 96: result = machine.dispense() print('Dispense:', result) if result == 'ENJOY': # Do they remember their change? 80% chance of yes. if random.randint(1, 5) <= 4: print('Get change, amount =', machine.get_change()) else: print('Forgot change after dispense.') elif random_op <= 99: print('Get change, amount =', machine.get_change()) else: print('Refill.') machine.refill() print() def create_machines(num_machines, max_capacity): """Create a list with the required number of machines. I expect num_machines > 0. The first machine always has a price of 25, 1 drink, and a capacity of 3. All machines will have a price between 25 and 200 in multiples of 25. All machines will have a capacity between 2 and max_capacity. """ price_multiplier = 25 machines = [VendingMachine(1, price_multiplier, 3)] print('Machine 0: price = 25, capacity = 3, initial drinks = 1') for i in range(1, num_machines): price = random.randint(1, 8) * price_multiplier capacity = random.randint(2, max_capacity) drinks = random.randint(0, capacity) print('Machine ', i, ': price = ', price, ', capacity = ', capacity, ', initial drinks = ', drinks, sep='') machines.append(VendingMachine(drinks, price, capacity)) print() return machines main()

Similar Samples

Explore our comprehensive library of programming assignment samples at ProgrammingHomeworkHelp.com. From Java and Python to C++ and beyond, our samples exemplify mastery in diverse programming languages and concepts. Each solution is crafted to demonstrate clarity, efficiency, and problem-solving skills, ensuring your understanding and success in programming coursework.