给出示例代码

时间:2016-01-27 02:42:26

标签: php oop

我正在努力完成任务。

我收到了一个文件并被告知要编写类和相应的函数来运行给我的示例代码。

我对Php并不熟悉。我正在寻求一些帮助我开始的指导。

以下是我给出的示例代码和相关输出:

<?php
include('Directory.php');
include('Entry.php');

$directory = new Directory("Staff Directory");

echo $directory->getName(); // prints "Staff Directory"

$billEntry = new Entry(array(
    "First Name" => "Bill",
    "Last Name" => "Hen",
    "Phone Number" => "876-330-1111",
    "Department" => "IT"
));

$directory->addEntry($billEntry);

$results = $directory->findByLastName("Hen");

foreach ($results as $result) {
    print $result->getPhoneNumber();
}
// the above prints "876-330-1111"
$stanEntry = new Entry(array(
    "First Name" => "Stanley",
    "Last Name" => "Little",
    "Phone Number" => "112-334-5565",
    "Department" => "IT"
));

$directory->addEntry($stanEntry);
echo $directory->getNumberOfEntries(); // prints 2
echo $directory; // should print all of the information in the directory so far, including the directory title
echo count($directory->findByDepartment("IT")); // prints 2
$directory->removeEntry($billEntry);
echo $directory->getNumberOfEntries(); // prints 1

以下是我认为getName()函数对于Directory类应该是这样的:

public function getName() {
    $this->name;
}

这足以使该功能发挥作用吗?如果没有,我错过了什么?

我主要需要有关如何处理条目存储的帮助。我认为它使用数组,但是如何?

我尝试了很多网络资源,但是现在,我被困住了。请帮助新手。我真的很感激。

编辑:成品

<?php
class DirectoryA {
    private $name;
    private $entries;

    public function __construct($name) {
        $this->name = $name;   
    }

    public function __toString() {
            $print = $this->name;
            $print .= "\n--------------------";
        foreach ($this->entries as $entry) {
            $print .= sprintf("\nFirst Name: %s\n", $entry->getFirstName());
            $print .= sprintf("Last Name: %s\n", $entry->getLastName());
            $print .= sprintf("Phone Number: %s\n", $entry->getPhoneNumber());
            $print .= sprintf("Department: %s", $entry->getDept());
            $print .= "\n--------------------------";
        }
        return $print;
     }

    public function getName() {
        return $this->name;
    }

    public function addEntry($person) {
        $this->entries[] = $person;
    }

    public function findByLastName($lastName) {
        $matches = array();
        foreach ($this->entries as $entry) {
            if ($entry->getLastName() == $lastName)
                $matches[] = $entry;
        }
        return $matches;
    }

    public function findByDepartment($depart) {
        $depMatch = array();
        foreach ($this->entries as $entry) {
            if ($entry->getDept() == $depart)
                $depMatch[] = $entry;
        }
        return $depMatch;
    }

    public function getNumberOfEntries() {
        $numOfEntries = 0;
        foreach ($this->entries as $entry) {
            $numOfEntries += $entry;
        }
        return $numOfEntries;
    }

    public function removeEntry($person) {
        unset($this->entries[0]);
    }
}//ends DirectoryA class

/ ------------------------------------------- --- /

<?php

class Entry {
    private $lN;
    private $fN;
    private $phone;
    private $dept;

    public function __construct($data) {
        $this->fN = $data['First Name'];
        $this->lN = $data['Last Name'];
        $this->phone = $data['Phone Number'];
        $this->dept = $data['Department'];
    }

    public function getFirstName() {
        return $this->fN;
    }

    public function getPhoneNumber() {
        return $this->phone;
    }

    public function getLastName(){
        return $this->lN;
    }

    public function getDept() {
        return $this->dept;
    }

}

我会说这个练习肯定帮助我理解了课程和范围。我希望它能以某种方式帮助其他人。

1 个答案:

答案 0 :(得分:0)

您的条目实体和目录类可能如下所示:

<?php
namespace Foo;

class Person
{
    private $first_name;

    public function __construct($data)
    {
        if(isset($data['First Name']))
            $this->first_name = $data['First Name'];
    }

    public function getFirstName()
    {
        return $this->first_name;
    }
}

class People
{
    // This will be an array of persons
    private $people;

    public function addPerson(Person $person)
    {
        $this->people[] = $person;
    }

    public function findByFirstName($first_name)
    {
        $results = array();
        if(count($this->people)) {
            foreach($this->people as $key => $person) {
                if($person->getFirstName() == $first_name) {
                    $results[] = $person;
                }
            }
        }

        return $results;
    }

}

$foo = new Person(array(
    "First Name" => "Foo",
));

var_dump($foo);

$people = new People;
$people->addPerson($foo);

var_dump($people->findByFirstName('Foo'));

这应该会给你一个良好的开端。

打印条目:

function entry_to_string($entry)
{
    $o  = sprintf("First Name: %s\n",   $entry->getFirstName());
    $o .= sprintf("Last Name: %s\n",    $entry->getLastName());
    $o .= sprintf("Department: %s\n",   $entry->getDepartment());
    $o .= sprintf("Phone Number: %s\n", $entry->getPhoneNumber());

    return $o;
}