Advertisement
jarinel

Untitled

Oct 1st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 2.37 KB | None | 0 0
  1. import groovy.transform.Canonical
  2. import groovy.transform.EqualsAndHashCode
  3. import org.apache.commons.lang.math.LongRange
  4.  
  5. import java.util.stream.Collectors
  6. import java.util.stream.Stream
  7.  
  8.  
  9. def all(List<List> list) {
  10.     if (list.size() == 1) {
  11.         return list
  12.     }
  13.  
  14.     all(list.tail()).stream()
  15.             .flatMap { Stream.of(list.head() + it, it) }
  16.             .collect(Collectors.toList()) << list.head()
  17. }
  18.  
  19. @Canonical
  20. @EqualsAndHashCode(excludes = ['pledges', 'amount'])
  21. class Kd {
  22.     Long id
  23.     Double initial
  24.     Double amount
  25.     List<Oo> pledges
  26.  
  27.     Kd(Long id, Double amount, List<Oo> pledges) {
  28.         this.id = id
  29.         this.initial = this.amount = amount
  30.         this.pledges = pledges
  31.     }
  32. }
  33.  
  34. @Canonical(excludes = 'loans')
  35. @EqualsAndHashCode(excludes = ['loans', 'amount'])
  36. class Oo {
  37.     Long id
  38.     Double initial
  39.     Double amount
  40.     Map<Kd, Double> loans
  41.  
  42.     Oo(Long id, Double amount) {
  43.         this.id = id
  44.         this.amount = this.initial = amount
  45.     }
  46. }
  47.  
  48. def generate(int count, double chance = 0.2, int ooMax = 50) {
  49.     def kdResult = new IntRange(false, 0, count).collect { id ->
  50.         new Kd(id, (Math.random() * 10).intValue() * 100 + 100, [])
  51.     }
  52.  
  53.     long idCounter = 0
  54.     def ooResult = all(kdResult.collect { [it] }).stream()
  55.         .filter { Math.random() > chance }
  56.         .map { List<Kd> kdList ->
  57.             def amount = (Math.random() * ooMax).intValue() * 10 + 20
  58.             def oo = new Oo(idCounter++, amount)
  59.             oo.loans = kdList.collectEntries { [(it): 0d] }
  60.             kdList.each { kd ->
  61.                 kd.pledges << oo
  62.             }
  63.  
  64.             return oo
  65.         }
  66.         .collect(Collectors.toList())
  67.  
  68.     return [kd: kdResult, oo: ooResult]
  69. }
  70.  
  71. void printTable(List<Kd> kdList, List<Oo> ooList) {
  72.     print String.format('%14s', '')
  73.     ooList.each {
  74.         print String.format('%7s', "OO ${it.id + 1}")
  75.     }
  76.     println()
  77.     kdList.each { kd ->
  78.         print String.format('KD %d %8s:', kd.id + 1, "(${kd.amount})")
  79.         ooList.each { oo ->
  80.             if (kd.pledges.contains(oo)) {
  81.                 print String.format('%7.1f', oo.amount)
  82.             } else {
  83.                 print String.format('%6s%1s', '--', '')
  84.             }
  85.         }
  86.         println()
  87.     }
  88. }
  89.  
  90. def result = generate(4, 0.4, 30)
  91. printTable(result['kd'], result['oo'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement