Saturday, November 1, 2008

class Struggle 1.0



/*
 *   class Struggle 1.0
 *
 *   by James Matthew B. Miraflor
 *   October 31, 2008
 *
 */

import java.util.*;

class Worker {



   private Capitalist Employer;
   private Worker Spouse;
   private Vector Children;
   private int ID;
   private int productiveCapacity;
   private int consumptionNeeds;
   private int lifespan;
   private int Savings;

   private static int count;

   // Constructors

   public Worker (Capitalist Employer) {
      Random random = new Random();      
      count++;
      this.Employer = Employer;
      this.ID = count;
      this.lifespan = random.nextInt(100);
      this.productiveCapacity = random.nextInt(30);
      this.consumptionNeeds = this.productiveCapacity;
      this.Savings = 0;
   }

   // Destructor

   protected void finalize () {
      if (Spouse!=null) Spouse.setSavings(this.Savings);
      else if(Children!=null) {
         for (int i=0; i <Children.size(); i++)
            ((Worker)Children.elementAt(i)).setSavings(
               ((Worker)Children.elementAt(i)).getSavings()+
               (getSavings()/Children.size())
            );
      }
      count--;
   }

   // Accessors
   
   public int getName () { return this.ID; }
   public int getSavings () { return this.Savings;   }
   public int getConsumptionNeeds() { return this.consumptionNeeds; }
   public int getProductiveCapacity() { return this.productiveCapacity; }
   public Capitalist getEmployer () { return this.Employer; }

   // Mutators
      
   public void setEmployer (Capitalist Employer) { this.Employer = Employer; }
   public void setSavings (int savings) { this.Savings = savings; }   

   // Other functions

   public void produceChild () { Children.addElement(new Worker(null)); };
   public void marry (Worker spouse) { this.Spouse = spouse; }
   public void age () { 
      if(this.lifespan <=1) finalize(); 
      else {
         this.lifespan--; 
         this.productiveCapacity++;
      }      
   }
   public void produceWealth () {      
      Employer.setWealth(Employer.getWealth()+this.productiveCapacity);      
      this.lifespan--; 
   }
   public void getWage(int wage) { this.Savings = wage; }
   public void consume() { 
      if(this.Savings>=this.consumptionNeeds) this.Savings -= this.consumptionNeeds;
      else this.finalize(); // Worker dies of starvation
   }
   public void resign() { this.Employer = null; }
}

class Capitalist {



   private Capitalist Spouse;
   private Vector Children;
   private int Wealth;
   private int ID;
   private static int count;
   private int lifespan;
   private int wagingCapacity;
   private int consumptionNeeds;

   // Constructors

   public Capitalist (int wealth) {
      Random random = new Random();
      count++;
      this.ID = count;
      this.lifespan = random.nextInt(100);
      this.wagingCapacity = random.nextInt(300);
      this.consumptionNeeds = this.wagingCapacity/10;
      if(wealth!=0) this.Wealth = wealth;
   }

   // Destructor

   protected void finalize () {
      if (Spouse!=null) Spouse.setWealth(this.Wealth);
      else if(Children!=null) {
         for (int i=0; i <Children.size(); i++)
            ((Capitalist)Children.elementAt(i)).setWealth(
               ((Capitalist)Children.elementAt(i)).getWealth()+
               (getWealth()/Children.size()
             )
         );
      }
      count--;
   }

   // Accessors

   public int getName () { return this.ID; }
   public int getWealth () { return this.Wealth; }
   public int getWagingCapacity () { return this.wagingCapacity; }

   // Mutators
   
   public void setWealth (int money) { this.Wealth = money; }
   
   // Other Functions

   public void createChild () { Children.addElement(new Capitalist(0)); };
   public void marry (Capitalist spouse) { this.Spouse = spouse; }
   public void age () {
      if(this.lifespan <=1) finalize(); 
      else {
         this.lifespan--;
         this.consumptionNeeds++;
      } 
   }
   public void consume () {
      Wealth -= this.consumptionNeeds; 
      lifespan += Math.round(this.consumptionNeeds/100); 
   }

   public void wageContract (int wage) { this.wagingCapacity -= wage; }
   public void resetWagingCapacity () { 
       this.wagingCapacity = this.Wealth - this.consumptionNeeds*30; 
   }

}

class Struggle {



   private static Vector CapitalistClass;
   private static Vector WorkingClass;

   public static void main (String args[]) {
      Random random = new Random();
      
      int capitalistPopulation = random.nextInt(10);
      int workerPopulation = random.nextInt(100);
      for (int i=0;i <capitalistPopulation;i++) 
          CapitalistClass.addElement(new Capitalist(random.nextInt(100)));
      for (int i=0;i <workerPopulation;i++) 
          WorkingClass.addElement(new Worker(null));      
      
      /* Time */
      int days_per_months = 30;
      int months_per_year = 12;
      int years = 100;
      
      int generation = 0;
      for (int year=0;year <years;year++)   {         
         for (int month=0;month <months_per_year;month++) {
            wage();
            for (int day=0;day <days_per_months;day++) {
               for (int i=0;i <CapitalistClass.size();i++)         
                  ((Capitalist)CapitalistClass.elementAt(i)).consume();
               for (int i=0;i <WorkingClass.size();i++) {         
                  ((Worker)WorkingClass.elementAt(i)).produceWealth();
                  ((Worker)WorkingClass.elementAt(i)).consume();
               }               
            }            
         }
         compete();         
         hire();
         age();
         
         generation++;
         if (generation>20) {
            breed();
            generation = 0;
         }         
      }      
   }

   public static void compete () {
      int weak1=0, weak2=1;
      for (int j=0;j <CapitalistClass.size();j++) {
         Capitalist current = (Capitalist)CapitalistClass.elementAt(j);
         if (
            current.getWealth()  < 
            ((Capitalist)CapitalistClass.elementAt(weak1)).getWealth()
         ) {
            weak2 = weak1;
            weak1 = j;
         }
         else if (
            current.getWealth()  < 
            ((Capitalist)CapitalistClass.elementAt(weak2)).getWealth()
         ) 
            weak2 = j;
      }
      Capitalist weakCap1 = (Capitalist)CapitalistClass.elementAt(weak1);
      Capitalist weakCap2 = (Capitalist)CapitalistClass.elementAt(weak2);
      WorkingClass.addElement(proletarianize(weakCap1,weakCap2));      
   }

   public static Worker proletarianize (Capitalist individual, Capitalist employer) {
      Worker newWorker = new Worker(employer);      
      individual.finalize();
      return newWorker;
   }

   public static void breed () {      
      Random random = new Random();
      int newCapitalists = random.nextInt(3);
      int newWorkers = random.nextInt(3);
      
      for (int i=0;i <CapitalistClass.size();i++)      
         for (int j=0;j <newCapitalists;j++) 
            ((Capitalist)CapitalistClass.elementAt(i)).createChild();
      
      for (int i=0;i <WorkingClass.size();i++)          
         for (int j=0;j <newWorkers;j++) 
            ((Worker)WorkingClass.elementAt(i)).produceChild();   
   }

   public static void wage () {
      for (int i=0;i <WorkingClass.size();i++) {
         Worker worker = (Worker)WorkingClass.elementAt(i);
         int wage = 30*worker.getConsumptionNeeds();
         Capitalist employer = (Capitalist)worker.getEmployer();
         employer.setWealth(employer.getWealth()-wage);
         worker.getWage(wage);
      }
   }

   public static void hire () {
      int selected,high,j;

      for (int i=0;i <CapitalistClass.size();i++) {
         while (((Capitalist)CapitalistClass.elementAt(i)).getWagingCapacity()>0) {
            j=0;
            high = ((Worker)WorkingClass.elementAt(0)).getProductiveCapacity();
            for (;j <WorkingClass.size();j++) {
               if(
                  WorkingClass.elementAt(j)!=null&& 
                  ((Worker)WorkingClass.elementAt(j)).getEmployer()==null&& 
                  high <((Worker)WorkingClass.elementAt(j)).getProductiveCapacity()
               ) {
                  selected = j;
                  high = ((Worker)WorkingClass.elementAt(j)).getProductiveCapacity();
               }
            }
            ((Worker)WorkingClass.elementAt(j)).setEmployer(
               (Capitalist)CapitalistClass.elementAt(i)
            );
            ((Capitalist)CapitalistClass.elementAt(i)).wageContract(
               ((Worker)WorkingClass.elementAt(j)).getConsumptionNeeds()
            );            
         }         
      }
   }
   
   public static void age () {
      for (int i=0;i <CapitalistClass.size();i++)         
         ((Capitalist)CapitalistClass.elementAt(i)).age();
      for (int i=0;i <WorkingClass.size();i++)
         ((Worker)WorkingClass.elementAt(i)).age();      
   }
}

3 comments:

Anonymous said...

cool...submit mo to somewhere ha.
:)

Ivan Eugenio said...

Hehe. GPL ba ang license nito?

James Miraflor said...

Dual licensing tol. Yung class Worker GPL. Yung class Capitalist proprietary. :-P