Skip to content

regev123/java-calculator-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧮 Calculator Project

A sophisticated Java-based calculator application that supports complex mathematical expressions, variable assignments, and increment/decrement operations. Built using clean architecture principles and design patterns.

✨ Features

  • Mathematical Operations: Addition, subtraction, multiplication, division
  • Variable Management: Declare and manipulate variables
  • Increment/Decrement: Pre and post increment (++i, i++) and decrement (--i, i--)
  • Compound Assignments: +=, -=, *=, /=
  • Operator Precedence: Proper handling of mathematical operator precedence
  • Parentheses Support: Nested parentheses for complex expressions
  • Syntax Validation: Comprehensive expression validation
  • Clean Architecture: SOLID principles and design patterns

🚀 Quick Start

Prerequisites

  • Java 8 or higher
  • Maven 3.6 or higher

Installation

  1. Clone the repository

    git clone https://github.com/yourusername/java-calculator-engine.git
    cd java-calculator-engine
  2. Compile the project

    mvn compile
  3. Run the application

    mvn exec:java -Dexec.mainClass="com.calculator.Main"

📖 Usage Examples

Basic Operations

i = 0          // i = 0
j = ++i        // i = 1, j = 1
x = i++ + 5    // i = 2, x = 6
y = 5 + 3 * 10 // y = 35
i += y         // i = 37

Complex Expressions

result = (5 + 3) * 2        // result = 16
calc = 10 + (2 * 3)         // calc = 16
nested = ((2 + 3) * 4) - 1  // nested = 19

Increment/Decrement Operations

a = 5
b = ++a    // a = 6, b = 6
c = a++    // a = 7, c = 6
d = --a    // a = 6, d = 6
e = a--    // a = 5, e = 6

🏗️ Architecture

Package Structure

src/main/java/com/calculator/
├── Main.java                    # Application entry point
├── core/                        # Core business logic
│   ├── Calculator.java          # Main calculator orchestrator
│   └── ExpressionEvaluator.java # Mathematical expression evaluator
├── interfaces/                  # Contract definitions
│   ├── ExpressionEvaluatorInterface.java
│   ├── SyntaxValidatorInterface.java
│   └── TokenProcessorInterface.java
├── strategies/                  # Strategy pattern implementations
│   ├── assignment/              # Assignment strategies
│   ├── operation/               # Mathematical operation strategies
│   └── variable/                # Variable processing strategies
├── factories/                   # Factory pattern implementations
│   ├── CalculatorFactory.java
│   ├── OperationFactory.java
│   ├── AssignmentStrategyFactory.java
│   └── VariableProcessorFactory.java
├── validation/                  # Input validation
│   ├── ExpressionSyntaxValidator.java
│   └── TokenValidator.java
└── utils/                       # Utility classes
    ├── Constants.java
    ├── ExpressionUtils.java
    └── IncrementDecrementHandler.java

Design Patterns Used

  • Strategy Pattern: For different types of operations and assignments
  • Factory Pattern: For creating calculator components
  • Dependency Injection: For loose coupling between components
  • Single Responsibility: Each class has one clear purpose
  • Open/Closed Principle: Easy to extend with new operations

🧪 Testing

The project includes comprehensive end-to-end tests covering all success and failure scenarios.

Run Tests

mvn test

Test Coverage

  • 45 End-to-End Tests covering:
    • ✅ All mathematical operations
    • ✅ Variable assignments and manipulations
    • ✅ Increment/decrement operations
    • ✅ Operator precedence
    • ✅ Parentheses handling
    • ✅ Error cases and validation

📋 Supported Expressions

Valid Expressions

  • variable = value
  • variable = expression
  • variable += expression
  • variable -= expression
  • variable *= expression
  • variable /= expression
  • ++variable (pre-increment)
  • variable++ (post-increment)
  • --variable (pre-decrement)
  • variable-- (post-decrement)

Mathematical Operations

  • Addition: +
  • Subtraction: -
  • Multiplication: *
  • Division: /
  • Parentheses: ()

Operator Precedence

  1. Parentheses: ()
  2. Multiplication/Division: *, /
  3. Addition/Subtraction: +, -

🚫 Error Handling

The calculator provides comprehensive error handling for:

  • Syntax Errors: Invalid expressions, unbalanced parentheses
  • Semantic Errors: Undeclared variables, division by zero
  • Validation Errors: Invalid variable names, reserved keywords

🔧 Development

Building from Source

# Compile
mvn compile

# Run tests
mvn test

# Package
mvn package

# Clean
mvn clean

Code Quality

  • Clean Code Principles: Readable, maintainable, and well-documented
  • SOLID Principles: Single responsibility, open/closed, dependency inversion
  • Design Patterns: Strategy, Factory, Dependency Injection
  • Comprehensive Testing: End-to-end integration tests

📝 License

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

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📞 Support

If you have any questions or need help, please:

  • Open an issue on GitHub
  • Check the existing issues for solutions
  • Review the test cases for usage examples

🎯 Future Enhancements

  • Support for floating-point numbers
  • Additional mathematical functions (sin, cos, sqrt)
  • Command-line interface improvements
  • GUI implementation
  • Performance optimizations

Made with ❤️ using Java and Clean Architecture principles

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages