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.
- 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
- Java 8 or higher
- Maven 3.6 or higher
-
Clone the repository
git clone https://github.com/yourusername/java-calculator-engine.git cd java-calculator-engine -
Compile the project
mvn compile
-
Run the application
mvn exec:java -Dexec.mainClass="com.calculator.Main"
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 = 37result = (5 + 3) * 2 // result = 16
calc = 10 + (2 * 3) // calc = 16
nested = ((2 + 3) * 4) - 1 // nested = 19a = 5
b = ++a // a = 6, b = 6
c = a++ // a = 7, c = 6
d = --a // a = 6, d = 6
e = a-- // a = 5, e = 6src/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
- 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
The project includes comprehensive end-to-end tests covering all success and failure scenarios.
mvn test- 45 End-to-End Tests covering:
- ✅ All mathematical operations
- ✅ Variable assignments and manipulations
- ✅ Increment/decrement operations
- ✅ Operator precedence
- ✅ Parentheses handling
- ✅ Error cases and validation
variable = valuevariable = expressionvariable += expressionvariable -= expressionvariable *= expressionvariable /= expression++variable(pre-increment)variable++(post-increment)--variable(pre-decrement)variable--(post-decrement)
- Addition:
+ - Subtraction:
- - Multiplication:
* - Division:
/ - Parentheses:
()
- Parentheses:
() - Multiplication/Division:
*,/ - Addition/Subtraction:
+,-
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
# Compile
mvn compile
# Run tests
mvn test
# Package
mvn package
# Clean
mvn clean- 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
This project is licensed under the MIT License - see the LICENSE file for details.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
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
- 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