A modern, responsive calculator web application built with Spring Boot and Spring MVC architecture. This multi-user calculator supports basic arithmetic operations and features a beautiful, intuitive user interface.
- โ Addition: Add two numbers with precision
- โ Subtraction: Subtract numbers accurately
- โ๏ธ Multiplication: Multiply numbers with decimal support
- โ Division: Divide numbers with zero-division protection
- ๐ All Clear (AC): Reset calculator to initial state
- โซ Backspace: Delete last entered character
- ๐ฏ Modern UI: Beautiful gradient design with glassmorphism effects
- ๐ฑ Responsive: Works perfectly on desktop, tablet, and mobile devices
- โจ๏ธ Keyboard Support: Full keyboard navigation and shortcuts
- โก Real-time Calculations: Instant feedback and calculations
- ๐จ Error Handling: User-friendly error messages and validations
- ๐ฅ Multi-User Support: Designed for concurrent user access
- ๐ REST API: Clean, documented API endpoints
- โ Comprehensive Testing: Full unit and integration test coverage
- ๐ Health Monitoring: Application health check endpoints
- ๐ก๏ธ CORS Support: Cross-origin resource sharing enabled
This application implements the Spring MVC (Model-View-Controller) architectural pattern:
โโโโโโโโโโโโโโโโโโโโโโโ
โ Presentation โ โ Static HTML/CSS/JS (View)
โโโโโโโโโโโโโโโโโโโโโโโค
โ Controller โ โ REST API Endpoints
โโโโโโโโโโโโโโโโโโโโโโโค
โ Service โ โ Business Logic (Model)
โโโโโโโโโโโโโโโโโโโโโโโค
โ Data Transfer โ โ DTOs for API Communication
โโโโโโโโโโโโโโโโโโโโโโโ
- ๐ฎ Controller Layer:
CalculatorController
- Handles HTTP requests and responses - โ๏ธ Service Layer:
CalculatorService
- Contains business logic for calculations - ๐ฆ DTO Layer:
CalculatorRequest
/CalculatorResponse
- Data transfer objects - ๐ผ๏ธ View Layer: Static HTML/CSS/JavaScript for user interface
- โ Java 17+ (JDK 17 or higher)
- ๐ฆ Maven 3.6+
- ๐ฅ๏ธ IDE (IntelliJ IDEA, Eclipse, or VS Code recommended)
-
Clone the repository
git clone https://github.com/AnderssonProgramming/calculator-mvc.git cd calculator-mvc
-
Build the project
mvn clean compile
-
Run tests (optional but recommended)
mvn test
-
Start the application
mvn spring-boot:run
-
Access the calculator
- Open your browser and navigate to: http://localhost:8081
- Start calculating! ๐
Using Java directly:
mvn clean package
java -jar target/calculator-mvc-0.0.1-SNAPSHOT.jar
Using your IDE:
- Import the project as a Maven project
- Run
CalculatorMvcApplication.java
as a Java application
- URL:
POST /calculate
- Description: Performs arithmetic calculations
- Content-Type:
application/json
Request Body:
{
"operand1": 10.5,
"operand2": 5.2,
"operation": "add"
}
Supported Operations:
add
or+
- Additionsubtract
or-
- Subtractionmultiply
or*
- Multiplicationdivide
or/
- Divisionclear
orac
- All Clear
Success Response:
{
"result": 15.7,
"operation": "add",
"success": true,
"errorMessage": null
}
Error Response:
{
"result": 0,
"operation": null,
"success": false,
"errorMessage": "Cannot divide by zero"
}
- URL:
GET /health
- Description: Check if the calculator service is running
- Response:
"Calculator service is running"
Using cURL:
# Addition
curl -X POST http://localhost:8081/api/calculator/calculate \
-H "Content-Type: application/json" \
-d '{"operand1": 15, "operand2": 25, "operation": "add"}'
# Division with error handling
curl -X POST http://localhost:8081/api/calculator/calculate \
-H "Content-Type: application/json" \
-d '{"operand1": 10, "operand2": 0, "operation": "divide"}'
# Health check
curl http://localhost:8081/api/calculator/health
# Run all tests
mvn test
# Run specific test class
mvn test -Dtest=CalculatorServiceTest
# Run tests with coverage report
mvn clean test jacoco:report
- Service Layer: 100% method coverage
- Controller Layer: Complete API endpoint testing
- Integration Tests: Full application context testing
- Unit Tests: Individual component testing
- Integration Tests: Spring context and HTTP request testing
- Error Handling: Edge cases and exception scenarios
calculator-mvc/
โโโ ๐ pom.xml # Maven configuration
โโโ ๐ README.md # This file
โโโ ๐ src/
โ โโโ ๐ main/
โ โ โโโ ๐ java/edu/eci/arsw/calculator_mvc/
โ โ โ โโโ ๐ CalculatorMvcApplication.java # Main application class
โ โ โ โโโ ๐ controller/
โ โ โ โ โโโ ๐ CalculatorController.java # REST API controller
โ โ โ โโโ ๐ dto/
โ โ โ โ โโโ ๐ CalculatorRequest.java # Request DTO
โ โ โ โ โโโ ๐ CalculatorResponse.java # Response DTO
โ โ โ โโโ ๐ service/
โ โ โ โโโ ๐ CalculatorService.java # Business logic
โ โ โโโ ๐ resources/
โ โ โโโ ๐ application.properties # App configuration
โ โ โโโ ๐ static/
โ โ โโโ ๐ index.html # Main UI
โ โ โโโ ๐ js/
โ โ โโโ ๐ calculator.js # Frontend logic
โ โโโ ๐ test/
โ โโโ ๐ java/edu/eci/arsw/calculator_mvc/
โ โโโ ๐ CalculatorMvcApplicationTests.java
โ โโโ ๐ controller/
โ โ โโโ ๐ CalculatorControllerTest.java
โ โโโ ๐ service/
โ โโโ ๐ CalculatorServiceTest.java
# Application settings
spring.application.name=calculator-mvc
server.port=8081
# Error handling
server.error.include-message=always
server.error.include-binding-errors=always
# Logging
logging.level.edu.eci.arsw.calculator_mvc=DEBUG
logging.level.org.springframework.web=INFO
- Port: Modify
server.port
inapplication.properties
- Logging: Adjust logging levels for different packages
- CORS: Configure allowed origins in
CalculatorController
- Glassmorphism Design: Modern frosted glass effect
- Responsive Layout: Adapts to different screen sizes
- Smooth Animations: Button hover effects and transitions
- Color-Coded Buttons: Different colors for numbers, operators, and functions
- Real-time Display: Shows both expression and result
- Numbers:
0-9
keys - Operations:
+
,-
,*
,/
- Calculate:
Enter
or=
- Clear:
Escape
key - Delete:
Backspace
key - Decimal:
.
key
1. Port Already in Use
Error: Web server failed to start. Port 8081 was already in use.
Solution: Change the port in application.properties
or stop the process using port 8081.
2. Maven Build Issues
Error: Could not find or load main class
Solution: Run mvn clean compile
and ensure Java 17+ is installed.
3. Application Won't Start
Error: APPLICATION FAILED TO START
Solution: Check logs for specific errors, verify Java version and dependencies.
Run with debug logging:
mvn spring-boot:run -Dspring-boot.run.profiles=debug
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
- Follow Spring Boot best practices
- Maintain test coverage above 90%
- Use meaningful commit messages
- Update documentation for new features
This project is licensed under the MIT License - see the LICENSE file for details.
- Spring Framework Team - For the excellent Spring Boot framework
- Font Awesome - For the beautiful icons
- Stack Overflow Community - For countless solutions and inspiration
- Scientific Calculator: Add trigonometric and logarithmic functions
- History Feature: Store and display calculation history
- Memory Functions: M+, M-, MR, MC operations
- Themes: Multiple UI themes and dark mode
- Keyboard Layout: Virtual keyboard display
- Unit Converter: Length, weight, temperature conversions
- Export Results: Save calculations to file
- User Profiles: Personal calculation preferences
โญ If you found this project helpful, please give it a star! โญ
Happy Calculating! ๐งฎโจ