diff --git a/src/main/java/com/h2/SavingsCalculator.java b/src/main/java/com/h2/SavingsCalculator.java index abc4ee92c..55cb6d1f7 100644 --- a/src/main/java/com/h2/SavingsCalculator.java +++ b/src/main/java/com/h2/SavingsCalculator.java @@ -1,6 +1,63 @@ package com.h2; +import java.time.LocalDate; +import java.time.YearMonth; public class SavingsCalculator { + private final float[] credits; + private final float[] debits; + public SavingsCalculator(float[] credits, float[] debits) { + this.credits = credits; + this.debits = debits; + } + + public float calculate() { + return sumOfCredits() - sumOfDebits(); + } + + private float sumOfCredits() { + float sum = 0.0f; + for (float credit : credits) { + sum += credit; + } + return sum; + } + + private float sumOfDebits() { + float sum = 0.0f; + for (float debit : debits) { + sum += debit; + } + return sum; + } + + private static int remainingDaysInMonth(LocalDate d) { + YearMonth yearMonth = YearMonth.of(d.getYear(), d.getMonth()); + int totalDaysInMonth = yearMonth.lengthOfMonth(); + return totalDaysInMonth - d.getDayOfMonth(); + + } + + public static void main(String[] args) { + + + final String[] creditsAsString = args[0].split(","); + final String[] debitsAsString = args[1].split(","); + + final float[] credits = new float[creditsAsString.length]; + final float[] debits = new float[debitsAsString.length]; + + for (int i = 0; i < creditsAsString.length; i++) { + credits[i] = Float.parseFloat(creditsAsString[i]); + } + + for (int i = 0; i < debitsAsString.length; i++) { + debits[i] = Float.parseFloat(debitsAsString[i]); + } + + SavingsCalculator c = new SavingsCalculator(credits, debits); + float netSavings = c.calculate(); + System.out.println("Net Savings = " + netSavings + ", remaining days in month = " + remainingDaysInMonth(LocalDate.now())); + } }