/Strategy_Pattern_Calculator

Python implementation of a command line calculator using strategy design pattern

Primary LanguagePythonMIT LicenseMIT

Python application test with Github Actions Upload Python Package

Strategy Pattern Calculator

Python implementation of a command line calculator using the strategy design pattern

Description

This is a simple command-line calculator that can perform basic arithmetic operations. The calculator is implemented using the strategy design pattern. The calculator can perform the following operations:

  • Addition
  • Subtraction
  • Multiplication
  • Division

Setup tool is configured to build and use as a CLI.

Further, Business logic is exposed as a pip package spcalc

To be implemented:

  • Exponentiation
  • Square root
  • Logarithm

Getting Started

Installing

  • Clone the repository
git clone https://github.com/yrangana/Strategy_Pattern_Calculator.git
  • Install the dependencies
make install

Executing program in Python

  • Run the calculator with prompts
python src/main.py
  • Run the calculator with arguments
python src/main.py -n1 <first number> -n2 <second number> -op <operation>
  • Run the tests
make test

Executing program in CLI

  • Setup as a Command Line Tool
python -m pip install --editable .
  • Run the calculator with prompts
spcalc
  • Run the calculator with arguments
spcalc -n1 <first number> -n2 <second number> -op <operation>

Help

Usage: main.py [OPTIONS]

Options:
  -n1, --num1 FLOAT               First number
  -n2, --num2 FLOAT               Second number
  -op, --operation [add|subtract|multiply|divide]
                                  Operation to perform
  --help                          Show this message and exit.

Use as a pip package

  • install pip package
pip install spcalc
  • Example usecae
from spcalc import calculator, add, subtract, multiply, divide

mycalc = calculator.Calculator(3,5,add.Add()) #subtract.Subtract(), multiply.Multiply(), divide.Divide()

print(mycalc.execute())

Authors

Yasiru Rangana

Version History

License

This project is licensed under the MIT License - see the License file for details