Skip to content
Open
Show file tree
Hide file tree
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
137 changes: 34 additions & 103 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,118 +1,49 @@
# Object Oriented Programming (OOP) Part 2 - Cash Register Lab
# Cash Register - OOP Lab (Part 2)

Now that we’ve discussed more about object oriented design philosophies and techniques like decorators we will be looking at building more complex objects. In this case we will be building a cash register object to simulate different functions of a cash register for an e-commerce site.
## Description

## Tools & Resources
* [GitHub Repo](https://github.com/learn-co-curriculum/oop-p2-cash-register-lab)
* [Python Classes](https://docs.python.org/3/tutorial/classes.html)
This project models real-world behavior of a register system used in e-commerce: adding items, applying discounts, and voiding transactions.

## Instructions
---

### Set Up
## Table of Contents

Before we begin coding, let's complete the initial setup for this lesson:
* Fork and Clone: For this lesson, you will need the following GitHub Repo:
* Go to the provided GitHub repository link.
* Fork the repository to your GitHub account.
* Clone the forked repository to your local machine.
* Open and Run File
* Open the project in VSCode.
* Run npm install to install all necessary dependencies.
- [Demo](#demo)
- [Setup](#setup)
- [Usage](#usage)
- [Testing](#testing)
- [Features](#features)

### Task 1: Define the Problem
---

Build a model for a cash register
* Build a cash register object
* Add items
* Apply discounts
* Void previous transactions
## Demo

### Task 2: Determine the Design
![demo](demo.png)

Cash Register
* Attributes
* discount
* total
* items
* previous_transactions
* Methods
* add_item(item, price, quantity)
* apply_discount()
* void_last_transaction()
---

### Task 3: Develop, Test, and Refine the Code
## Setup

#### Step 1: Git Feature Branch
1. Fork and clone the repo
2. Create a virtual environment: `python3 -m venv .venv`
3. Activate virtual environment:
- Mac/Linux: `source .venv/bin/activate`
- Windows ` .venv\Scripts\activate`

* Create a feature branch for your work using git.
---

#### Step 2: Create a CashRegister class
## Testing
- Install pytest: `pip install pytest`
- Run the provided test file: `pytest`

* ```__init__```:
* discount
* Allow for user to input
* If no input initialize as 0
* Note that discount is a percentage off of the total cash register price (e.g. a discount of 20 means the customer receives 20% off of their total price)
* ```total```
* Initialize as 0
* ```items```
* Initialize as empty array
* ```previous_transactions```
* Initialize as empty array
---

#### Step 3: Properties

* Discount:
* Ensure discount is an integer
* Ensure that discount is between 0-100 inclusive
* If not print “Not valid discount”

#### Step 4: Methods

* add_item(item, price, quantity)
* Add price to total
* Add item to the items array
* Add an object to the previous transactions with the item, price and quantity.
* apply_discount()
* Apply discount as percentage off from total
* Remove the last item of previous_transaction from array
* Ensure price reflects correctly
* Ensure items reflects correctly
* If no transactions in array print “There is no discount to apply.”void_last_transaction()

#### Step 5: Push feature branch and open a PR on GitHub

* Save, commit, and push your code to GitHub.
* Open a PR on the main branch of your own repo (be sure not to open a PR on the learn-co-curriculum repo).

#### Step 6: Merge to main

* Review the PR and merge your finished code into the main branch.

### Task 4: Document and Maintain

Best Practice documentation steps:

* Add comments to code to explain purpose and logic
* Clarify intent / functionality of code to other developers
* Add screenshot of completed work included in Markdown in README.
* Update README text to reflect the functionality of the application following https://makeareadme.com.
* Delete any stale branches on GitHub
* Remove unnecessary/commented out code
* If needed, update git ignore to remove sensitive data

## Save your work and push to GitHub

Before you submit your solution, you need to save your progress with git.
1. Add your changes to the staging area by executing git add .
2. Create a commit by executing git commit -m "Your commit message"
3. Push your commits to GitHub by executing git push origin main or git push origin master , depending on the name of your branch (use git branch to check on which branch you are).

## Submission and Grading Criteria

1. Use the rubric in Canvas as a guide for how this lab is graded.
2. Your submission will be automatically scored in CodeGrade, using the most recent commit. Remember to make sure you have pushed your commit to GitHub before submitting your assignment.
3. You can review your submission in CodeGrade and see your final score in your Canvas gradebook.
4. When you are ready to submit, click the ***Load Lab: Object Oriented Programming (OOP)- Part 2- Cash Register*** button in Canvas to launch CodeGrade.
* Click on + Create Submission. Connect your repository for this lab.
* For additional information on submitting assignments in CodeGrade: [Getting Started in Canvas](https://help.codegrade.com/for-students/getting-started/getting-started-in-canvas).
## Features
• add_item(item, price, quantity=1)
Adds one or multiple items to the register and updates the total.
• apply_discount()
Applies a percentage discount to the total price.
• void_last_transaction()
Removes the last transaction from the total and item list.
• @property and setter for discount
Ensures only valid discount values (integers between 0–100) are accepted.
Binary file added demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 53 additions & 1 deletion lib/cash_register.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@
#!/usr/bin/env python3

class CashRegister:
pass
def __init__ (self, discount=0):
self._discount = discount
self.total = 0
self.items = []
self.previous_transactions = []

@property
def discount(self):
return self._discount
@discount.setter
def discount(self, value):
#checking that discount is an integer and a number from 0-100 (inclusive)
if isinstance (value, int) and 0 <= value <= 100:
self._discount = value
else: print("Not valid discount")

def add_item(self, item, price, quantity=1):
if quantity > 0:
#adding item in items list as many times as the quality specifies
for _ in range(quantity):
self.items.append(item)
#adding to total
self.total += price*quantity
#adding transaction on previous_transactions list
self.previous_transactions.append({ "item" : item,
"price" : price,
"quantity": quantity
})

def apply_discount(self):
if self._discount > 0:
#subtracting discount percentage from total
self.total -= self.total*self._discount/100
print(f"After the discount, the total comes to ${int(self.total)}.")
else:
print("There is no discount to apply.")

def void_last_transaction(self):
#if there are no previous transaction, set total to 0
if len(self.previous_transactions) == 0:
self.total = 0
else:
# removing the last transaction's total from total
self.total -= self.previous_transactions[-1]["price"]*self.previous_transactions[-1]["quantity"]

#removing the last transaction's items from items list
for _ in range(self.previous_transactions[-1]["quantity"]):
self.items.pop()

#removing the last transaction
self.previous_transactions.pop()