Solved

I added method EventCostStrategy to charge based upon type of lesson

Posted on 2014-04-21
12
116 Views
Last Modified: 2014-05-06
<?php
abstract class Lesson {
    private   $duration;
    private   $costStrategy;

    function __construct( $duration, CostStrategy $strategy ) {
        $this->duration = $duration;
        $this->costStrategy = $strategy;
    }

    function cost() {
        return $this->costStrategy->cost( $this );
    }

    function chargeType() {
        return $this->costStrategy->chargeType( );
    }

    function getDuration() {
        return $this->duration;
    }

    // more lesson methods...
}


abstract class CostStrategy {
    abstract function cost( Lesson $lesson );
    abstract function chargeType();
}

class TimedCostStrategy extends CostStrategy {
    function cost( Lesson $lesson ) {
        return ( $lesson->getDuration() * 5 );
    }

    function chargeType() {
        return "hourly rate";
    }
}

class FixedCostStrategy extends CostStrategy {
    function cost( Lesson $lesson ) {
        return 30;
    }

    function chargeType() {
        return "fixed rate";
    }
}

class EventCostStrategy extends CostStrategy{
  function __get($event){
    $this->event=$event;
  }
  
  
  function cost(Lesson $lesson){
    return $this->event;
  }
  function chargeType(){
    return 'event rate';
  }
}

class Lecture extends Lesson {
    // Lecture-specific implementations ...
}

class Seminar extends Lesson {
    // Seminar-specific implementations ...
}

class RegistrationMgr {
    function register( Lesson $lesson ) {
        // do something with this Lesson

        // now tell someone
        $notifier = Notifier::getNotifier();
        $notifier->inform( "new lesson: cost ({$lesson->cost()})" );
    }
}

abstract class Notifier {
    
    static function getNotifier() {
        // acquire concrete class according to 
        // configuration or other logic

        if ( rand(1,2) === 1 ) {
            return new MailNotifier();
        } else {
            return new TextNotifier();
        }
    }

    abstract function inform( $message );
}

class MailNotifier extends Notifier {
    function inform( $message ) {
        print "MAIL notification: {$message}<br>";
    }
}

class TextNotifier extends Notifier {
    function inform( $message ) {
        print "TEXT notification: {$message}<br>";
    }
} 
$lessons1 = new Seminar( 4, new TimedCostStrategy() );
$lessons2 = new Lecture( 4, new FixedCostStrategy() );
$lessons3 = new Lecture('private tutor', new EventCostStrategy());
$mgr = new RegistrationMgr();
$mgr->register( $lessons1 );
$mgr->register( $lessons2 );
$mgr->register( $lessons3 );

?>

Open in new window



I added
class EventCostStrategy extends CostStrategy{

I was thinking
private tutor =45
group class=15


but I do not know how to pass variable to class EventCostStrategy

I tried

 function __get($event){
    $this->event=$event;
  }
0
Comment
Question by:rgb192
  • 6
  • 6
12 Comments
 
LVL 33

Expert Comment

by:Slick812
ID: 40015375
greetings  rgb192, , I looked at your code for the -
class EventCostStrategy

and you seem to think that because you the PHP declaration of "extends CostStrategy" as in - the way you define it -
class EventCostStrategy  extends CostStrategy {

That you can not add anything else (properties, methods) to the parent class of - "CostStrategy" - , , but in any "child" class you can add anything to it that you want to, So I ask you WHY did you not write your new class as -
class EventCostStrategy extends CostStrategy{
  public $event = "my Event Value";// or $event = 45;
  public $rate = 15;
  
  
  function cost(Lesson $lesson){
    return $this->event;
  }
  function chargeType(){
    return 'event rate is '.$this->rate;
  }
}

Open in new window

I have added TWO Properties, AND I could have added more methods as NEEDED to have this class do what I need to do, BUT you absolutely need to have TWO methods in this , cost() and chargeType().
The way that you use the -
function __get($event){
is not workable in this way to do things.

I am not to sure about your ask - "pass variable to class", you could have a Class __construct( ) like this

  public function __construct($event1) {
    $this->event = $event1;
  }
OR add any method to use or set the $this->event, or just change the property o de Object -
$EventCostStrategyObject->event = "some other Event";
 

Ask questions if you do not see that by Extending a CLASS , you still can "Customize: and add to that Class definition.
0
 

Author Comment

by:rgb192
ID: 40018843
private tutor =45
group class=15


could you modify your code to have an if/or statement $15 or $45
0
 
LVL 33

Expert Comment

by:Slick812
ID: 40018979
you say - "could you modify your code to have an if/or statement $15 or $45 "

I can not see at all what you may mean by this statement. you give two things -
private tutor =45
group class=15

which are meaningless to me for any code. as in NONE of code is there anything that might correspond to "private tutor", , AND as you have in your class some $event used as $this->event , I do not see any relevance to any "Functioning" in these Classes for any sort of EVENT.

You seem to have missed the point of this lesson, if you can not build a Class that extends CostStrategy  that has both a "Fixed Rate" for "group class"  AND a  "hourly rate"  for the  "private tutor". In your code -
class EventCostStrategy extends CostStrategy{
  function __get($event){
    $this->event=$event;
  }
  
  
  function cost(Lesson $lesson){
    return $this->event;
  }
  function chargeType(){
    return 'event rate';
  }
}

Open in new window

You do not have ANY additions to do any sort of code work! !
first of all you will need to "SET" the type of instruction as "group" or "tutor", then you will need a CLASS property to hold the "type of instruction" set. (hint make a  -
 function __construct($courseType = "group") {
to set the "type of instruction" )

I should not have to tell you this but , I will start you out wid Hint-
In your Class method of cost( ) you will need to Multiply the "duration" of the "tutoring" by 45 ONLY IF the "type of instruction" is "tutor", as like -
        return ( $lesson->getDuration() * 45 );

Also you will need to GIVE the KIND Rate as "hourly rate"  or  "Fixed Rate" in your chargeType()  method.
0
 

Author Comment

by:rgb192
ID: 40019135
function __construct($courseType = "group") {

so $courseType="group" is default if empty object call

$this->courseType=$courseType

if ($this->courseType=="tutor"){
return "tutor";
}else{
return "group";
}


because there is now a constructor
should this
$lessons3 = new Lecture('private tutor', new EventCostStrategy());
change to
$lessons3 = new Lecture(new EventCostStrategy('private tutor'));
$lessons3 = new Lecture(new EventCostStrategy('group'));



and should there be if statement in cost() method
if ($this->courseType=="tutor"){
 return ( $lesson->getDuration() * 45 );
}else{
 return ( $lesson->getDuration() * 15 );
}
0
 
LVL 33

Expert Comment

by:Slick812
ID: 40019243
yes this -
     $this->courseType=$courseType;
will set the course in your object in the constuctor


as to this code -
     if ($this->courseType=="tutor"){
       return "tutor";
       }else{
       return "group";
       }
NO, NO, you are not looking at WHAT you need this Class to do, the above code does not have any purpose as far as I can tell, so it is Wrong.

as to this code -
$lessons3 = new Lecture('private tutor', new EventCostStrategy());

IT IS WRONG! , not because of anything that has to do with the "EventCostStrategy" Class, but because of this parameter 'private tutor', that is INCORRECT!
If you are going to code this thing you need to look at what you are coding, look at the "Lecture" which is an extention of the Lesson Class, see below-

abstract class Lesson {
    private   $duration;
    private   $costStrategy;

// LOOK at the first parameter, It's $duration  !
    function __construct( $duration, CostStrategy $strategy ) {
        $this->duration = $duration;// How many HOURS of Lessons
        $this->costStrategy = $strategy;
    }

= = = = =
the $duration can NOT be 'private tutor' , it needs to be the HOURS of the Lecture as 5 or 3 .

this is also INCORRECT -
       $lessons3 = new Lecture(new EventCostStrategy('private tutor'));
because there is NO $duration , so it will be a fatal error!
ALSO you may should use 'tutor' instead of 'private tutor'

= = = = = = = =

this next code is Wrong for cost() -
     if ($this->courseType=="tutor"){
        return ( $lesson->getDuration() * 45 );
        }else{
        return ( $lesson->getDuration() * 15 );
        }

Please look at the TWO examples, the one for TimedCostStrategy uses
       return ( $lesson->getDuration() * 5 );

the one for FixedCostStrategy uses -
       return 30;

can you see the difference? the "group Class" is a FixedCost, , and the "private tutor" is a TimedCost
0
 

Author Comment

by:rgb192
ID: 40021868
$lessons3 = new Lecture('private tutor', new EventCostStrategy());
so would there be a third parameter because time,method needs a third for type of eventCostStrategy
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 33

Expert Comment

by:Slick812
ID: 40023105
????
you say - "so would there be a third parameter because time,method needs a third for type of eventCostStrategy"

Sorry your statement above really has NO MEANING! !
 I have already tried to Show You in my last post that this line of code-
     $lessons3 = new Lecture('private tutor', new EventCostStrategy());

is entirely INCORRECT.
You seem to have real trouble in learning the most basic programming concepts, I can not respond to - "so would there be a third parameter because time,method needs a third for type of eventCostStrategy", because you reference an incorrect line of code -
    $lessons3 = new Lecture('private tutor', new EventCostStrategy());
I told you that the $duration can NOT be 'private tutor' , it needs to be the HOURS of the Lecture as a "Number an Integer", ,  a small integer that a normal person can sit for instruction - - likely 1 to 5 hours.
Sorry but I did not mention the you also had an incorrect Class of Lecture, in that code.
so an attempt at showing you some possible correct code would be this series -
$lessons1 = new Seminar( 4, new TimedCostStrategy() );
$lessons2 = new Lecture( 4, new FixedCostStrategy() );
  //the two code lines below are for your EventCostStrategy CLASS
  // the Seminar class is for TimedCost, the Lecture class for FixedCost
$lessons3 = new Seminar( 5, new EventCostStrategy("tutor") );
$lessons4 = new Lecture( 3, new EventCostStrategy("group") );
$mgr = new RegistrationMgr();
$mgr->register( $lessons1 );
$mgr->register( $lessons2 );
$mgr->register( $lessons3 );
$mgr->register( $lessons4 );

Open in new window

Please rewrite your EventCostStrategy Class code to have a "mixed" or "dependent" code for a flexible implementation able to use either a Seminar with a TimedCost OR a Lecture with a FixedCost setup. You may need to try an understand why they show this separation for the different TimedCost or FixedCost classes in Lecture and Seminar.
0
 

Author Comment

by:rgb192
ID: 40039557
$lessons3 = new Seminar( 5, new EventCostStrategy("tutor") );
$lessons4 = new Lecture( 3, new EventCostStrategy("group") );

or more simple:

 new EventCostStrategy("tutor") );
 new EventCostStrategy("group") );


class EventCostStrategy{
public __construct($tutorOrGroup){
foreach ($tutorOrGroup as $key=>$value){
$this->courseType=$key;
}

}

     if ($this->courseType=="tutor"){
//I do not know what return
        return ( $lesson->getDuration() * 45 );
        }else{
//I do not know what return
        return ( $lesson->getDuration() * 15 );
        }



}
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
ID: 40042601
I have a problem understanding your code, in your  __construct( ) , ,  you now have a foreach( ) loop , which is just incorrect, if you create your objects with -
     new EventCostStrategy("tutor")
     new EventCostStrategy("group")

you then say -
     if ($this->courseType=="tutor"){
     //I do not know what return
        return ( $lesson->getDuration() * 45 );

in this "return" there are no stated requirements in the code comments, and I am going by what is in the TimedCostStrategy and FixedCostStrategy classes, but
I have tried to give you some help with this, but you do not seem to understand what you are writing your code for, . . . .
Below is some really simple code that goes back to your use of the -
return $this->event;

I have used your first code for the EventCostStrategy except I have added the public $event  AND added the  function __construct($event = 15) so you can initialize this with a Numeric (number) value for the amount Charged for an Event.
class EventCostStrategy extends CostStrategy{
  public $event;

public function __construct($event = 15) {
  $this->event = $event;
  }
  
  function cost(Lesson $lesson){
    return $this->event;
  }
  function chargeType(){
    return 'event rate';
  }
}

Open in new window

in case you do not understand what this code is set up to do , here is the code you need to use this -
   // place the Event "cost" as a number for $event, I use 45 below
$lessons3 = new Lecture(4, new EventCostStrategy(45));
$mgr = new RegistrationMgr();
$mgr->register( $lessons3 );

Open in new window

This maybe will show you something, but I have simplified it so there is not much code to look at, and hopefully you can just copy paste this code and run it on your server and understand what it is doing.
0
 

Author Comment

by:rgb192
ID: 40043537
changing the get to a constructor thank you for teaching me.

?php
abstract class Lesson {
    private   $duration;
    private   $costStrategy;

    function __construct( $duration, CostStrategy $strategy ) {
        $this->duration = $duration;
        $this->costStrategy = $strategy;
    }

    function cost() {
        return $this->costStrategy->cost( $this );
    }

    function chargeType() {
        return $this->costStrategy->chargeType( );
    }

    function getDuration() {
        return $this->duration;
    }

    // more lesson methods...
}


abstract class CostStrategy {
    abstract function cost( Lesson $lesson );
    abstract function chargeType();
}

class TimedCostStrategy extends CostStrategy {
    function cost( Lesson $lesson ) {
        return ( $lesson->getDuration() * 5 );
    }

    function chargeType() {
        return "hourly rate";
    }
}

class FixedCostStrategy extends CostStrategy {
    function cost( Lesson $lesson ) {
        return 30;
    }

    function chargeType() {
        return "fixed rate";
    }
}

class EventCostStrategy extends CostStrategy{
  public $event;

public function __construct($event = 15) {
  $this->event = $event;
  }
  
  function cost(Lesson $lesson){
    return $this->event;
  }
  function chargeType(){
    return 'event rate';
  }
}


class Lecture extends Lesson {
    // Lecture-specific implementations ...
}

class Seminar extends Lesson {
    // Seminar-specific implementations ...
}

class RegistrationMgr {
    function register( Lesson $lesson ) {
        // do something with this Lesson

        // now tell someone
        $notifier = Notifier::getNotifier();
        $notifier->inform( "new lesson: cost ({$lesson->cost()})" );
    }
}

abstract class Notifier {
    
    static function getNotifier() {
        // acquire concrete class according to 
        // configuration or other logic

        if ( rand(1,2) === 1 ) {
            return new MailNotifier();
        } else {
            return new TextNotifier();
        }
    }

    abstract function inform( $message );
}

class MailNotifier extends Notifier {
    function inform( $message ) {
        print "MAIL notification: {$message}<br>";
    }
}

class TextNotifier extends Notifier {
    function inform( $message ) {
        print "TEXT notification: {$message}<br>";
    }
} 


   // place the Event "cost" as a number for $event, I use 45 below
$lessons3 = new Lecture(4, new EventCostStrategy(45));
$lessons4 = new Lecture(5, new EventCostStrategy(15) );
$lessons5 = new Seminar(14, new EventCostStrategy(15));
$mgr = new RegistrationMgr();
$mgr->register( $lessons3 );
$mgr->register($lessons4);
$mgr->register($lessons5);

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 40043538
thanks.
I learned alot from code especially constructor instead of get
0
 
LVL 33

Expert Comment

by:Slick812
ID: 40044897
I will tell you some more, the Class "constructor" as -
      public function __construct( )

is a very very Useful and Important way to have a Class do "Special" and "Different" output for your web pages according to the Values you place in the parameters - in this one -
      new EventCostStrategy(45)
you have a Cost output of 45, and in this one -
      new EventCostStrategy(15)
you have a Cost output of 15


There is an example in this code, please see this part o code -
    abstract class Lesson {
        private   $duration;
        private   $costStrategy;

        function __construct( $duration, CostStrategy $strategy ) {
            $this->duration = $duration;
            $this->costStrategy = $strategy;
        }

this  function __construct( $duration, CostStrategy $strategy )    has TWO parameters, one is for the length of time that a "Lesson" will last as $duration, a Number like 5,
the next is for the Way that the COST for a lesson is calculated, as input parameter $strategy . You did not seem to understand this at all, likely because you did not try look at this code and see what "functioning" it is doing and HOW different inputs for the $duration and $strategy can Change the Outputs in the the Page echo (print) here -
      print "MAIL notification: {$message}<br>";
which is in this code -
      class MailNotifier extends Notifier {
          function inform( $message ) {
              print "MAIL notification: {$message}<br>";
          }
       }

unfortunately for you, as a beginner, the person that wrote this tutorial tried to have an example for "ABSTRACT", and the code here the "class methods" are so distant, separated and complex, that even experienced PHP coders can not understand the the many "associations-links" that are used here for the TEN different Classes used to just print one line of browser output. Class here are -

1. abstract class Lesson {
2. abstract class CostStrategy {
3. class TimedCostStrategy extends CostStrategy {
4. class FixedCostStrategy extends CostStrategy {
5. class Lecture extends Lesson {
6. class Seminar extends Lesson {
7. class RegistrationMgr {
8. abstract class Notifier {
9. class MailNotifier extends Notifier {
10. class TextNotifier extends Notifier {

This is the sort of "complexity" that many "ABSTRACT" setups have, but because PHP is a Typeless Variable Language, you will probably NEVER NEED to use "ABSTRACT", and even very experienced PHP coders will never understand the setup of a useful Abstract PHP Class structure.

As a beginner you can not understand much of anything in this lesson, because it is so complex, it is a waist of your time. I am glad that you tried to do code for your new class here, and got something out of this. I have learned much by using a Class constructor with many different parameters (inputs).
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now