From 4eea47cb5c4028985c4e40c5a8c422c56923b839 Mon Sep 17 00:00:00 2001 From: eric2861994 Date: Fri, 2 Jan 2015 23:18:02 +0700 Subject: [PATCH 1/3] MVC with PHP --- mvc.php | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 mvc.php diff --git a/mvc.php b/mvc.php new file mode 100644 index 0000000..f0c30ad --- /dev/null +++ b/mvc.php @@ -0,0 +1,103 @@ +controller = $controller; + $this->model = $model; + $this->part1 = ' + +Fuel Calculator + + +

Liters per 100 kilometers calculator

+
+ + + + + +
Fuel (in liters)part2 = '/>
Journey Driven (in kilometers)part3 = '/>
+ + +
'; + $this->part4 = ''; + } + + public function toString() { + $res = $this->part1 . $this->part2 . $this->part3; + if ($this->model->isEnabled()) { + $res = $res . '

Average fuel consumption = '.$this->model->getResult(); + $res = $res . ' liters/100 kilometers

'.$this->part4; + } else { + $res = $res . $this->part4; + } + return $res; + } +} + +class Model +{ + private $enabled; + private $fuel; + private $kilo; + private $result; + + public function __construct() { + $this->enabled = false; + } + + public function calculate($fuel, $kilo) { + $this->fuel = $fuel; + $this->kilo = $kilo; + $this->result = $fuel/$kilo; + $this->enabled = true; + } + + public function getResult() { + return $this->result; + } + + public function getFuel() { + return $this->fuel; + } + + public function getKilo() { + return $this->kilo; + } + + public function isEnabled() { + return $this->enabled; + } +} + +class Controller +{ + private $model; + + public function __construct($model) { + $this->model = $model; + } + + public function submitted($fuel, $kilo) { + $this->model->calculate($fuel, $kilo); + } +} +?> + +submitted($_GET['fuel'], $_GET['kilo']); + } + + echo $view->toString(); +?> \ No newline at end of file From d5a87d82b5454160aaa05e90a7924650d69d69f2 Mon Sep 17 00:00:00 2001 From: eric2861994 Date: Fri, 2 Jan 2015 23:25:42 +0700 Subject: [PATCH 2/3] Standarized HTML --- mvc.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mvc.php b/mvc.php index f0c30ad..6e58f69 100644 --- a/mvc.php +++ b/mvc.php @@ -8,8 +8,10 @@ class View public function __construct($controller, $model) { $this->controller = $controller; $this->model = $model; - $this->part1 = ' + $this->part1 = ' + + Fuel Calculator From 70a9f60d468bfdcf002fa02ef7cf63e254cefef9 Mon Sep 17 00:00:00 2001 From: eric2861994 Date: Mon, 5 Jan 2015 18:06:29 +0700 Subject: [PATCH 3/3] redesigned code --- classes/average_fuel_controller.php | 14 ++++ classes/average_fuel_model.php | 24 +++++++ index.php | 42 +++++++++++ mvc.php | 105 ---------------------------- 4 files changed, 80 insertions(+), 105 deletions(-) create mode 100644 classes/average_fuel_controller.php create mode 100644 classes/average_fuel_model.php create mode 100644 index.php delete mode 100644 mvc.php diff --git a/classes/average_fuel_controller.php b/classes/average_fuel_controller.php new file mode 100644 index 0000000..700af11 --- /dev/null +++ b/classes/average_fuel_controller.php @@ -0,0 +1,14 @@ +model = $model; + } + + public function calculate($fuel, $kilo) { + $this->model->calculate($fuel, $kilo); + } +} +?> \ No newline at end of file diff --git a/classes/average_fuel_model.php b/classes/average_fuel_model.php new file mode 100644 index 0000000..b32c952 --- /dev/null +++ b/classes/average_fuel_model.php @@ -0,0 +1,24 @@ +enabled = false; + } + + public function calculate($fuel, $km) { + $this->liter_per_100km = $fuel*100/$km; + $this->enabled = true; + } + + public function getResult() { + return $this->liter_per_100km; + } + + public function isEnabled() { + return $this->enabled; + } +} +?> \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..b279490 --- /dev/null +++ b/index.php @@ -0,0 +1,42 @@ +calculate($_GET['fuel'], $_GET['kilo']); + } +?> + + + + + +Fuel Calculator + + + + +

Liters per 100 kilometers calculator

+
+ + + + + +
Fuel (in liters)
Journey Driven (in kilometers)
+ + + +
+ +isEnabled()) { + echo '

Average fuel consumption = ' . $model->getResult() . + ' liters/100 kilometers

'; + } +?> + + \ No newline at end of file diff --git a/mvc.php b/mvc.php deleted file mode 100644 index 6e58f69..0000000 --- a/mvc.php +++ /dev/null @@ -1,105 +0,0 @@ -controller = $controller; - $this->model = $model; - $this->part1 = ' - - - -Fuel Calculator - - -

Liters per 100 kilometers calculator

-
- - - - - -
Fuel (in liters)part2 = '/>
Journey Driven (in kilometers)part3 = '/>
- - -
'; - $this->part4 = ''; - } - - public function toString() { - $res = $this->part1 . $this->part2 . $this->part3; - if ($this->model->isEnabled()) { - $res = $res . '

Average fuel consumption = '.$this->model->getResult(); - $res = $res . ' liters/100 kilometers

'.$this->part4; - } else { - $res = $res . $this->part4; - } - return $res; - } -} - -class Model -{ - private $enabled; - private $fuel; - private $kilo; - private $result; - - public function __construct() { - $this->enabled = false; - } - - public function calculate($fuel, $kilo) { - $this->fuel = $fuel; - $this->kilo = $kilo; - $this->result = $fuel/$kilo; - $this->enabled = true; - } - - public function getResult() { - return $this->result; - } - - public function getFuel() { - return $this->fuel; - } - - public function getKilo() { - return $this->kilo; - } - - public function isEnabled() { - return $this->enabled; - } -} - -class Controller -{ - private $model; - - public function __construct($model) { - $this->model = $model; - } - - public function submitted($fuel, $kilo) { - $this->model->calculate($fuel, $kilo); - } -} -?> - -submitted($_GET['fuel'], $_GET['kilo']); - } - - echo $view->toString(); -?> \ No newline at end of file