Giter VIP home page Giter VIP logo

python-p3-oo-cash-register-lab's Introduction

Cash Register Lab

Learning Goals

  • Build a class with instance methods.
  • Call instance methods inside of other instance methods.
  • Use instance methods to track information pertinent to an instance of a class.

Key Vocab

  • Class: a bundle of data and functionality. Can be copied and modified to accomplish a wide variety of programming tasks.
  • Initialize: create a working copy of a class using its __init__ method.
  • Instance: one specific working copy of a class. It is created when a class's __init__ method is called.
  • Object: the more common name for an instance. The two can usually be used interchangeably.
  • Object-Oriented Programming: programming that is oriented around data (made mobile and changeable in objects) rather than functionality. Python is an object-oriented programming language.
  • Function: a series of steps that create, transform, and move data.
  • Method: a function that is defined inside of a class.
  • Magic Method: a special type of method in Python that starts and ends with double underscores. These methods are called on objects under certain conditions without needing to use their names explicitly. Also called dunder methods (for double underscore).
  • Attribute: variables that belong to an object.
  • Property: attributes that are controlled by methods.

Introduction

We're going to create an Object-Oriented Cash Register that can:

  • Add items of varying quantities and prices
  • Calculate discounts
  • Keep track of what's been added to it
  • Void the last transaction

Instructions

This is a test-driven lab! You will need to read the spec file and the test output very carefully to solve this one.

Note that a discount is calculated as 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).

Hint #1: Keep in mind that to access an attribute or call an instance method inside another instance method, we use the self keyword to refer to the instance on which we are operating. For example:

class Person:

  def __init__(self, age=0):
    self.age = age

  def birthday(self):
    self.age += 1

Follow along with the tests in lib/testing/cash_register_test.py. Reading along with what the tests are looking for can be really helpful!

Take it one step at a time!

Hint #2: The apply_discount() method requires some knowledge about working with integers versus floats in Python. When you get to that method, take a look at what return value the tests are expecting and keep in mind that Python provides methods for changing an Integer to a Float and vice versa.

Hint #3: The void_last_transaction() method will remove the last transaction from the total. You'll need to make an additional attribute and keep track of that last transaction amount somehow. In what method of the class are you working with an individual item?

Hint #4: Python handles mutable default values for arguments differently than it handles immutable default values. This means that you should usually not set default values for lists, dictionaries, and instances of classes. You can learn more on this quirk in Python's documentation on More Control Flow Tools .

python-p3-oo-cash-register-lab's People

Contributors

professor-ben avatar lizbur10 avatar jlboba avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.