Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
interface IFizzBuzz
{
public function setFizzBuzz($fizz = null, $buzz = null);
public function checkFizz($int);
public function checkBuzz($int);
public function generateFizzBuzz();
}
class FizzBuzz implements IFizzBuzz
{
/**
* Fizz
*
* @var string
*/
public $fizzText = 'Fizz';
/**
* Buzz
*
* @var string
*/
public $buzzText = 'Buzz';
/**
* Lower interger range.
*
* @var int
*/
public $lowerInt = 1;
/**
* Upper interger range.
*
* @var int
*/
public $upperInt = 100;
interface IFormatter
{
public function newline($str);
}
class FizzBuzzFormatter implements IFormatter
{
/*
* Converts FizzBuzz row to newline string.
*
* @param string $str FizzBuzz result row.
* @return string FizzBuzz formatted result row.
*/
public function newline($str)
{
return "<div>$str</div>";
}
}
$fizzbuzz = new FizzBuzz;
$fbArray = $fizzbuzz->generateFizzBuzz();
$fbFormatter = new FizzBuzzFormatter;
foreach($fbArray as $fb)
{
print $fbFormatter->newline($fb);
}
Name:
Anonymous2008-04-29 21:24
Bow down before the ENTERPRISE package fizzbuzz;
public interface IFizzBuzz {
public void setStart();
public void setEnd();
public String getFizzBuzz();
}
=================================================
package fizzbuzz;
/**
* Singleton adapter factory
*/
public class FizzBuzzFactory {
public static final FizzBuzzFactory INSTANCE = new FizzBuzzFactory();
private FizzBuzzFactory() {
registerAdapters();
}
public <T> createInstance(Class<T> clazz) {
if (clazz == IFizzBuzz.class) {
return new FizzBuzzImpl();
} else return null;
}
public String getFizzBuzz() {
String result = "";
for (int i = fStart; i <= fEnd; i++) {
if (getFizz(i) && getBuzz(i))
result += "FizzBuzz";
else if (getFizz(i))
result += "Fizz";
else if (getBuzz(i))
result += "Buzz";
else result += i;
}
return result;
}
}
=================================================
import junit.framework.*;
public class TestFizzBuzz extends TestCase {
public void setUp() {
}
public void tearDown() {
}
public void testGetFizzBuzz() {
IFizzBuzz f = FizzBuzzFactory.INSTANCE.createInstance(IFizzBuzz.class);
f.setStart(1);
f.setEnd(100);
AssertEquals(f.getFizzBuzz(),