-
Notifications
You must be signed in to change notification settings - Fork 268
/
procedural_resale_shop.py
88 lines (77 loc) · 3.15 KB
/
procedural_resale_shop.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
"""
Filename: procedural_resale_shop.py
Description: an example of procedural code to run a small computer resale shop,
part of A2: Object-ification, CSC120: Object-Oriented Programming
as taught at Smith College in Spring 2024. Based on an example by Sami Islam.
Author: R. Jordan Crouser (@jcrouser)
Date: 1 February 2024
Note: YOU DO NOT NEED TO MODIFY THIS FILE
"""
# Import a few useful containers from the typing module
from typing import Dict, Optional
""" inventory: a dictionary where we'll store our inventory
The key is an int representing the item number
The value is another dictionary containing information about the machine
"""
inventory : Dict[int, Dict] = {}
itemID = 0 # We'll increment this every time we add a new item
# so that we always have a new value for the itemID
"""
Takes in a Dict containing all the information about a computer,
adds it to the inventory, returns the assigned item_id
"""
def buy(computer: Dict):
global itemID
itemID += 1 # increment itemID
inventory[itemID] = computer
return itemID
"""
Takes in an item_id and a new price, updates the price of the associated
computer if it is the inventory, prints error message otherwise
"""
def update_price(item_id: int, new_price: int):
if item_id in inventory:
inventory[item_id]["price"] = new_price
else:
print("Item", item_id, "not found. Cannot update price.")
"""
Takes in an item_id, removes the associated computer if it is the inventory,
prints error message otherwise
"""
def sell(item_id: int):
if item_id in inventory:
del inventory[item_id]
print("Item", item_id, "sold!")
else:
print("Item", item_id, "not found. Please select another item to sell.")
"""
prints all the items in the inventory (if it isn't empty), prints error otherwise
"""
def print_inventory():
# If the inventory is not empty
if inventory:
# For each item
for item_id in inventory:
# Print its details
print(f'Item ID: {item_id} : {inventory[item_id]}')
else:
print("No inventory to display.")
def refurbish(item_id: int, new_os: Optional[str] = None):
if item_id in inventory:
computer = inventory[item_id] # locate the computer
if int(computer["year_made"]) < 2000:
computer["price"] = 0 # too old to sell, donation only
elif int(computer["year_made"]) < 2012:
computer["price"] = 250 # heavily-discounted price on machines 10+ years old
elif int(computer["year_made"]) < 2018:
computer["price"] = 550 # discounted price on machines 4-to-10 year old machines
else:
computer["price"] = 1000 # recent stuff
if new_os is not None:
computer["operating_system"] = new_os # update details after installing new OS
else:
print("Item", item_id, "not found. Please select another item to refurbish.")
def main():
buy({"description":"2019 MacBook Pro", "processor_type":"Intel", "hard_drive_capacity":256, "memory":16, "operating_system":"High Sierra", "year_made":2019, "price":1000})
print_inventory()
main()