Skip to content

YuriiJavaDev/JavaBasics_Task_407_V0.2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Calculator: Clean Code Refactoring (JavaBasics_Task_407_V0.2)

📖 Description

In software engineering, keeping all logic in a single class creates "God Objects" that are hard to maintain. This project demonstrates the Refactoring of a monolithic Calculator into a modular architecture based on the Single Responsibility Principle (SRP). We have decoupled the system into three distinct roles:

  • CalculatorData: An immutable record for holding the state.
  • CalculationService: A dedicated class for mathematical operations.
  • ResultPrinter: A specialized component for UI/Console output.

    This modular approach ensures that changing the calculation logic won't affect how results are printed, making the system extensible and testable.

📋 Requirements Compliance

  • Separation of Concerns: Each class has exactly one reason to change.
  • Immutability: Used a Java Record for data transfer to ensure results are not accidentally modified.
  • Encapsulation: Hidden calculation details within the service layer.
  • Clean Architecture: Followed the package-by-feature structure for better organization.

🚀 Architectural Stack

  • Java 16+ (Records, OOP Design Patterns)

🏗️ Implementation Details

  • CalculatorData: Data carrier.
  • CalculationService: Logic provider.
  • ResultPrinter: Output handler.

📋 Expected result

The final result of calculation is: 55

💻 Code Example

Project Structure:

JavaBasics_Task_407_V0.2/
├── src/
│   └── com/yurii/pavlenko/
│                 ├── app/
│                 │   └── CalculatorApp.java
│                 └── math/
│                     ├── models/
│                     │   └── CalculatorData.java
│                     ├── services/
│                     │   └── CalculationService.java
│                     └── ui/
│                         └── ResultPrinter.java
├── LICENSE
├── TASK.md
├── THEORY.md
└── README.md

Code

package com.yurii.pavlenko.app;

import com.yurii.pavlenko.math.models.CalculatorData;
import com.yurii.pavlenko.math.services.CalculationService;
import com.yurii.pavlenko.math.ui.ResultPrinter;

public class CalculatorApp {

  public static void main(String[] args) {
    CalculationService service = new CalculationService();
    ResultPrinter printer = new ResultPrinter();

    CalculatorData data = service.calculate(10, 5);

    printer.print(data);
  }
}
package com.yurii.pavlenko.math.models;

public record CalculatorData(int result) {
}
package com.yurii.pavlenko.math.services;

import com.yurii.pavlenko.math.models.CalculatorData;

public class CalculationService {

    public CalculatorData calculate(int a, int b) {
        // Calculation: a * b + (a - b)
        int calculatedValue = a * b + (a - b);
        return new CalculatorData(calculatedValue);
    }
}
package com.yurii.pavlenko.math.ui;

import com.yurii.pavlenko.math.models.CalculatorData;

public class ResultPrinter {

    public void print(CalculatorData data) {
        System.out.println("The final result of calculation is: " + data.result());
    }
}

⚖️ License

This project is licensed under the MIT License.

Copyright (c) 2026 Yurii Pavlenko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...

License: MIT

About

This is a tutorial project. JavaBasics_Task_407_V0.2 Calculator Refactoring: applying the Single Responsibility Principle by decoupling calculation logic, data storage, and output formatting into separate classes. 120526_1353

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages