This Flutter project implements a functional calculator built with a strong emphasis on Object-Oriented Programming (OOP) principles. It serves as a clear and practical example of how to structure an application's logic using classes, inheritance, and polymorphism to create clean, maintainable, and extensible code.
This project is more than just a calculator; it's a case study in applying fundamental OOP concepts in a real Flutter app.
-
Abstraction (
abstract class Operation): Defines a "contract" for all mathematical operations. It guarantees that every operation will have anexecutemethod and asymbol, without specifying how it's done. -
Inheritance & Implementation (
Addition,Subtraction, etc.): Each specific operation (likeAdditionorDivision) implements theOperationcontract, providing a concrete version of theexecutemethod. This promotes code reuse and organization. -
Encapsulation (
class Calculator): The mainCalculatorclass acts as the "brain." It hides its internal state (like the current operands and output) and exposes a clean public API (addDigit,setOperation,calculate,clear) for the UI to interact with. -
Polymorphism: The
Calculatorclass holds a variable of typeOperation. It can hold anAdditionobject, aSubtractionobject, or any other that follows the contract. Whenexecute()is called, Dart's runtime polymorphism ensures the correct operation is performed without theCalculatorneeding to know the specific details.
- Four Basic Operations: Addition, Subtraction, Multiplication, and Division.
- Clean, Responsive UI: The user interface fits various screen sizes without overflow errors.
- Error Handling: Gracefully handles division by zero by displaying an "Error" message.
- Chained Calculations: The result of one calculation automatically becomes the first operand for the next.
- Integer & Decimal Display: Correctly formats results, showing
20for whole numbers and2.5for decimals.
The project includes a robust suite of unit tests located in test/calculator_test.dart, which verifies:
- The correctness of each individual
Operationclass. - All core logic within the
Calculator"brain." - Critical edge cases like division by zero, chained operations, and initial state.
- You must have the Flutter SDK installed.
-
Clone the repository:
-
Install dependencies: