Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring java #2

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
54768b5
Add refactoring approach in README
keent Aug 18, 2024
de2030e
#1 Refactor Readability - Apply formatter using redhat
keent Aug 18, 2024
73478bc
#7 Refactor Other - Type issue with HashMap
keent Aug 18, 2024
e0418d6
#1 #3 #4 Refactor to apply SRP and DIP (partial). Move out Hashmap as…
keent Aug 18, 2024
b46ef0e
#3 Refactor - Directory structure and organization of classes and com…
keent Aug 18, 2024
525e13d
#4 Extract Rental Calculator out of Rental Info
keent Aug 18, 2024
64c20e4
#4 Move statement generation to StatementGenerator to apply SRP
keent Aug 18, 2024
a561a2c
#1 Add MovieGenre to improve readability
keent Aug 20, 2024
c31c741
#1 #4 Use interface to allow for OCP
keent Aug 20, 2024
6347fe4
#1 Cleanup unused imports and incorrect references
keent Aug 20, 2024
f1236f7
#1 #3 Cleanup and Use DIP for inRentalInfo
keent Aug 20, 2024
121b09c
#1 Update types to enum
keent Aug 20, 2024
708fb5c
#1 Improve RentalInfo readability
keent Aug 20, 2024
d3a0604
#1 Make RentalStatementGenerator more readable
keent Aug 20, 2024
49f0406
#1 Make RentalCalculator more readable
keent Aug 20, 2024
650e882
#4 #5 Implement Strategy Pattern for Rental Calculator
keent Aug 20, 2024
eb8c756
#6 production grade eco - add google's checkstyle
keent Aug 20, 2024
a76a747
#6 Reorganize to prod structure
keent Aug 20, 2024
05b29b6
#6 Add junit test boilerplate and pom file
keent Aug 20, 2024
45d2707
#6 Add more test cases
keent Aug 20, 2024
d9e7573
# final commit - add further ideas
keent Aug 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"java.checkstyle.configuration": "${workspaceFolder}\\src\\checkstyle.xml",
"files.exclude": {
"**/*.class": true
},
"java.checkstyle.version": "10.17.0",
"files.watcherExclude": {
"**/*.class": true
}
}
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,44 @@ Reason how you have been thinking and the decisions you took.
You can hand in the result any way you feel (git patch, pull-request or ZIP-file).
Note: the Git history must be included.

## Solution
Below are the steps on my thought process, the reasoning behind it is
pretty standard for a refactoring process:
Make it work, Make it beautiful and organized, Then make it fast.

1. Understand the code
- It's a Movie rental statement generator for Customers.
Contains classes organized by Customer, Movie, MovieRental,
RentalInfo.
2. Understand and list the issues in the codebase.
- Unreadable
- Tests are not properly organized
- Classes are unorganized
- Not using SOLID principles
- Not using best practices
- Not using production grade ecosystem
- Other potential improvements: Code smells, Linting, Improve speed/performance

3. Iterate on fixing the issues so that the codebase will be
production grade.


## More refactoring ideas

- Better robust error handling using exceptions
- More comprehensive test cases
- Functional Programming idioms (not necessary)
- Spring boot


## To run mvn test
```
mvn test
```
## To run the test:

```
javac src/*.java
java -cp src Main
cd src
javac main/java/customer/*.java main/java/movie/*.java main/java/rental/*.java Main.java
java Main
```
37 changes: 37 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>refactoring-java</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
<junit.version>5.7.0</junit.version>
</properties>

<dependencies>
<!-- JUnit 5 dependency -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Maven Surefire Plugin for running tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>

21 changes: 13 additions & 8 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import main.java.movie.InMemoryMovieRepository;
import main.java.movie.MovieRental;
import main.java.rental.RentalCalculator;
import main.java.rental.RentalStatementGenerator;
import main.java.customer.Customer;
import java.util.Arrays;

public class Main {

public static void main(String[] args) {
String expected = "Rental Record for C. U. Stomer\n\tYou've Got Mail\t3.5\n\tMatrix\t2.0\nAmount owed is 5.5\nYou earned 2 frequent points\n";
InMemoryMovieRepository movieRepository = new InMemoryMovieRepository();
RentalCalculator rentalCalculator = new RentalCalculator();
RentalStatementGenerator statementGenerator = new RentalStatementGenerator(movieRepository, rentalCalculator);

String result = new RentalInfo().statement(new Customer("C. U. Stomer", Arrays.asList(new MovieRental("F001", 3), new MovieRental("F002", 1))));
String result = statementGenerator
.generateStatement(
new Customer("C. U. Stomer", Arrays.asList(new MovieRental("F001", 3), new MovieRental("F002", 1))));

if (!result.equals(expected)) {
throw new AssertionError("Expected: " + System.lineSeparator() + String.format(expected) + System.lineSeparator() + System.lineSeparator() + "Got: " + System.lineSeparator() + result);
}

System.out.println("Success");
System.out.println(result);
}
}
}
50 changes: 0 additions & 50 deletions src/RentalInfo.java

This file was deleted.

Loading