-
Notifications
You must be signed in to change notification settings - Fork 0
Add ShoppingCart class with item management and total calculation #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||||||||
| class ShoppingCart: | ||||||||||||
| def __init__(self): | ||||||||||||
| self.cart = {} | ||||||||||||
|
|
||||||||||||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Boolean-Returning Function Without Prefix The function
Suggested change
Reasons & GapsReasons
Gaps
|
||||||||||||
| 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()) | ||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JAS - Just a suggestion
Vague but Functional Generic Name
The variable name
cartis functional but could be more expressive to indicate it stores items and their associated prices, such asitems_to_price_maporcart_items.Reasons & Gaps
Reasons
Gaps