PHP object oriented - Exercise Coffee

0

Dear, I am a bit confused with this PHP exercise.

Develop a Coffee class with attributes _Maximum capacity (the amount maximum coffee that the coffee machine can contain) and _quantityCurrent (the amount current coffee in the coffee maker).

Implement, at least, the following methods:

  • Default Constructor: Sets the maximum capacity to 1000 (c.c.)
    and the current one at zero (empty coffee maker).
  • fillCafe (): because that, makes the current amount equal to the capacity.
  • serveTaza (int): simulates the action of serving a cup with the capacity indicated If the current amount of coffee "is not enough" to fill the cup, It serves what is left.
  • emptyCafe (): sets the current coffee quantity to zero.
  • addCafe (int): add the amount of coffee indicated to the coffee maker.

At the moment, what I have is this and but it gives me errors

<?php

class Cafetera {
    public $capacidadMaxima;
    public $CantidadActual;

    public function __construct($capacidadMaxima,$CantidadActual)
    {
        $this->$capacidadMaxima = $capacidadMaxima;
        $this->CantidadActual = $CantidadActual;
    }

    public function llenarCafetera()
    {
        $this->CantidadActual = capacidadMaxima; 
    }   

    public function servirTaza($LlenarTaza)
    {
        if($LlenarTaza > $this->CantidadActual) {
            echo 'No alcanza para una taza. Recargá la cafetera!';
        } else {
            $this->CantidadActual -= $LlenarTaza;
        }
    }


    public function vaciarCafetera()
    {
        $this->CantidadActual = 0; 
    }   

    public function agregarCafe($rellenar)
    {
        $this->CantidadActual = $rellenar; 
    }   

    public function Mostrar()
    {
        echo 'Capacidad Máxima: '. $capacidadMaxima.'<br>';
        echo 'Capacidad Actual: '. $CantidadActual.'<br>';
    }
}

$cafetera = new Cafetera(1000,0);
$cafetera->servirTaza(200);
$cafetera->agregarCafe(150);
$cafetera->mostrar();

?>
    
asked by CocoBD 16.05.2017 в 01:59
source

2 answers

1

This is your corrected code:

<?php

class Cafetera {
    private $capacidadMaxima;
    private $cantidadActual;

    public function __construct() {
        $this->capacidadMaxima = 1000;
        $this->cantidadActual = 0;
    }

    public function llenarCafetera(){
        $this->cantidadActual = $this->capacidadMaxima; 
    }   

    public function servirTaza($capacidadTaza) {
        if($capacidadTaza > $this->cantidadActual) {
            echo 'No alcanza para una taza. Recargá la cafetera!<br>';
        } else {
            $this->cantidadActual -= $capacidadTaza;
        }
    }


    public function vaciarCafetera() {
        $this->cantidadActual = 0; 
    }   

    public function agregarCafe($rellenar) {
        // esto agrega café por lo tanto debe ser +=
        $this->cantidadActual += $rellenar;

        // si se excede, dejar en el maximo
        if($this->cantidadActual > $this->capacidadMaxima){
            $this->cantidadActual = $this->capacidadMaxima;
        }
    }   

    public function mostrar() {
        echo 'Capacidad Máxima: ' . $this->capacidadMaxima . '<br>';
        echo 'Capacidad Actual: ' . $this->cantidadActual . '<br>';
    }
}

$cafetera = new Cafetera(1000,0);
$cafetera->servirTaza(200);
$cafetera->agregarCafe(150);
$cafetera->mostrar();

?>

As the previous answer says, you are missing some this . Whenever you refer to an attribute of the class from the same class you must prefix the this . You can read it in the documentation.

The other thing is that I understand that your requirement should use a contructor without parameters, since says:

  

Default Constructor: Sets the maximum capacity to 1000 (c.c.)   and the current one at zero (empty coffee maker).

I've also changed the visibility from your attributes to private and the agregarCafe() method .

    
answered by 16.05.2017 в 02:44
0

you're missing the $this->

here:

public function llenarCafetera()
    {
    $this->CantidadActual = $this->capacidadMaxima; 
    }   

and here:

public function Mostrar()
    {
    echo 'Capacidad Máxima: '. $this->capacidadMaxima.'<br>';
    echo 'Capacidad Actual: '. $this->CantidadActual.'<br>';
     }
    
answered by 16.05.2017 в 02:37