Page 1 of 1

need help debugging two classes

Posted: Thu Nov 30, 2006 6:13 pm
by rjwelte
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


When I add this to my page, it clears any responses that I'm already getting from the main page.  I'm basically adjusting UNIX time stamps.

Code: Select all

class Exception {
    function __construct();

	final public getMessage();
	final public getCode();
	final public getFile();
	final public getLine();
	final public getTrace();
	final public getTraceAsString();

	protected $message;
	protected $code;
	protected $file;
	protected $line;
		
}


class Paydate_Calculator	{

	
  ###
  # Weekend
  # determines if day is a Saturday or a Sunday
  # Then calls LoopType passing "Forward" as its argument
  ###
  */ 
	private function Weekend()	{
		
		if ((date('D',$this->pay_day) == "Saturday") || (date('D',$this->pay_day) == "Sunday"))	
			LoopType("Forward");
			
		if ($this->pay_day == NULL)	{
			throw new Exception("In function Weekend, pay_day is null");
		}
	}

  ###
  # LoopType
  # adds a day or subtracts a day to the payday date depending on
  # whether we recieve "Forward" or "Reverse" as its argument
  ###	
  private function LoopType($type)	{

		switch ($type)	(
			case "Forward":
			$this->pay_day = $this->pay_day + DAY; 
			break;
			 
			case "Reverse":
			$this->pay_day = $this->pay_day - DAY;
			break;
		}
		
		if($type == NULL)	{
			throw new Exception("type is NULL");
		}
	}

  ###
  # CheckIfHoliday
  # if pay-day is a Monday we loop throught the Holiday array
  # to see if the date matches a holiday. If so we add a day 
  # if it isn't we subtract a day
  ###	
	private function CheckIfHoliday()	{

		$blnIsHoliday = 0;
		if(date('N',$this->pay_day) == 1)	{
			for ($i = 0; $i < count($this->holiday_array); $i++)	{
				// add one more day if holiday occurs on a Monday
				if ((date("N",$this->pay_day) == 1) && ($this->pay_day == $this->holiday_array[$i]))	{
					LoopType("Forward");
					$blnIsHoliday = 1; 
				} else	{
					LoopType("Reverse");
					$blnIsHoliday = 1;  
				}
			}
		}
		return $blnIsHoliday;
			
		if ($this->payday == NULL}	{
			throw new Exception("In function CheckIfHoliday,  pay_day is null");
		}
		
	}
	
  ###
  # setDueDate
  # takes the orginal date of the loan and adds 10 days to set the 1st due date
  # then we determine how many intervals there are from then until today
  # We  then keep adding ten days per loop to he orginal due date until we are
  # current with today.
  ###	
	private function setDueDate($type)	{
		// now
		$now = time();
		//Set 1st Due Date
		$original_due_date = $this->fund_day + self::TENDAYS;
		// if due_date occured before today calculate next due_date 										
		While ($original_due_date < $now)	{
			$this->due_date = $original_due_date + self::TENDAYS;
		}	
				
		if ($this->due-day == NULL)	{
				throw new Exception("I function setDuedate due_date is null");
		}
	}

  ###
  # tunePayDay
  # if the client does not desire direct deposit, we add 10 days to the date 
  # we then call Weekend() to determine if its a saturday
  # we again call Weekend() to determine if its a sunday
  # finally we check if it's a Holiday
  ###		
	private function tunePayDay()	{

		if (!$this->direct_deposit) { $this->pay_day = $this->pay_day + DAY; }
		// check if Saturday if yes, Loop forward
		Weekend();
		// check if Sunday if yes, Loop forward
		Weekend();
		// check if it's a Monday and a Holiday, then move it forward else move it backward
		CheckIfHoliday();

	}

  ###
  # Calculate_Due_Date
  # we set all the class variables
  # we then set 1st due date to fund_day + pay_span and make it current
  # we now fine tune the pay date for weekends and holidays
  # returning true or false if this waas accomplished
  # We call all thrown error and print them to the page.
  ###	 
	public function Calculate_Due_Date($fund_day, $holiday_array, $pay_span, $pay_day, $direct_deposit)	{
		
		try	{
			$this->fund_day = $fund_day;
			$this->holiday_array = $holiday_array;
			$this->pay_span = $pay_span;
			$this->pay_day = $pay_day;
			$this->direct_deposit = $direct_deposit;

			try	{
			// set 1st due date to fund_day + pay_span and make it current
			setDueDate();	
			tunePayDay();						
			if ($this->pay_day >= $this->due_date)	{
				return 1;
			} else {
				$this->pay_day = $this->pay_day + $this->pay_span;
				$tunePayDay();
				return 0;	
			}
		} catch (Exception $exception)	{
			print $exception->getMessage();
    		print " in file " . $exception->getFile();
    		print " on line " . $exception->getLine() . "\n";
		} 	
	}

  ###
  # Get_due_date
  # I added this to try and test whether the due date is being set  
  # we then the constructor Calculate_Due_Date is called
  ###		
	function Get_due_date()	{
		return $this->due_date;
	}
	
	const DAY = (1*24*60*60);
	const TENDAYS = (10*24*60*60);
	private $due_date;
	private $fund_day;
	private $holiday_array;
	private $pay_span;
	private $pay_day;
	private $direct_deposit;
	
};
On the main page I'm instantiating the class as follows passing in UNIX time stamps:

Code: Select all

$calc = new Paydate_Calculator($fund_day, $holiday_array, $pay_span, $pay_day, $direct_deposit);
if ($calc == 1)	{
	$pay_day = $calc->Get_due_date();
	echo "pay_day is: $payday";
}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Nov 30, 2006 11:19 pm
by feyd
Coding Critique is not the location to get help debugging one's class(es).

Need Help debugging class - revised but still no good

Posted: Fri Dec 01, 2006 12:40 am
by rjwelte
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?php	

class Exception {
    public function __construct();

	final public getMessage();
	final public getCode();
	final public getFile();
	final public getLine();

	public $message;
	public $code;
	public $file;
	public $line;
		
}

class NullHandleException extends Exception {
    public function __construct($message)
    {
        parent::__construct($message);
    }
}

class Paydate_Calculator {

	###
	# __construct
	# we set all the class variables here
	###	    
    public function __construct($fund_day, $holiday_array, $pay_span, $pay_day, $direct_deposit)
    {
		$this->fund_day = $fund_day; 
		$this->holiday_array = $holiday_array; 
		$this->pay_span = $pay_span; 
		$this->pay_day = $pay_day; 
		$this->direct_deposit = $direct_deposit;
    }

	###
	# setDueDate
	# takes the orginal date of the loan and adds 10 days to set the 1st due date
	# then we determine how many intervals there are from then until today
	# We  then keep adding ten days per loop to he orginal due date until we are
	# current with today.
	###	
	public function setDueDate()	{
		// now
		$now = time();
		//Set 1st Due Date
		$original_due_date = $this->fund_day + TENDAYS;
		// if due_date occured before today calculate next due_date 										
		While ($original_due_date < $now)	{
			$this->due_date = $original_due_date + TENDAYS;
		}	

		if ($this->fund_day == NULL}	{
			throw new NullHandleException("fund_day is null");
		}
	}
	
	###
	# Calculate_Due_Date
	# we then set 1st due date to fund_day + pay_span and make it current
	# we now fine tune the pay date for weekends and holidays
	# returning true or false if this waas accomplished
	# We catch all thrown error and print them to the page.
	###	 
	public function Calculate_Due_Date()	{
		
		try	
		{
			// set 1st due date to fund_day + pay_span and make it current
			setDueDate();	
			tunePayDay();						
			if ($this->pay_day >= $this->due_date)	{
				return 1;
			} else {
				$this->pay_day = $this->pay_day + $this->pay_span;
				Calculate_Due_Date();
			}
		} catch (NullHandleException $exception) {
			print $exception->getMessage();
			print " in file " . $exception->getFile();
			print " on line " . $exception->getLine() . "\n";

		} catch (Exception $exception)	{
			print $exception->getMessage();
    		print " in file " . $exception->getFile();
    		print " on line " . $exception->getLine() . "\n";
		} 	
	}

	###
	# tunePayDay
	# if the client does not desire direct deposit, we add 10 days to the date 
	# we then call Weekend() to determine if its a saturday
	# we again call Weekend() to determine if its a sunday
	# finally we check if it's a Holiday
	###		
	public function tunePayDay()	{

		// check if direct_deposit is no, Loop forward
		if (!$this->direct_deposit) 
			LoopType("Forward"); 
		// check if Saturday if yes, Loop forward
		if (date('D',$this->pay_day) == "Sat")	
			LoopType("Forward");
		// check if Sunday if yes, Loop forward	
		if (date('D',$this->pay_day) == "Sun")	
			LoopType("Forward");
		// check if it's a Monday and a Holiday, then move it forward else move it backward
		if(date('D',$this->pay_day) == "Mon")	{
			for ($i = 0; $i < count($this->holiday_array); $i++)	{
				// add one more day if holiday occurs on a Monday
				if ((date("D",$this->pay_day) == "Mon") && ($this->pay_day == $this->holiday_array[$i]))	{
					LoopType("Forward");
				} else {
					LoopType("Reverse");
				}
			}
		}
		
		if($this->direct_deposit == NULL) {
			throw new NullHandleException("direct_deposit is null");
		}

		if($this->holiday_array == NULL) {
			throw new NullHandleException("holiday_array is null");
		}	
	}

	###
	# LoopType
	# adds a day or subtracts a day to the payday date depending on
	# whether we recieve "Forward" or "Reverse" as its argument
	###	
	private function LoopType($type)	{

		switch ($type)	{
			case "Forward":
			$this->pay_day = $this->pay_day + DAY; 
			break;
			 
			case "Reverse":
			$this->pay_day = $this->pay_day - DAY;
			break;
		}
		
		if($type == NULL)	{
			throw new NullHandleException("type is NULL");
		}
		
	}
	
	###
	# Get_due_date
	# I added this to try and test whether the due date is being set  
	###		
	public function Get_due_date()	{
		return $this->due_date;
	}
	
	###
	# Get_pay_day
	# I added this to try and test whether the pay_day is being set  
	###	
	public function Get_pay_day()	{
		return $this->due_date;
	}

	define("DAY",    (1*24*60*60));
	define("TENDAYS",    (10*24*60*60));
	public $fund_day, $holiday_array, $due_date;
	public $pay_span, $pay_day, $direct_deposit;
}

$fund_day = strtotime("12/15/2005");
// setup holiday array
$holiday_array[0] = strtotime("1/1/2006");
$holiday_array[1] = strtotime("1/16/2006");
$holiday_array[2] = strtotime("2/20/2006");
$holiday_array[3] = strtotime("5/29/2006");
$holiday_array[4] = strtotime("7/4/2006");
$holiday_array[5] = strtotime("9/4/2006");
$holiday_array[6] = strtotime("10/9/2006");
$holiday_array[7] = strtotime("11/11/2006");
$holiday_array[8] = strtotime("11/23/2006");
$holiday_array[9] = strtotime("12/25/2006");

//setup pay_span 
$pay_span =  (7*24*60*60); //weekly (7*24*60*60),bi-weekly (14*24*60*60),monthly(30*24*60*60)

// setup $pay_day
$now = time();

// calculaate latest pay_day = fund_day + (#pay_span intervals between fund_day and present * pay_span)
$pay_day = $fund_day + strtotime(((int)$now/$pay_span) * $this->pay_span);

//setup direct deposit	false = 0
$direct_deposit = 0;

$obj = new Paydate_Calculator($fund_day, $holiday_array, $pay_span, $pay_day, $direct_deposit);
$obj->Calculate_Due_Date();
print "due_date is: " . $obj->Get_due_date();
print "pay_day is: " . $obj->Get_pay_day();

?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Dec 01, 2006 12:45 am
by feyd
Post code using the appropriate tags please. None is rarely acceptable.

Posted: Fri Dec 01, 2006 1:07 am
by rjwelte
feyd wrote:Post code using the appropriate tags please. None is rarely acceptable.
Hows hould code be posted? This message doesn't help me at all. Please give me an example. Thankyou.

Posted: Fri Dec 01, 2006 1:11 am
by feyd
There are links that have been added to your posts at the top and bottom if you look at them.