You need to use the Report->printToTerminal() method provided in the code file i
ID: 3733465 • Letter: Y
Question
You need to use the Report->printToTerminal() method provided in the code file in your Report class. You should also set the timezone to CST which is also shown in the code file. Finally, all your output should match that shown in the code file. You can copy out parts of the code to test your program. I will run the code file in order to grade your program as well as review your source code.
Each child class needs to implement various abstract methods which are used to generate the report result. Since all the reports should be in the same form, an abstract class Report is used as the parent for all the children.
Abstract Class Report
Properties:
private $generation_time_stamp
Abstract protected methods:
getTitle(): should return the title of the report as a string
getReportDescription(): should return the description of the report as a string
getParameterString(): should return the parameters given to the report as a string
getReportResultString(): should return the result of the report computation
Protected methods:
getGenerationTimeStamp(): returns the value of the private property $generation_time_stamp
Public methods:
printToTerminal(): prints all the report information to the terminal. Code is given for this method.
__construct(): set the private instance variable $generation_time_stamp to the current time stamp in the DATE_RFC2822 format (an example is given in the given code).
printToTerminal: should be implemented as (which is provided in the given code file): public function printToTerminal()
{
echo "Report title: {$this->getTitle()} "; echo "Report generated at: {$this->getGenerationTimeStamp()} "; echo "Report description: {$this->getReportDescription()} "; echo "Report parameters: {$this->getParameterString()} "; echo "Report result: {$this->getReportResultString()} ";
}
Basics of the Subclasses (child classes)
Each child class requires parameters to in order to generate the report. The parameters are passed into the constructor. A private instance variable should be used to store each parameter. The report result can either be generated in the constructor or in the getReportResultString() method.
Class RangeReport extends Report
title: "Number Range Generation" description: "Prints every integer from START to END separated by a space." parameter string: "START: $this->start END: $this->end" (make your variable names match)
report result string: The string of integers separated by a command a space, example:
"3, 4, 5" constructor: first argument is the starting integer, second argument is the ending integer
constructor required validation of the starting and ending integers: Use the filter_var function to ensure that the start and end values are integers and that they are in the range of 0 to 100. You don't need to perform any more validation than that. For example, it is okay if the end value is less than the starting value (that should return an empty string as there is no range value for those parameters).
invalid input action: For both the start and end integers, throw an exception if they fail validation. There should be one exception thrown for when the start value fails with the message: "Bad start value." and another exception thrown for when the end values fails with the message: "Bad end value."
Class SumReport extends Report
title: "Sum Calculator" description: "Sums the given integers."
parameter string: "INTEGERS: " followed by the list of integers separated by a space and a comma (hint use the array function implode()), example:
"INTEGERS: 2, 4, 6" report result string: The sum of the integers give in the input array.
constructor: only argument is an array of integers validation: no validation required
Class MinMaxReport extends Report
title: "Minimum and Maximum Finder" description: "Find the minimum and maximum of the given integers."
parameter string: "INTEGERS: " followed by the list of integers separated by a space and a comma (hint use the array function implode()), example:
"INTEGERS: 2, 4, 6" report result: "MINIMUM: $min MAXIMUM: $max" (make your variable names match) constructor: only argument is an array of integers (hint, array sorting can be used to find the min/max) validation: no validation required
Explanation / Answer
<?php
abstract class Report{
private $generation_time_stamp;
protected abstract function getTitle(): string;
protected abstract function getReportDescription(): string;
protected abstract function getParameterString(): string;
protected abstract function getReportResultString(): string;
public function __construct(){
$this->generation_time_stamp = date(DATE_RFC2822);
}
protected function getGenerationTimeStamp(){
return $this->generation_time_stamp . '<br>';
}
public function printToTerminal(){
echo "Report title: {$this->getTitle()} <br>";
echo "Report generated at: {$this->getGenerationTimeStamp()} <br>";
echo "Report description: {$this->getReportDescription()} <br>";
echo "Report parameters: <br>{$this->getParameterString()} <br> <br>";
echo "Report result: <br>{$this->getReportResultString()} <br>";
}
}
class RangeReport extends Report{
private $title = "Number Range Generation";
private $description = "Prints every integer from START to END separated by a space.";
private $parameter_string = "";//"START: " . $this->start . " END: " . $this->end;
private $report_result_string = "";//The string of integers separated by a command a space
public function __construct($starting_int, $ending_int){
parent::__construct();
$min = 1;
$max = 100;
if (filter_var($starting_int, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$min, "max_range"=>$max))) === false) {
throw new Exception("Bad start value");
}
if (filter_var($ending_int, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$min, "max_range"=>$max))) === false) {
throw new Exception("Bad end value");
}
$this->parameter_string = "START: $starting_int <br>END: $ending_int";
for($i = $starting_int; $i<=$ending_int; $i++){
$this->report_result_string .= "$i, ";
}
rtrim($this->report_result_string,', ');
}
public function getTitle(): string{
return $this->title;
}
public function getReportDescription(): string{
return $this->description;
}
public function getParameterString(): string{
return $this->parameter_string;
}
public function getReportResultString(): string{
return $this->report_result_string;
}
}
class SumReport extends Report{
private $title = "Sum Calculator";
private $description = "Sums the given integers.";
private $parameter_string = ""; //"INTEGERS: " followed by the list of integers separated by a space and a comma
private $report_result_string = ""; //The sum of the integers give in the input array.
public function __construct(array $values){
parent::__construct();
$this->parameter_string = "INTEGERS: " . implode(", ",$values);
rtrim($this->parameter_string, ', ');
$this->report_result_string = "" . array_sum($values);
}
public function getTitle(): string{
return $this->title;
}
public function getReportDescription(): string{
return $this->description;
}
public function getParameterString(): string{
return $this->parameter_string;
}
public function getReportResultString(): string{
return $this->report_result_string;
}
}
class MinMaxReport extends Report{
private $title = "Minimum and Maximum Finder";
private $description = "Find the minimum and maximum of the given integers.";
private $parameter_string = ""; //"INTEGERS: " followed by the list of integers separated by a space and a comma
private $report_result_string = ""; //"MINIMUM: $min MAXIMUM: $max" (make your variable names match)
public function __construct(array $values){
parent::__construct();
sort($values);
$this->parameter_string = "INTEGERS: " . implode(", ",$values);
rtrim($this->parameter_string, ', ');
$this->report_result_string = "MINIMUM: " . min($values) . "<br>MAXIMUM: " . max($values);
}
public function getTitle(): string{
return $this->title;
}
public function getReportDescription(): string{
return $this->description;
}
public function getParameterString(): string{
return $this->parameter_string;
}
public function getReportResultString(): string{
return $this->report_result_string;
}
}
date_default_timezone_set('America/Chicago');
/* OUTPUT:
* Report title: Sum Calculator
* Report generated at: Tue, 13 Sep 2016 14:45:09 -0500
* Report description: Sums the given integers.
* Report parameters:
* INTEGERS: 2, 4, 6
*
* Report result:
* 12
*/
$my_sum_report = new SumReport([2, 4, 6]);
$my_sum_report->printToTerminal();
echo "<br>-----------------------------------------------<br>";
/* OUTPUT:
* Report title: Minimum and Maximum Finder
* Report generated at: Tue, 13 Sep 2016 14:45:09 -0500
* Report description: Find the minimum and maximum of the given integers.
* Report parameters:
* INTEGERS: 3, 7, 8, 9, 27
*
* Report result:
* MINIMUM: 3
* MAXIMUM: 27
*/
$my_min_max_report = new MinMaxReport([8, 3, 9, 27, 7]);
$my_min_max_report->printToTerminal();
echo "<br>-----------------------------------------------<br>";
/* OUTPUT:
* Report title: Number Range Generation
* Report generated at: Tue, 13 Sep 2016 14:45:09 -0500
* Report description: Prints every integer from START to END separated by a space.
* Report parameters:
* START: 4
* END: 21
*
* Report result:
* 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
*/
$my_range_report = new RangeReport(4, 21);
$my_range_report->printToTerminal();
echo "<br>-----------------------------------------------<br>";
// OUTPUT: Sorry, there was a problem running RangeReport: Bad start value.
try {
$my_range_report = new RangeReport(-2, 9);
$my_range_report->printToTerminal();
} catch (Exception $e) {
echo "Sorry, there was a problem running RangeReport: " . $e->getMessage() . " <br>";
}
// OUTPUT: Sorry, there was a problem running RangeReport: Bad end value.
try {
$my_range_report = new RangeReport(4, 102);
$my_range_report->printToTerminal();
} catch (Exception $e) {
echo "Sorry, there was a problem running RangeReport: " . $e->getMessage() . " <br>";
}
// OUTPUT: Sorry, there was a problem running RangeReport: Bad start value.
try {
$my_range_report = new RangeReport('a string', 102);
$my_range_report->printToTerminal();
} catch (Exception $e) {
echo "Sorry, there was a problem running RangeReport: " . $e->getMessage() . " <br>";
}
//QUESTION TO TEACHER, why would I do this instead of just forcing it to only accept ints?
?>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.