Skip to content

haokicode/b_oop

Repository files navigation

OOP Calculator - A Flutter Project

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.

✨ Core Concepts Demonstrated

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 an execute method and a symbol, without specifying how it's done.

  • Inheritance & Implementation (Addition, Subtraction, etc.): Each specific operation (like Addition or Division) implements the Operation contract, providing a concrete version of the execute method. This promotes code reuse and organization.

  • Encapsulation (class Calculator): The main Calculator class 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 Calculator class holds a variable of type Operation. It can hold an Addition object, a Subtraction object, or any other that follows the contract. When execute() is called, Dart's runtime polymorphism ensures the correct operation is performed without the Calculator needing to know the specific details.

✅ Features

  • 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 20 for whole numbers and 2.5 for decimals.

🧪 Comprehensive Unit Testing

The project includes a robust suite of unit tests located in test/calculator_test.dart, which verifies:

  • The correctness of each individual Operation class.
  • All core logic within the Calculator "brain."
  • Critical edge cases like division by zero, chained operations, and initial state.

🚀 Getting Started

Prerequisites

Installation & Running

  1. Clone the repository:

  2. Install dependencies:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published