-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
165 lines (129 loc) · 3.88 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8" />
</head>
<body>
<?php
require 'config.php';
interface Chargeable {
public function getPrice();
}
class ShopProduct implements Chargeable {
const AVAILABLE = 0;
const OUT_OF_STOCK = 1;
private $title;
private $producerFirstName;
private $producerMainName;
protected $price;
private $publicationDate;
private $discount = 0;
private $id = 0;
public function __construct($title, $firstName, $mainName, $price, $publicationDate) {
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerMainName = $mainName;
$this->price = $price;
$this->publicationDate = $publicationDate;
}
public function setID($id) {
$this->id = $id;
}
public function getProducer(){
return $this->producerFirstName.' '. $this->producerMainName;
}
public function setDiscount($num){
$this->discount = $num;
}
public function getPrice(){
return ($this->price - $this->discount);
}
public function getPublicationDate(){
return $this->publicationDate;
}
public function getTitle(){
return $this->title;
}
public function getSummaryLine(){
$base = $this->title.' ('.$this->publicationDate.') - '.$this->getProducer();
return $base;
}
public static function getInstance($id, PDO $pdo){
$stmt = $pdo->prepare("SELECT * FROM products WHERE id=?");
$result = $stmt->execute(array($id));
$row = $stmt->fetch();
if (empty($row)){
return null;
}
if ($row['type'] === "książka") {
$product = new BookProduct($row['title'], $row['firstname'], $row['mainname'], $row['price'], $row['numpages']);
}
elseif ($row['type'] == "cd") {
$product = new CdProduct($row['title'], $row['firstname'], $row['mainname'], $row['price'], $row['playlength']);
}
else {
$product = new ShopProduct($row['title'], $row['firstname'], $row['mainname'], $row['price']);
}
$product->setId($row['id']);
$product->setDiscout($row['discount']);
return $product;
}
}
class CdProduct extends ShopProduct {
private $playLength = 0;
public function __construct($title, $firstName, $mainName, $price, $publicationDate, $playLenght){
parent::__construct($title, $firstName, $mainName, $price, $publicationDate);
$this->playLength = $playLenght;
}
public function getPlayLength(){
return $this->playLength;
}
public function getSummaryLine(){
$base = parent::getSummaryLine();
$base .= ': play length - '.$this->playLength;
return $base;
}
}
class BookProduct extends ShopProduct {
private $numPages = 0;
public function __construct($title, $firstName, $mainName, $price, $publicationDate, $numPages){
parent::__construct($title, $firstName, $mainName, $price, $publicationDate);
$this->numPages = $numPages;
}
public function getNumberOfPages(){
return $this->numPages;
}
public function getSummaryLine(){
$base = parent::getSummaryLine();
$base .= ': number of pages - '.$this->numPages;
return $base;
}
}
abstract class ShopProductWriter {
protected $products = array();
public function addProduct(ShopProduct $shopProduct){
$this->products[] = $shopProduct;
}
public function write(ShopProduct $shopProduct){
$str = '';
foreach ($this->products as $shopProduct) {
$str .= $shopProduct->getTitle().' - ';
$str .= $shopProduct->getProducer().' - ';
$str .= $shopProduct->getPublicationDate().' - ';
$str .= $shopProduct->getPrice().'$';
}
print $str.'<br>';
}
}
$book1 = new ShopProduct('Silmarillion', 'J.R.R.', 'Tolkien', '59.99', '15 September 1977', '365', null);
$book2 = new ShopProduct('The Fellowship of the Ring', 'J.R.R.', 'Tolkien', '49.99', '29 July 1954', '445', null);
$music1 = new ShopProduct('Dangerous', 'Michael', 'Jackson', '39.99', '26 November 1991', null, '77:00');
/*
$writer = new ShopProductWriter();
$writer->addProduct($music1);
$writer->write($music1);
*/
print ShopProduct::AVAILABLE;
?>
</body>
</html>