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
*/
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 330 |
prosper
|
20년 전 | 2263 | |
| 329 |
prosper
|
20년 전 | 1884 | |
| 328 |
prosper
|
20년 전 | 1690 | |
| 327 | 20년 전 | 3754 | ||
| 326 | 20년 전 | 4845 | ||
| 325 |
hwatta
|
20년 전 | 2470 | |
| 324 |
|
20년 전 | 3137 | |
| 323 | 20년 전 | 5849 | ||
| 322 |
hwatta
|
20년 전 | 2838 | |
| 321 |
hwatta
|
20년 전 | 2311 | |
| 320 |
yesmoa
|
20년 전 | 4566 | |
| 319 | 20년 전 | 2678 | ||
| 318 | 20년 전 | 2233 | ||
| 317 |
kyodon
|
20년 전 | 2756 | |
| 316 | 20년 전 | 2571 | ||
| 315 |
|
20년 전 | 2879 | |
| 314 |
|
20년 전 | 3335 | |
| 313 |
|
20년 전 | 2636 | |
| 312 |
yesmoa
|
20년 전 | 4708 | |
| 311 | 20년 전 | 3263 | ||
| 310 |
홀로남은자
|
20년 전 | 4556 | |
| 309 | 20년 전 | 3003 | ||
| 308 | 20년 전 | 4123 | ||
| 307 | 20년 전 | 4366 | ||
| 306 | 20년 전 | 7029 | ||
| 305 | 20년 전 | 3867 | ||
| 304 | 20년 전 | 2761 | ||
| 303 |
크리스탈처럼
|
20년 전 | 4393 | |
| 302 | 20년 전 | 2201 | ||
| 301 |
|
20년 전 | 4316 | |
| 300 | 20년 전 | 3798 | ||
| 299 | 20년 전 | 2606 | ||
| 298 | 20년 전 | 4820 | ||
| 297 |
|
20년 전 | 2523 | |
| 296 | 20년 전 | 4522 | ||
| 295 | 20년 전 | 3565 | ||
| 294 | 20년 전 | 3590 | ||
| 293 | 20년 전 | 3814 | ||
| 292 | 20년 전 | 3202 | ||
| 291 |
yesmoa
|
20년 전 | 5936 | |
| 290 | 20년 전 | 2934 | ||
| 289 | 20년 전 | 5845 | ||
| 288 |
|
20년 전 | 2373 | |
| 287 |
|
20년 전 | 1794 | |
| 286 |
|
20년 전 | 2145 | |
| 285 |
|
20년 전 | 3530 | |
| 284 |
|
20년 전 | 2028 | |
| 283 |
|
20년 전 | 4399 | |
| 282 | 20년 전 | 3381 | ||
| 281 |
|
20년 전 | 2208 | |
| 280 |
|
20년 전 | 7801 | |
| 279 | 20년 전 | 5549 | ||
| 278 | 20년 전 | 2990 | ||
| 277 |
|
20년 전 | 5568 | |
| 276 | 20년 전 | 2355 | ||
| 275 | 20년 전 | 2602 | ||
| 274 | 20년 전 | 2380 | ||
| 273 | 20년 전 | 2215 | ||
| 272 | 20년 전 | 2146 | ||
| 271 | 20년 전 | 2599 | ||
| 270 | 20년 전 | 2628 | ||
| 269 | 20년 전 | 2480 | ||
| 268 | 20년 전 | 2679 | ||
| 267 | 20년 전 | 2363 | ||
| 266 | 20년 전 | 2561 | ||
| 265 | 20년 전 | 3502 | ||
| 264 |
|
20년 전 | 5358 | |
| 263 |
|
20년 전 | 3724 | |
| 262 | 20년 전 | 3177 | ||
| 261 |
허저비
|
20년 전 | 5926 | |
| 260 |
|
20년 전 | 5712 | |
| 259 | 20년 전 | 4138 | ||
| 258 | 20년 전 | 2375 | ||
| 257 | 20년 전 | 3191 | ||
| 256 | 20년 전 | 1897 | ||
| 255 | 20년 전 | 1567 | ||
| 254 | 20년 전 | 3141 | ||
| 253 | 20년 전 | 3545 | ||
| 252 | 20년 전 | 5122 | ||
| 251 | 20년 전 | 5805 | ||
| 250 | 20년 전 | 3674 | ||
| 249 | 20년 전 | 5012 | ||
| 248 | 20년 전 | 3283 | ||
| 247 | 20년 전 | 3638 | ||
| 246 |
|
20년 전 | 7949 | |
| 245 |
|
20년 전 | 5905 | |
| 244 | 20년 전 | 4487 | ||
| 243 |
|
20년 전 | 4051 | |
| 242 | 20년 전 | 2783 | ||
| 241 | 20년 전 | 2727 | ||
| 240 | 20년 전 | 2363 | ||
| 239 | 20년 전 | 1660 | ||
| 238 |
아우겐나이스
|
20년 전 | 2263 | |
| 237 |
email
|
20년 전 | 3686 | |
| 236 | 20년 전 | 4154 | ||
| 235 | 20년 전 | 10461 | ||
| 234 | 20년 전 | 5068 | ||
| 233 | 20년 전 | 3370 | ||
| 232 | 20년 전 | 3208 | ||
| 231 | 20년 전 | 3840 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기