Skip to content

Instantly share code, notes, and snippets.

@lazypower
Created January 22, 2012 02:55
Show Gist options
  • Save lazypower/1655202 to your computer and use it in GitHub Desktop.
Save lazypower/1655202 to your computer and use it in GitHub Desktop.
Public Class FishTank
{
// properties of the FishTank
// Create a list object to hold the wildlife in the tank
public List<Fish> TankLife = new List<Fish>();
// Total widlife Capacity for a healthy tank
public int Gallons;
// default constructor
public FishTank() {}
public FishTank(int waterCapacity)
{
Gallons = waterCapacity;
}
// Factory method used in unit tests
public FishTank tankFactory(int waterCapacity)
{
FishTank thisTank = new FishTank(waterCapacity, tankCapacity);
return thisTank;
}
// Method to add fish
public void addFish(Fish thisFish)
{
// iterate over each fish going in the tank
tankLife.add(fish);
}
// predicate function to test if the tank is overpopulated
// general rule of thumb that i've been taught is 2 fish per gallon. NO MORE
// it promotes less stress on filtration systems -- fish poop is yuck
public bool isOverPopulated()
{
if (tankLife.count() > (Gallons / 2) )
{
return true;
}
else
{
return false;
}
}
}
// Fish object
// TODO: write setters for later object manipulation -- but this simple demo wont receive that.
Public Class Fish
{
// Name the Phylum, class, genum, or just "pete", any is ok
public string FishName; // people forget who fish are - they are fish after all
// so make this property public so we can change it on the fly without a setter method.
private bool isAlgeaEater;
private bool isCannibal;)
private bool Alive = true;
// Default Constructor
public Fish() {}
// public constructor
public Fish(string thisFishName, bool fishEatsAlgea, bool fishEatsOtherFish)
{
FishName = thisFishName;
isAlgeaEater = fishEatsAlgea;
isCannibal = fishEatsOtherFish;
}
// factory method used in unit testing
public Fish FishFactory(string thisFishName, bool fishEatsAlgea, bool fishEatsOtherFish)
{
Fish thisFish = new Fish(thisFishName, fishEatsAlgea, fishEatsOtherFish);
return thisFish;
}
public bool isFishDead()
{
return alive;
}
public bool canEatAlgea()
{
return isAlgeaEater;
}
public bool willEatOtherFish()
{
return isCannibal;
}
public void Swim()
{
// TODO: add swim logic to detect boundaries of tank and swim in circles.
}
public void Float()
{
//TODO: add float logic to bob at the top of the tank
}
public void KillFish()
{
Alive = false;
}
}
public void main()
{
FishTank thisTank = new FishTank(30);
thisTank.addFish(new Fish("bob", true, false));
thisTank.addFish(new Fish("Angel Fish", false, false));
thisTank.addFish(new Fish("Pete", false, false));
foreach (fish in thisTank.TankLife)
{
if (!fish.isFishDead())
{
fish.Swim();
}
else
{
fish.Float();
}
if (fish.isCannibal())
{
// randomly select another fish to eat
int randomFish = math.rand(0, thisTank.TankLife.Count());
thisTank.TankLife[randomFish].KillFish();
thisTank.TankLife[randomFish].Float();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment