-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
Conversation
…throw ParseExcepion if something isn't right, StringBuilder contained solution and dataFormat.
There was a problem hiding this 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! ✨
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; |
There was a problem hiding this comment.
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.
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; | ||
} |
There was a problem hiding this comment.
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.
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; | ||
} |
There was a problem hiding this comment.
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.
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; | ||
} |
There was a problem hiding this comment.
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.
StringBuilder result = new StringBuilder(); | ||
result.append("Report for period ").append(dateFrom).append(" - ").append(dateTo); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
if (date.after(dateFrom) && date.before(dateTo) | ||
|| date.equals(dateFrom) || date.equals(dateTo)) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this 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")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result.append(System.lineSeparator()).append(name).append(" - ").append(salary); | ||
} | ||
|
||
return new String(result); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing private
access modifier
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)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this 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:
} | ||
|
||
private static final boolean dateComparator(String data, String dataFrom, String dataTo) { | ||
DateTimeFormatter format = DateTimeFormatter.ofPattern(("dd.MM.yyyy")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String regexSymbol = " "; | ||
for (String datum : data) { | ||
String[] parts = datum.split(regexSymbol); | ||
int dateIndex = 0; | ||
int nameIndex = 1; | ||
int hoursIndex = 2; | ||
int incomeIndex = 3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No description provided.