Overview
The Currency Converter application follows the Model-View-Controller (MVC) architectural pattern, implementing object-oriented programming (OOP) principles to ensure clean separation of concerns and maintainable code.MVC Pattern Implementation
The application is structured into three distinct layers, each with specific responsibilities:Controller Layer (Controladores)
Package:lad.com.alura.conversormoneda.controladores
The controller layer orchestrates application flow and business logic.
- Application entry point (
mainmethod) - User input handling and validation
- Orchestrating interactions between View and Model
- Managing conversion history
- Business logic for currency pair selection
Model Layer (Modelos)
Package:lad.com.alura.conversormoneda.modelos
The model layer contains business entities and service classes.
ConsultaMoneda (Service)
Handles external API communication and data retrieval.RegistroConversion (Entity)
Immutable data structure using Java 14+record feature.
View Layer (Vista)
Package:lad.com.alura.conversormoneda.vista
The view layer handles user interface and presentation logic.
- User interface presentation
- Menu display
- User interaction prompts
Package Structure
Class Relationships
- Class Diagram
- Sequence Diagram
Design Decisions
Object-Oriented Programming (OOP) Principles
Encapsulation
Service Encapsulation
The
ConsultaMoneda class encapsulates all API communication logic, hiding implementation details from the controller.Single Responsibility Principle (SRP)
Each class has a single, well-defined responsibility:- ConversorApp: Application orchestration and flow control
- ConsultaMoneda: External API communication
- RegistroConversion: Data representation
- Conversor: User interface presentation
Immutability
Java Records
The
RegistroConversion record provides immutable data structures, ensuring thread safety and preventing accidental modifications.Modern Java Features
The application leverages several modern Java features:Records (Java 14+)
Immutable data carriers with automatic constructors, getters,
equals(), hashCode(), and toString()Text Blocks (Java 13+)
Multi-line string literals for cleaner menu formatting
Switch Expressions (Java 14+)
Enhanced switch with
yield for cleaner conditional logicLambda Expressions (Java 8+)
Functional programming for history display:
historial.forEach(System.out::println)Data Flow
- User Input: User selects currency pair and enters amount
- Controller Processing:
ConversorAppprocesses the selection using switch expressions - Service Call: Controller invokes
ConsultaMoneda.obtenerTasa() - API Request: Service makes HTTP request to ExchangeRate-API
- Response Parsing: JSON response parsed using Gson
- History Recording: Conversion saved as
RegistroConversionwith timestamp - Result Display: Converted amount displayed to user
The application maintains in-memory history using
ArrayList<RegistroConversion>, which persists only during the application session.Error Handling Strategy
The application implements graceful error handling:- API failures return
0with error message - Invalid menu selections prompt retry
- Exception catching prevents application crashes