-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW4.py
240 lines (179 loc) · 7.79 KB
/
HW4.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import unittest
## Name : Haley Johnson
## Student ID: 2694 6938
## Your Email: [email protected]
## People you worked with :
## Github URL : https://github.com/haleyej/HW4
### Customer Class
class Customer:
# Constructor
def __init__(self, name, wallet = 100):
self.name = name
self.wallet = wallet
# Withdraws fast_cash from the atm into the customer's wallet.
def withdraw_money(self, fast_cash):
self.wallet += fast_cash
# Pays the server
def make_order(self, server, amount):
server.receive_payment(server, amount)
# Orders food from the truck to be brought to the table by the server,
# assuming certain conditions are met.
def order_food(self, server, truck, food_name, quantity):
if not(server.serve_truck(truck)):
print("Sorry, I don't serve that food truck. Please try a different one.")
elif self.wallet < server.estimated_cost(truck, quantity):
print("Don't have enough money for that :( Please withdraw more money!")
elif not(truck.has_food(food_name, quantity)):
print("Our food truck has run out of " + food_name + " :( Please try a different truck!")
else:
bill = server.place_order(truck, food_name, quantity)
self.make_order(server, bill)
self.eat_food()
# Eats the ordered food and prints out message indicating this .
def eat_food(self):
print("Wow that was really tasty")
def __str__(self):
return "Hello! My name is " + self.name + ". I have $" + str(self.wallet) + " and I want to order some food."
### Food Truck Server Class
class Server:
# Constructor
def __init__(self, name, money = 200, food_trucks = [], service_fee = 5):
self.name = name
self.money = money
self.food_trucks = food_trucks[:] # makes a copy of the list
self.service_fee = service_fee
# Adds a food truck to the known list of trucks at the festival.
def add_truck(self, new_truck):
self.food_trucks.append(new_truck)
# Receives payment from customer, and adds the money to the server's fanny pack.
def receive_payment(self, money):
self.money += money
# Returns the estimated cost of an order, namely the cost of the foods ( quantity times cost)
# plus the server's own service fee.
def estimated_cost(self, truck, quantity):
return ((truck.cost * quantity) + self.service_fee)
# Places an order at the food truck.
# The server pays the food truck the cost.
# The food truck processes the order
# Function returns cost of the food + service fee.
def place_order(self, truck, food_item, quantity):
self.money = self.money - (truck.cost * quantity)
truck.process_order(food_item, quantity)
return self.estimated_cost(truck, quantity)
# Returns boolean value letting customer know if this server can order from that food truck or not.
def serve_truck(self, truck):
return truck in self.food_trucks
# string function.
def __str__(self):
return "Hello, my name is " + self.name + " I will be your server today, I have this much $" + str(self.money) + "in change. We take cash only. I charge $" + str(self.service_fee) + " and I can order from " + str(len(self.food_trucks)) + " food trucks."
### Create Truck class here
class Truck:
def __init__(self, truck_name, inventory, cost = 7, money = 700):
self.name = truck_name
self.inventory = inventory
self.cost = cost
self.money = money
def process_order(self, food_item, quantity):
if self.has_food(food_item, quantity):
self.inventory -= quantit
self.money += (quantity * self.cost)
def has_food(self, food_item, quantity):
in_inventory = self.inventory[food_item]
if quantity >= in_inventory:
return True
def stock_up(self, food_item, quantity):
self.inventory[food_item] = self.inventory.get(food_item, 0) + quantity
def __str__(self):
return f"Hello, we are {self.name}. This is our current menu {list(self.inventory.keys())}. We charge ${self.cost} per item. we have ${self.money} in total."
def Main():
inventory1 = {"Milkshake": 9, "Fries": 12, "Burgers":16}
inventory2 = {"Salad": 10, "Smoothie": 14, "Omlette":17}
customer1 = Customer('Haley', 150.0)
customer2 = Customer('Zach', 70.0)
truck1 = Truck('American Diner Truck', inventory1, cost = 5, money = 1000)
truck2 = Truck('Health Food to Go', inventory2, cost = 8.50, money = 650)
server1 = Server('Anna', money = 150, food_trucks = [truck1], service_fee = 4.00)
server2 = Server('Andrew', money = 300, food_trucks = [truck1, truck2], service_fee = 6.50)
customer1.order_food(server1, truck1, "Fries", 5)
customer2.order_food(server2, trucky2, "Smoothie", 10)
class TestAllMethods(unittest.TestCase):
def setUp(self):
inventory = {"Burger":40, "Taco":50}
self.f1 = Customer("Ted")
self.f2 = Customer("Morgan", 150)
self.t1 = Truck("The Grill Queen", inventory, cost = 10)
self.t2 = Truck("Tamale Train", inventory, cost = 9)
self.t3 = Truck("The Streatery", inventory)
self.s1 = Server("Greg")
self.s2 = Server("Tina", service_fee = 8, food_trucks = [self.t1, self.t2])
## Check to see whether constructors work
def test_customer_constructor(self):
self.assertEqual(self.f1.name, "Ted")
self.assertEqual(self.f2.name, "Morgan")
self.assertEqual(self.f1.wallet, 100)
self.assertEqual(self.f2.wallet, 150)
## Check to see whether constructors work
def test_server_constructor(self):
self.assertEqual(self.s1.name, "Greg")
self.assertEqual(self.s1.service_fee, 5)
self.assertEqual(self.s2.service_fee, 8)
self.assertEqual(self.s1.food_trucks, [])
self.assertEqual(len(self.s2.food_trucks), 2)
## Check to see whether constructors work
def test_truck_constructor(self):
self.assertEqual(self.t1.name, "The Grill Queen")
self.assertEqual(self.t1.inventory, {"Burger":40, "Taco":50})
self.assertEqual(self.t1.money, 700)
self.assertEqual(self.t2.cost, 9)
# Check that the food truck can stock up properly.
def test_stocking_medicine(self):
inventory = {"Burger":10}
t4 = Truck("Misc Truck", inventory)
# Testing whether food truck can stock up on items
self.assertEqual(t4.inventory,{"Burger":10} )
t4.stock_up("Burger", 30)
self.assertEqual(t4.inventory, {"Burger": 40})
def test_make_payment(self):
# Check to see how much money there is prior to a payment
previous_wallet_customer = self.f2.wallet
previous_money_server = self.s2.money
# Make the payment
self.f2.make_order(self.s2, 30)
# See if money has changed hands
self.assertEqual(self.f2.wallet, previous_wallet_customer - 36)
self.assertEqual(self.s2.money, previous_money_server + 36)
# Check to see that the server can serve from the different trucks
def test_adding_and_serving_truck(self):
s3 = Server("Felix", service_fee = 7, food_trucks = [self.t1, self.t2])
self.assertTrue(s3.serve_truck(self.t1))
self.assertFalse(s3.serve_truck(self.t3))
s3.add_truck(self.t3)
self.assertTrue(s3.serve_truck(self.t3))
self.assertEqual(len(s3.food_trucks), 3)
# Test that estimated cost works properly.
def test_estimated_cost(self):
self.assertEqual(self.s1.estimated_cost(self.t1, 5), 45)
self.assertEqual(self.s2.estimated_cost(self.t2, 6), 61)
# Check that the food truck can properly see when it is empty
def test_has_food(self):
# Test to see if has_food returns True when a food truck has food left
# Test to see if has_food returns True when a food truck has
# just a little bit of food left (i.e., food_left == 1)
# Test to see if has_food returns False when a food truck has no food left
pass
# Test order food
def test_order_food(self):
# test if a customer doesn't have enough money in their wallet to order
# test if the food truck doesn't have any food left in stock
# check if the server can order food from that truck
pass
#Write Test Case
#test if a customer can add money to their wallet
def test_withdraw_money(self):
pass
def main():
pass
if __name__ == "__main__":
main()
print("\n")
unittest.main(verbosity = 2)