fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. System.out.println(InstallmentTransaction.getInstance(10, false).toString());
  14. }
  15. }
  16.  
  17. class InstallmentTransaction {
  18. private final int installments;
  19. private final boolean interest;
  20.  
  21. private static final Map<Integer, Map<Boolean, InstallmentTransaction>> instances = new HashMap<>();
  22. private static final Object lockObject = new Object();
  23.  
  24. private InstallmentTransaction(int installments, boolean interest)
  25. {
  26. this.installments = installments;
  27. this.interest = interest;
  28. }
  29.  
  30. public static InstallmentTransaction getInstance(int installments, boolean interest){
  31. synchronized(lockObject){
  32. Map<Boolean, InstallmentTransaction> map = instances.get(installments);
  33. if(map == null){
  34. map = new HashMap<>();
  35. instances.put(installments, map);
  36. }
  37. InstallmentTransaction instance = map.get(interest);
  38. if(instance == null){
  39. instance = new InstallmentTransaction(installments, interest);
  40. map.put(interest, instance);
  41. }
  42. return instance;
  43. }
  44. }
  45.  
  46. //getters go here
  47. }
Success #stdin #stdout 0.1s 320576KB
stdin
Standard input is empty
stdout
InstallmentTransaction@52e922