-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSoda-Machine-Class.py
More file actions
48 lines (44 loc) · 2.39 KB
/
Soda-Machine-Class.py
File metadata and controls
48 lines (44 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Author: Jessica Strait
# This project introduces elementary class methods through a soda machine simulation.
# Main functions include purchase, deposit, and restock
class SodaMachine:
def __init__(self, product, price):
# First, I'll assign some variables to make life a little easier. We should also set our balance and
# stock to zero initially.
self.product = product
self.price = price
self.balance = 0
self.stock = 0
def purchase(self):
# We'll start with an if statement to ensure that at least one soda is in stock for our customer to buy.
if self.stock < 1:
return 'Product out of stock'
else:
# Now, if the balance is less than the price, the customer needs to deposit the remaining difference.
if self.balance < self.price:
return 'Please deposit ${}'.format(int(self.price-self.balance))
# If the balance is exactly that of the price, there is no change. Remember to decrease the stock though!
elif self.balance == self.price:
self.balance = 0
self.stock -= 1
return '{} dispensed'.format(self.product)
# In this case, we can use simple subtraction to determine the change for the customer. Again, remember to
# decrease the stock by one. This time, we need to reset the balance to zero manually, because we "told the
# user to take their change, which would set the balance to zero.
elif self.balance > self.price:
self.stock -= 1
change = self.balance - self.price
self.balance = 0
return '{} dispensed, take your ${}'.format(self.product, int(change))
def deposit(self, amount):
# Let's start by checking if there is any soda to be sold. If not, the balance does not change.
if self.stock <= 0:
return 'Sorry, out of stock. Take your ${} back'.format(amount)
else:
# We should increase the balance by the amount deposited.
self.balance += amount
return 'Balance: ${}'.format(self.balance)
def restock(self, amount):
# We should increase the stock by the amount restocked by our customer.
self.stock += amount
return 'Current soda stock: {}'.format(self.stock)