class Buyer {
var $money = 1000,
$cart = array(), // 구입한 제품을 저장하기 위한 배열
$i = 0; // Product배열 cart에 사용될 index
function buy(Product $p) {
if ($p->price > $this->money) {
println('잔액이 부족하여 ' . $p . '를 살수 없습니댜.');
return;
}
$this->money -= $p->price;
$this->add($p);
/*
가진 돈과 물건의 가격을 비교해서 가진 돈이 적으며 종료한다.
가진 돈이 충분하면, 제품의 가격을 가진 돈에서 빼고
장바구니에 구입한 물건을 담는다. (add메서드 호출)
*/
}
function add(Product $p) {
$this->cart[$this->i] = $p;
$this->i++;
/*
물건을 장바구니(cart)에 저장한다. 그리고 i의 값을 1 증가시킨다.
*/
} // add(Product p)
function summary() {
$itemList = '';
$sum = 0;
for ($i=0; $i<count($this->cart); $i++) {
$this->itemList .= $this->cart[$i] . ',';
$this->sum += $this->cart[$i]->price;
}
println('구입한 물건 : ' . $this->itemList);
println('사용한 금액 : ' . $this->sum);
println('남은 금액 : ' . $this->money);
/*
장바구니에 담긴 물건들의 목록을 만들어 출력한다.
장바구니에 담긴 물건들의 가격을 모두 더해서 출력한다.
물건을 사고 남은 금액(money)를 출력한다.
*/
} // summary()
}
class Product {
var $price; // 제품의 가격
function __construct($price) {
$this->price = $price;
}
}
class Tv extends Product {
function __construct() {
Parent::__construct(100);
}
public function __toString() {
return 'Tv';
}
}
class Computer extends Product {
function __construct() {
Parent::__construct(200);
}
public function __toString() {
return 'Computer';
}
}
class Audio extends Product {
function __construct() {
Parent::__construct(50); }
public function __toString() {
return 'Audio';
}
}
$b = new Buyer();
$b->buy(new Tv());
$b->buy(new Computer());
$b->buy(new Tv());
$b->buy(new Audio());
$b->buy(new Computer());
$b->buy(new Computer());
$b->buy(new Computer());
$b->summary();
// 줄바꿈 출력
function println($arg) {
echo $arg.'<br />';
}
/* 출력
잔액이 부족하여 Computer를 살수 없습니댜.
구입한 물건 : Tv,Computer,Tv,Audio,Computer,Computer,사용한 금액 : 850
남은 금액 : 150
*/
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 230 | 20년 전 | 2066 | ||
| 229 | 20년 전 | 3139 | ||
| 228 | 20년 전 | 3325 | ||
| 227 | 20년 전 | 2397 | ||
| 226 | 20년 전 | 5480 | ||
| 225 | 20년 전 | 2524 | ||
| 224 | 20년 전 | 2970 | ||
| 223 | 20년 전 | 4207 | ||
| 222 | 20년 전 | 2606 | ||
| 221 | 20년 전 | 2290 | ||
| 220 | 20년 전 | 3679 | ||
| 219 | 20년 전 | 2074 | ||
| 218 | 20년 전 | 3566 | ||
| 217 | 20년 전 | 2482 | ||
| 216 | 20년 전 | 2912 | ||
| 215 | 20년 전 | 2227 | ||
| 214 | 20년 전 | 3338 | ||
| 213 | 20년 전 | 2919 | ||
| 212 | 20년 전 | 3053 | ||
| 211 | 20년 전 | 2148 | ||
| 210 | 20년 전 | 1897 | ||
| 209 | 20년 전 | 2358 | ||
| 208 | 20년 전 | 1980 | ||
| 207 | 20년 전 | 1677 | ||
| 206 | 20년 전 | 1855 | ||
| 205 | 20년 전 | 3952 | ||
| 204 | 20년 전 | 1677 | ||
| 203 | 20년 전 | 2011 | ||
| 202 | 20년 전 | 2360 | ||
| 201 | 20년 전 | 1828 | ||
| 200 | 20년 전 | 2957 | ||
| 199 | 20년 전 | 2005 | ||
| 198 | 20년 전 | 2088 | ||
| 197 | 20년 전 | 3655 | ||
| 196 | 20년 전 | 2985 | ||
| 195 | 20년 전 | 2095 | ||
| 194 | 20년 전 | 10251 | ||
| 193 | 20년 전 | 2248 | ||
| 192 | 20년 전 | 1605 | ||
| 191 | 20년 전 | 2669 | ||
| 190 | 20년 전 | 2293 | ||
| 189 | 20년 전 | 1686 | ||
| 188 | 20년 전 | 1482 | ||
| 187 | 20년 전 | 1908 | ||
| 186 | 20년 전 | 1723 | ||
| 185 | 20년 전 | 1770 | ||
| 184 | 20년 전 | 2352 | ||
| 183 | 20년 전 | 1562 | ||
| 182 | 20년 전 | 1487 | ||
| 181 | 20년 전 | 1626 | ||
| 180 | 20년 전 | 2723 | ||
| 179 | 20년 전 | 1808 | ||
| 178 | 20년 전 | 1862 | ||
| 177 | 20년 전 | 1986 | ||
| 176 | 20년 전 | 1807 | ||
| 175 | 20년 전 | 1872 | ||
| 174 | 20년 전 | 1694 | ||
| 173 | 20년 전 | 2061 | ||
| 172 | 20년 전 | 1787 | ||
| 171 | 20년 전 | 2566 | ||
| 170 | 20년 전 | 2274 | ||
| 169 | 20년 전 | 2555 | ||
| 168 | 20년 전 | 1487 | ||
| 167 | 20년 전 | 1578 | ||
| 166 | 20년 전 | 2160 | ||
| 165 | 20년 전 | 1631 | ||
| 164 | 20년 전 | 3771 | ||
| 163 | 20년 전 | 2668 | ||
| 162 | 20년 전 | 2086 | ||
| 161 | 20년 전 | 2795 | ||
| 160 | 20년 전 | 1735 | ||
| 159 | 20년 전 | 1616 | ||
| 158 | 20년 전 | 2567 | ||
| 157 | 20년 전 | 1489 | ||
| 156 | 20년 전 | 1743 | ||
| 155 | 20년 전 | 3231 | ||
| 154 | 20년 전 | 1902 | ||
| 153 | 20년 전 | 1625 | ||
| 152 | 20년 전 | 4953 | ||
| 151 | 20년 전 | 4579 | ||
| 150 | 20년 전 | 3516 | ||
| 149 | 20년 전 | 3800 | ||
| 148 | 20년 전 | 7078 | ||
| 147 | 20년 전 | 3550 | ||
| 146 | 20년 전 | 2625 | ||
| 145 | 20년 전 | 2623 | ||
| 144 | 20년 전 | 7168 | ||
| 143 | 20년 전 | 4602 | ||
| 142 | 20년 전 | 1900 | ||
| 141 | 20년 전 | 3253 | ||
| 140 | 20년 전 | 1963 | ||
| 139 | 20년 전 | 1586 | ||
| 138 | 20년 전 | 2309 | ||
| 137 | 20년 전 | 1782 | ||
| 136 | 20년 전 | 1457 | ||
| 135 | 20년 전 | 1786 | ||
| 134 | 20년 전 | 2968 | ||
| 133 | 20년 전 | 2451 | ||
| 132 | 20년 전 | 1707 | ||
| 131 | 20년 전 | 1643 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기