Created
February 9, 2016 13:21
-
-
Save naoto-ogawa/049ebda209eff49a376b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class AssignIntegerBetweenMinAndMax { | |
public static void main(String[] args) { | |
Block block = new Block(); | |
block.setMin(10); | |
block.setMax(15); | |
block.addKey("A"); | |
logBlock("01", block); | |
block.addKey("B"); | |
logBlock("02", block); | |
block.addKey("C"); | |
logBlock("03", block); | |
block.addKey("D"); | |
logBlock("04", block); | |
block.addKey("E"); | |
logBlock("05", block); | |
} | |
private static class Block { | |
private List<String> keys = new ArrayList<String>(); | |
private long min; | |
private long max; | |
public Block() { | |
} | |
public Block(String key, long min, long max) { | |
keys.add(key); | |
this.min = min; | |
this.max = max; | |
} | |
public String getLastKey() { | |
return keys.get(keys.size() - 1); | |
} | |
public void addInfo(String key, long min, long max) { | |
keys.add(key); | |
this.min = min; | |
this.max = max; | |
} | |
public void addKey(String s) { | |
keys.add(s); | |
} | |
public List<String> getDat() { | |
return keys; | |
} | |
public long getMin() { | |
return min; | |
} | |
public long getMax() { | |
return max; | |
} | |
public void setMin(long min) { | |
this.min = min; | |
} | |
public void setMax(long max) { | |
this.max = max; | |
} | |
public boolean isAsinable() { | |
return getIncrement() > 0; | |
} | |
public long getIncrement() { | |
double inc = | |
(getMax() - getMin()) | |
/ | |
(double)(keys.size() + 1); | |
return inc < 1 ? 0 : Math.round(inc); | |
} | |
public List<String> asigne() { | |
List<String> ret = new ArrayList<>(); | |
long order = getMin(); | |
final long inc = getIncrement(); | |
for (String key : keys) { | |
order += inc; | |
ret.add(String.format("key=%s, val=%d", key, order)); | |
} | |
return ret; | |
} | |
@Override | |
public String toString() { | |
return String.format("keys=%s, size=%d, min=%d, max=%d", keys.toString(), keys.size(), (int) min, (int) max); | |
} | |
} | |
private static void logBlock(String test, Block block) { | |
log("*+ " + test + " **"); | |
log(block.toString()); | |
log(block.isAsinable()); | |
if (block.isAsinable()) { | |
log(block.asigne()); | |
} | |
} | |
public static void log(Object obj) { | |
System.out.println(obj.toString()); | |
} | |
} | |
Author
naoto-ogawa
commented
Feb 9, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment