package jp.nora.economics.labor;
import java.util.ArrayList;
import java.util.Iterator;
public class SlaveMaster {
public class Slave {
long wallet;
Slave(){
init();
}
String name(){
return getClass().getSimpleName();
}
void init(){
wallet = 10000;
}
boolean spend(){
wallet -= 3000;
return wallet < 0;
}
long bid(){
return 5000;
}
void getPayment(){
wallet += bid();
}
void recoverPayment(){
wallet -= bid();
}
long getWallet(){
return wallet;
}
}
public class Slave2 extends Slave {
long bid(){
return 3000;
}
}
ArrayList<Slave> slaves;
void init(){
slaves = new ArrayList<Slave>();
slaves.add(new Slave()); // and so on. many instances
slaves.add(new Slave2());
slaves.add(new Nora());
}
void print(){
System.out.println("-----");
for(Slave s:slaves){
System.out.print(s.name());
System.out.print(" ");
System.out.print(s.bid());
System.out.print(" ");
System.out.println(s.getWallet());
}
}
void selection(){
Slave max = null;
for (Slave s:slaves){
if ( max == null )
max = s;
if ( max.bid() < s.bid()){
max = s;
}
s.getPayment();
}
max.recoverPayment();
}
boolean spendAll(){
boolean flag = false;
for(Slave s:slaves){
flag |= s.spend();
}
return flag;
}
void dead(){
Iterator<Slave> i = slaves.iterator();
while ( i.hasNext()){
Slave s = i.next();
if ( s.getWallet() < 0 ) {
System.out.println(s.name()+":dead");
i.remove();
}
}
}
public static void main(String args[]){
SlaveMaster sm = new SlaveMaster();
sm.init();
sm.run();
}
public void run(){
while (slaves.size()> 1){
oneTrial();
reset();
}
}
void reset(){
for(Slave s:slaves){
s.init();
}
}
public void oneTrial(){
while ( true){
print();
selection();
if ( spendAll()){
dead();
return;
}
}
}
}