Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Programs/shoppingcart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class ShoppingCart:
def __init__(self):
self.cart = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JAS Confidence Score: 72% View Citation

JAS - Just a suggestion
Vague but Functional Generic Name

The variable name cart is functional but could be more expressive to indicate it stores items and their associated prices, such as items_to_price_map or cart_items.

Suggested change
self.cart = {}
self.cart_items = {}
Reasons & Gaps

Reasons

  1. Enhancing variable names to be fully expressive improves long-term maintenance
  2. 'cart_items' explicitly identifies that the dictionary contains item data
  3. Reduces ambiguity when the codebase grows to include other cart-related data

Gaps

  1. 'cart' is a standard domain term for shopping applications
  2. The context of the class 'ShoppingCart' makes the purpose of 'cart' clear


def add_item(self, item, price):
if item in self.cart:
self.cart[item] += price
else:
self.cart[item] = price
return f"{item} added to cart"

def view_cart(self):
Comment on lines +9 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning Confidence Score: 75% View Citation

Boolean-Returning Function Without Prefix

The function view_cart returns a boolean-like state (the dictionary or a string message) but its name doesn't follow the boolean prefix convention. However, more importantly, it returns mixed types (String or Dict). If intended to check status, use a prefix like is_ or has_.

Suggested change
self.cart[item] = price
return f"{item} added to cart"
def view_cart(self):
def get_cart_contents(self):
Reasons & Gaps

Reasons

  1. Function names should clearly indicate if they return a state or data
  2. 'view_cart' is slightly vague regarding whether it displays or returns data
  3. Using 'get_' or 'is_' prefixes aligns with standard Python naming conventions

Gaps

  1. The function returns a dictionary (data) or a string, not strictly a boolean
  2. Standard 'view' verbs are common in UI-centric logic but vague in data processing

if not self.cart:
return "Cart is empty"
return self.cart

def get_total(self):
return sum(self.cart.values())

# ---- Testing ----
cart = ShoppingCart()

print(cart.add_item("Laptop", 50000))
print(cart.add_item("Mouse", 800))
print("Cart:", cart.view_cart())
print("Total Price:", cart.get_total())
Loading