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.
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.
Build a model for a cash register
- Build a cash register object
- Add items
- Apply discounts
- Void previous transactions
Cash Register
- Attributes
- discount
- total
- items
- previous_transactions
- Methods
- add_item(item, price, quantity)
- apply_discount()
- void_last_transaction()
- Create a feature branch for your work using git.
__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
- Discount:
- Ensure discount is an integer
- Ensure that discount is between 0-100 inclusive
- If not print “Not valid discount”
- 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()
- Remove the last item of previous_transaction from the array.
- Ensure the price reflects correctly.
- Ensure items reflect correctly.
- If no transactions are in the array, print “There is no transaction to void.”
- Remove the last item of previous_transaction from the array.
- 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).
- Review the PR and merge your finished code into the main branch.
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
Before you submit your solution, you need to save your progress with git.
- Add your changes to the staging area by executing git add .
- Create a commit by executing git commit -m "Your commit message"
- 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).
- Use the rubric in Canvas as a guide for how this lab is graded.
- 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.
- You can review your submission in CodeGrade and see your final score in your Canvas gradebook.
- 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.