package trickyexamples;

public class CalculateReturning {
  public static void calculateReturning(int cost, int paid) {
    System.out.println("Cost: " + cost + " Paid: " + paid);
    System.out.println("Returns: ");
    if (cost % 5 >= 3) {
      cost += 5 - cost % 5;
    } else {
      cost -= cost % 5;
    }
    int diff = paid - cost;
    if (diff > 0) {
      int[] money = new int[] {20000, 10000, 5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5};
      int[] pieces = new int[money.length];
      for (int i = 0; i < money.length; i++) {
        pieces[i] = diff / money[i];
        diff = diff % money[i];
        System.out.println(money[i] + " : " + pieces[i]);
      }
    } else {
      System.out.println("Paid more!");
    }
  }

  public static void main(String[] args) {
    calculateReturning(100, 20100);
    calculateReturning(1345, 9000);
    calculateReturning(1500, 12000);
    calculateReturning(1503, 12000);
    calculateReturning(992, 4800);
    calculateReturning(12, 20);
    calculateReturning(13, 20);
  }
}

@@ -619,32 +619,64 @@
 ff %25 money%5Bi%5D;%0D%0A
+        if (pieces%5Bi%5D %3E 0) %7B%0D%0A  
         System.o
@@ -728,60 +728,1040 @@
     
-%7D%0D%0A    %7D else %7B%0D%0A      System.out.println(%22Paid more
+  %7D%0D%0A        if (diff == 0) %7B%0D%0A          break;%0D%0A        %7D%0D%0A      %7D%0D%0A    %7D else %7B%0D%0A      System.out.println(%22Paid more!%22);%0D%0A    %7D%0D%0A  %7D%0D%0A%0D%0A  public static void getVisszajaro(int fizetendo, int fizetett) %7B%0D%0A    System.out.println(%22Fizetend: %22 + fizetendo + %22 Fizetett: %22 + fizetett);%0D%0A    if (fizetendo %25 5 %3E= 3) %7B%0D%0A      fizetendo += 5 - fizetendo %25 5;%0D%0A    %7D else %7B%0D%0A      fizetendo -= fizetendo %25 5;%0D%0A    %7D%0D%0A    int kulonbozet = fizetett - fizetendo;%0D%0A    if (kulonbozet %3E 0) %7B%0D%0A      System.out.println(%22Visszajr: %22);%0D%0A      int%5B%5D penzek = new int%5B%5D %7B20000, 10000, 5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5%7D;%0D%0A      int%5B%5D darabok = new int%5Bpenzek.length%5D;%0D%0A      for (int i = 0; i %3C penzek.length; i++) %7B%0D%0A        darabok%5Bi%5D = kulonbozet / penzek%5Bi%5D;%0D%0A        kulonbozet = kulonbozet %25 penzek%5Bi%5D;%0D%0A        if (darabok%5Bi%5D %3E 0) %7B%0D%0A          System.out.println(penzek%5Bi%5D + %22 : %22 + darabok%5Bi%5D);%0D%0A        %7D%0D%0A        if (kulonbozet == 0) %7B%0D%0A          break;%0D%0A        %7D%0D%0A      %7D%0D%0A    %7D else %7B%0D%0A      System.out.println(%22Fizess tbbet
 !%22);
@@ -2072,16 +2072,105 @@
 , 20);%0D%0A
+    getVisszajaro(12, 20);%0D%0A    getVisszajaro(13, 20);%0D%0A    getVisszajaro(1000, 10000);%0D%0A
   %7D%0D%0A%7D%0D%0A

Add example about how to calculate the minimal coins and banknotes of returning in case of overpaid.