Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import groovy.transform.Canonical
- import groovy.transform.EqualsAndHashCode
- import org.apache.commons.lang.math.LongRange
- import java.util.stream.Collectors
- import java.util.stream.Stream
- def all(List<List> list) {
- if (list.size() == 1) {
- return list
- }
- all(list.tail()).stream()
- .flatMap { Stream.of(list.head() + it, it) }
- .collect(Collectors.toList()) << list.head()
- }
- @Canonical
- @EqualsAndHashCode(excludes = ['pledges', 'amount'])
- class Kd {
- Long id
- Double initial
- Double amount
- List<Oo> pledges
- Kd(Long id, Double amount, List<Oo> pledges) {
- this.id = id
- this.initial = this.amount = amount
- this.pledges = pledges
- }
- }
- @Canonical(excludes = 'loans')
- @EqualsAndHashCode(excludes = ['loans', 'amount'])
- class Oo {
- Long id
- Double initial
- Double amount
- Map<Kd, Double> loans
- Oo(Long id, Double amount) {
- this.id = id
- this.amount = this.initial = amount
- }
- }
- def generate(int count, double chance = 0.2, int ooMax = 50) {
- def kdResult = new IntRange(false, 0, count).collect { id ->
- new Kd(id, (Math.random() * 10).intValue() * 100 + 100, [])
- }
- long idCounter = 0
- def ooResult = all(kdResult.collect { [it] }).stream()
- .filter { Math.random() > chance }
- .map { List<Kd> kdList ->
- def amount = (Math.random() * ooMax).intValue() * 10 + 20
- def oo = new Oo(idCounter++, amount)
- oo.loans = kdList.collectEntries { [(it): 0d] }
- kdList.each { kd ->
- kd.pledges << oo
- }
- return oo
- }
- .collect(Collectors.toList())
- return [kd: kdResult, oo: ooResult]
- }
- void printTable(List<Kd> kdList, List<Oo> ooList) {
- print String.format('%14s', '')
- ooList.each {
- print String.format('%7s', "OO ${it.id + 1}")
- }
- println()
- kdList.each { kd ->
- print String.format('KD %d %8s:', kd.id + 1, "(${kd.amount})")
- ooList.each { oo ->
- if (kd.pledges.contains(oo)) {
- print String.format('%7.1f', oo.amount)
- } else {
- print String.format('%6s%1s', '--', '')
- }
- }
- println()
- }
- }
- def result = generate(4, 0.4, 30)
- printTable(result['kd'], result['oo'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement