0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PHP課題を渡せれたので解いてみた[メモ]

0
Last updated at Posted at 2021-06-14

面接終了後、PHPの課題を渡されたの解いてみました。

課題に必要な知識として

・if文
・API
・Json
・演算子
・オブジェクト指向
・コンストラクト
など

階層

ev001.JPG

問題1

以下の要件を満たすプログラム関数を作成してください。

概要
引数として渡された値から処理を実行し、指定する型の戻り値を出力してください。

関数
string test_func(int v1, int v2, int v3);

処理
0を下限、v3の値を上限とし、v1の値をv2乗して整数を文字列型で返してください。

index.php
<?php

namespace ta;

use ta\me\Integr;

require 'me\Integr.php';


 $a=new Integr();

 $a->test_func(0,1,7);


Integr.php
<?php

namespace ta\me;

class Integr
{
    function test_func(int $v1, int $v2, int $v3)
    {
        $total = $v1 * $v2; //v1の値をv2乗

        if ($total > $v3) {
            echo 'v3の値をまでにしてください';
        } elseif (0 > $total) {
            echo '$v1 * $v2を0以上にして下さい';
        } else {
            echo (strval($total)); //整数を文字列型に変換
        }
    }
}

問題2

郵便番号を引数として受け取って、APIにアクセスを行い得られた住所に関する情報を画面上に出力する関数を作成してください。
APIの使用方法は下記のドキュメント(URL)を参考にしてください。

■郵便番号検索API
https://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/zipcodesearch.html

住所に関する情報をなんでもいいんで取得してください。

index1.php
<?php
$postalCode = 5530001;
$url = "https://map.yahooapis.jp/search/zip/V1/zipCodeSearch?query=" . $postalCode . "&appid=dj00aiZpPVZ0ek43eXZrUkYyaiZzPWNvbnN1bWVyc2VjcmV0Jng9NTM-&output=json";

$resJson = file_get_contents($url); 
$resJson = mb_convert_encoding($resJson, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN'); 
$res = json_decode($resJson,true);

$address = $res['Feature'][0]["Property"]["Address"];
$a = $res['Feature'][0]["Property"]["Country"]["Name"];

print $address.'<br>';
print $a.'<br>';

問題3

class Person {
	// 名前
	public $name = '';
	// 生年月日(年/月/日)
	public $birthday = '';
	// 性別(m:男性, f:女性)
	public $gender = '';

	// 自己紹介文を生成
	public function selfIntroduction(){
		if($this->gender == 'm'){
			$gendata = '男性';
		}else if($this->gender == 'f'){
			$gendata = '女性';
		}else{
			$gendata = '[性別は不明]';
		}
		return 'わたしは' . $this->name . '、' . $this->birthday . '生まれ、' . $gendata . 'です。';
	}
}
  1. 上記のクラスに下記の機能を追加してください。
     (1)名前、生年月日、性別の情報を受け取りプロパティを初期化するコンストラクタを作成してください。

 (2)生年月日から年齢を算出し返す「getAge」メソッドを作成してください。

  1. 「1.」で作成したクラスを継承した「Profile」クラスを作成し「Profile」クラスに下記の機能を追加してください。
     (1)「hometown(出身地)」「hobby(趣味)」プロパティを作成しコンストラクタで初期化できるようにしてください。
     ※「Person」クラスのコンストラクタも機能させるように注意してください

 (2)「Person」クラスの「selfIntroduction」メソッドをオーバーライドし下記の文章を返してください。
  ※[]で囲ったものは該当するプロパティまたはメソッドから参照すること
  私の名前は[$name]です。
  [getAge()]才、[$gendata]です。
  出身は[$hometown]、趣味は[$hobby]です。

index2
.php
<?php

namespace ta;

use ta\me\Person;
use ta\me\Profile;

require 'me\Person.php';
require 'me\Profile.php';

//----------------Person

// $a = new Person("太郎", "1990/04/20", "m");
// $b=$a->selfIntroduction();
// echo $b;
// echo '<br>';


//------------------Profile

$profile = new Profile("太郎", "1990/04/20", "m","大阪","野球");
$pro=$profile->selfIntroduction();
echo $pro;

Preson.php
<?php

namespace ta\me;

class Person
{
    // 名前
    public $name = '';
    // 生年月日(年/月/日)
    public $birthday = '';
    // 性別(m:男性, f:女性)
    public $gender = '';

    public function __construct(string $name, string $birthday, string $gender)
    {
        $this->name = $name;
        $this->birthday = $birthday;
        $this->gender = $gender;
    }

    //年齢計算
    public function getAge(string $birthday)
    {
        $now = date("Ymd");
        $birthdate = str_replace("/", "", $birthday); //ハイフンを除去しています。
        return floor(($now - $birthdate) / 10000);
    }

    // 自己紹介文を生成
    public function selfIntroduction()
    {
        if ($this->gender == 'm') {
            $gendata = '男性';
        } else if ($this->gender == 'f') {
            $gendata = '女性';
        } else {
            $gendata = '[性別は不明]';
        }
        return 'わたしは' . $this->name . '、' . $this->birthday . '生まれ、' . $gendata . 'です。';
    }
}

Profile.php
<?php

namespace ta\me;

class Profile extends Person
{
    // 出身地
    public $hometown = '';
    // 趣味
    public $hobby = '';

    function __construct(string $name, string $birthday, string $gender, string $hometown, string $hobby)
    {
        parent::__construct($name, $birthday, $gender);

        $this->hometown = $hometown;
        $this->hobby = $hobby;
    }

    public function selfIntroduction()
    {
        if ($this->gender == 'm') {
            $gendata = '男性';
        } else if ($this->gender == 'f') {
            $gendata = '女性';
        } else {
            $gendata = '[性別は不明]';
        }
        return '私の名前は' . $this->name . 'です。' . $this->getAge($this->birthday) . '才、' . $gendata . 'です。出身は' . $this->hometown . '、趣味は' . $this->hobby . 'です。';
    }
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?