I'm a beginner developer and I wrote a simple program. How could be the code improved to meet best practices of the development? I mean if the names of classes or variables are enough self-explaining for others and it is required to insert setters and getters in the classes, when I know that at this point it is necessary? Any help to improve the code would be appreciated.
Brief code explanation
An employee signs a contract with an employer, that he cannot terminate before 18 months working there. Otherwise he is obliged to pay back a certain amount of money, that decrease each month of working.
UPDATE
Assuming a user of the program started a job on 1st April 2017, he inserts in the Main
class a date of starting a potential new job. In the CalculateAmountOfMonthToGo
class determines how many months differs the inserted date and the date that he does not need to pay back any money. Then in the class CalculateAmountOfMoneyToBePaid
it is calculated how much he needs to repay. Later there will be displayed a kind of summary with dates of starting new job and the amount of money to be repaid at that time.
Main.java
package changeWork;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
CalculateAmountOfMonthToGo monthsToGo = new CalculateAmountOfMonthToGo("01-10-2017");
monthsToGo.retriveAmountOfMonthToGo();
CalculateAmountOfMoneyToBePaid moneyToBePaidClass = new CalculateAmountOfMoneyToBePaid();
double money=moneyToBePaidClass.amountMoneyToPayBack();
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("Do splaty pozostalo:");
System.out.println(df.format(money));
System.out.println();
System.out.println("Kwota do zaplaty po kolejnych miesiacach");
moneyToBePaidClass.printMoneyToBePaidEachMonth();
}
}
CalculateAmountOfMonthToGo.java
package changeWork;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class CalculateAmountOfMonthToGo {
private final String END_PENALTY="01-10-2018";
private LocalDate newWorkStartDate;
private String newWorkStart;
private long monthsBetween=0;
public String getNewWorkStart() {
return newWorkStart;
}
public void setNewWorkStart(String newWorkStart) {
this.newWorkStart = newWorkStart;
}
public long getMonthsBetween() {
return monthsBetween;
}
public void setMonthsBetween(long monthsBetween) {
this.monthsBetween = monthsBetween;
}
public CalculateAmountOfMonthToGo(String newWorkStart) {
super();
this.newWorkStart = newWorkStart;
}
public CalculateAmountOfMonthToGo(){
}
public long retriveAmountOfMonthToGo(){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
try{
newWorkStartDate =LocalDate.parse(newWorkStart, formatter);}
catch (Exception e) {
e.printStackTrace();
}
LocalDate endPenaltyDate= LocalDate.parse(END_PENALTY,formatter);
monthsBetween= ChronoUnit.MONTHS.between(newWorkStartDate, endPenaltyDate);
if (monthsBetween<0){
throw new RuntimeException("Rozpoczecie pracy po wygasnieciu okresu splaty");
} else if (monthsBetween>18){
throw new RuntimeException("Bledna data rozpoczecia nowej pracy");
}
return monthsBetween;
}
}
CalculateAmountOfMoneyToBePaid.java
package changeWork;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class CalculateAmountOfMoneyToBePaid {
private final double PENALTY = 6000;
private final double PENALTY_DURATION = 18;
private String newWorkStart;
private long monthsBetween = 0;
private HashMap<String, Double> amountOfMoney = new HashMap<String, Double>();
public CalculateAmountOfMoneyToBePaid() {
}
public CalculateAmountOfMoneyToBePaid(long monthsBetween) {
this.monthsBetween = monthsBetween;
}
public double amountMoneyToPayBack() {
double ratio = monthsBetween / PENALTY_DURATION;
return ratio * PENALTY;
}
public HashMap<String, Double> printMoneyToBePaidEachMonth() {
CalculateAmountOfMonthToGo monthsToGo = new CalculateAmountOfMonthToGo();
System.out.println("Data: \t\t Kwota:");
List<String> keys = Arrays.asList("01-04-2017", "01-05-2017", "01-06-2017", "01-07-2017", "01-08-2017",
"01-09-2017", "01-10-2017", "01-11-2017", "01-12-2017", "01-01-2018", "01-02-2018", "01-03-2018",
"01-04-2018", "01-05-2018", "01-06-2018", "01-07-2018", "01-08-2018", "01-09-2018");
for (int i = 0; i < keys.size(); i++) {
newWorkStart = keys.get(i);
monthsToGo.setNewWorkStart(newWorkStart);
monthsBetween = monthsToGo.retriveAmountOfMonthToGo();
double moneyToPay = amountMoneyToPayBack();
amountOfMoney.put(newWorkStart, moneyToPay);
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(newWorkStart + "\t\t" + df.format(moneyToPay));
}
return amountOfMoney;
}
}