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

Solutionn with: two for loops, function which check the date and can throw ParseExcepion if something isn't right, StringBuilder contained solution and dataFormat. #1320

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

B4rt1omiej
Copy link

No description provided.

…throw ParseExcepion if something isn't right, StringBuilder contained solution and dataFormat.
Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there! 👋 It looks like there are a few important improvements to be made regarding date handling, formatting, and code style. While these might not be breaking changes, they are essential for ensuring your code is up-to-date with modern Java practices and is as clean and efficient as possible. Let's tidy up those date formats and keep the code style sharp! 😊👍

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

Comment on lines 3 to 5
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use LocalDate instead of Date. The Date class is outdated and has been replaced with the more modern and robust java.time (JSR-310) package in Java 8. Here is some background why you should use it primarily: https://www.baeldung.com/migrating-to-java-8-date-time-api.

Comment on lines 28 to 42
public boolean dateComparator(String data, String dataFrom, String dataTo) {
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
try {
Date date = format.parse(data);
Date dateFrom = format.parse(dataFrom);
Date dateTo = format.parse(dataTo);
if (date.after(dateFrom) && date.before(dateTo)
|| date.equals(dateFrom) || date.equals(dateTo)) {
return true;
}
} catch (ParseException ex) {
System.out.println("Problem with data" + ex);
}
return false;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you create a formatter, make it a constant field. Since you're using the SimpleDateFormat in the dateComparator method, you should define it as a static final field in the class to avoid creating a new instance every time the method is called. This will also help with thread safety, as SimpleDateFormat is not thread-safe.

Comment on lines 28 to 42
public boolean dateComparator(String data, String dataFrom, String dataTo) {
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
try {
Date date = format.parse(data);
Date dateFrom = format.parse(dataFrom);
Date dateTo = format.parse(dataTo);
if (date.after(dateFrom) && date.before(dateTo)
|| date.equals(dateFrom) || date.equals(dateTo)) {
return true;
}
} catch (ParseException ex) {
System.out.println("Problem with data" + ex);
}
return false;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don’t begin method implementation with an empty line. The dateComparator method starts with an empty line which is unnecessary and should be removed.

Comment on lines 7 to 42
public class SalaryInfo {
public String getSalaryInfo(String[] names, String[] data, String dateFrom, String dateTo) {
return null;
StringBuilder result = new StringBuilder();
result.append("Report for period ").append(dateFrom).append(" - ").append(dateTo);

for (String name : names) {
int salary = 0;
for (String datum : data) {
String[] parts = datum.split(" ");
int hours = Integer.parseInt(parts[2]);
int income = Integer.parseInt(parts[3]);
if (parts[1].equals(name) && dateComparator(parts[0], dateFrom, dateTo)) {
salary += hours * income;
}
}
result.append(System.lineSeparator()).append(name).append(" - ").append(salary);
}

return String.valueOf(result);
}

public boolean dateComparator(String data, String dataFrom, String dataTo) {
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
try {
Date date = format.parse(data);
Date dateFrom = format.parse(dataFrom);
Date dateTo = format.parse(dataTo);
if (date.after(dateFrom) && date.before(dateTo)
|| date.equals(dateFrom) || date.equals(dateTo)) {
return true;
}
} catch (ParseException ex) {
System.out.println("Problem with data" + ex);
}
return false;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use an empty line to start the class implementation. Remove the redundant empty line at the beginning of the SalaryInfo class definition.

Comment on lines +9 to +10
StringBuilder result = new StringBuilder();
result.append("Report for period ").append(dateFrom).append(" - ").append(dateTo);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use StringBuilder instead of String concatenation in loops. Although it's not inside a loop here, it's a good practice to use StringBuilder for assembling strings over multiple operations.

result.append(System.lineSeparator()).append(name).append(" - ").append(salary);
}

return String.valueOf(result);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to call String.valueOf on a StringBuilder instance. StringBuilder's toString method provides the necessary functionality to convert it to a String.

Comment on lines 34 to 35
if (date.after(dateFrom) && date.before(dateTo)
|| date.equals(dateFrom) || date.equals(dateTo)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition can be simplified by using the !date.before(dateFrom) and !date.after(dateTo) checks, which cover the inclusive range between dateFrom and dateTo.

return true;
}
} catch (ParseException ex) {
System.out.println("Problem with data" + ex);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When logging an exception, it's important to include the stack trace for debugging purposes. Consider using ex.printStackTrace() instead of just printing the message.

…hrow ParseExcepion if something isn't right, StringBuilder contained solution and dataFormat changed from Data to LocalData and simplified function.
Copy link

@MateuszRostek MateuszRostek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice try! See some advice and implement it.

}

static final boolean dateComparator(String data, String dataFrom, String dataTo) {
DateTimeFormatter format = DateTimeFormatter.ofPattern(("dd.MM.yyyy"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

result.append(System.lineSeparator()).append(name).append(" - ").append(salary);
}

return new String(result);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How i can return a stringBuilder in "public string" class if i didn' change it into string?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As AI Mentor said, use .toString() method on the stringBuilder.

return new String(result);
}

static final boolean dateComparator(String data, String dataFrom, String dataTo) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing private access modifier

Comment on lines 14 to 17
String[] parts = datum.split(" ");
int hours = Integer.parseInt(parts[2]);
int income = Integer.parseInt(parts[3]);
if (parts[1].equals(name) && dateComparator(parts[0], dateFrom, dateTo)) {
Copy link

@MateuszRostek MateuszRostek Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use magic numbers/string literals, create constans for 0, 1, 2, 3 and " ".

…hrow ParseException if something isn't right, StringBuilder contained solution and dataFormat. Data changed to LocalData and simplified function. Private access added and constants are presents.
Copy link

@MateuszRostek MateuszRostek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please read about constants in java and how to create them:

https://www.javatpoint.com/java-constant

}

private static final boolean dateComparator(String data, String dataFrom, String dataTo) {
DateTimeFormatter format = DateTimeFormatter.ofPattern(("dd.MM.yyyy"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +13 to +19
String regexSymbol = " ";
for (String datum : data) {
String[] parts = datum.split(regexSymbol);
int dateIndex = 0;
int nameIndex = 1;
int hoursIndex = 2;
int incomeIndex = 3;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants