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
*/
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 430 | 19년 전 | 4112 | ||
| 429 | 19년 전 | 3308 | ||
| 428 | 19년 전 | 4134 | ||
| 427 | 19년 전 | 3479 | ||
| 426 | 19년 전 | 3108 | ||
| 425 | 19년 전 | 3411 | ||
| 424 | 19년 전 | 2441 | ||
| 423 | 19년 전 | 2869 | ||
| 422 | 19년 전 | 2356 | ||
| 421 | 19년 전 | 3710 | ||
| 420 | 19년 전 | 4744 | ||
| 419 | 19년 전 | 3806 | ||
| 418 |
|
19년 전 | 1753 | |
| 417 | 19년 전 | 2753 | ||
| 416 | 19년 전 | 2395 | ||
| 415 | 19년 전 | 2610 | ||
| 414 | 19년 전 | 4414 | ||
| 413 |
|
19년 전 | 2606 | |
| 412 | 19년 전 | 3052 | ||
| 411 |
|
19년 전 | 2997 | |
| 410 |
|
19년 전 | 3693 | |
| 409 |
|
19년 전 | 3643 | |
| 408 |
|
19년 전 | 1861 | |
| 407 | 19년 전 | 2234 | ||
| 406 | 19년 전 | 2788 | ||
| 405 | 19년 전 | 2457 | ||
| 404 | 19년 전 | 4311 | ||
| 403 | 19년 전 | 3306 | ||
| 402 |
NeoGenesis
|
19년 전 | 4097 | |
| 401 | 19년 전 | 2610 | ||
| 400 |
|
19년 전 | 2502 | |
| 399 | 19년 전 | 2960 | ||
| 398 | 19년 전 | 2490 | ||
| 397 | 19년 전 | 2785 | ||
| 396 | 19년 전 | 2486 | ||
| 395 | 19년 전 | 3191 | ||
| 394 | 19년 전 | 1732 | ||
| 393 | 19년 전 | 2997 | ||
| 392 | 19년 전 | 2264 | ||
| 391 | 19년 전 | 2180 | ||
| 390 | 19년 전 | 2291 | ||
| 389 | 19년 전 | 2647 | ||
| 388 | 19년 전 | 2186 | ||
| 387 | 19년 전 | 4488 | ||
| 386 |
|
19년 전 | 2693 | |
| 385 |
|
19년 전 | 2494 | |
| 384 | 19년 전 | 3026 | ||
| 383 | 19년 전 | 3061 | ||
| 382 | 19년 전 | 3113 | ||
| 381 |
|
19년 전 | 2635 | |
| 380 |
|
19년 전 | 3025 | |
| 379 | 19년 전 | 2551 | ||
| 378 | 19년 전 | 2213 | ||
| 377 | 19년 전 | 2783 | ||
| 376 | 19년 전 | 2471 | ||
| 375 |
|
19년 전 | 2567 | |
| 374 | 19년 전 | 3823 | ||
| 373 | 19년 전 | 3276 | ||
| 372 | 19년 전 | 4995 | ||
| 371 |
세은아빠2
|
19년 전 | 2419 | |
| 370 | 19년 전 | 4504 | ||
| 369 | 19년 전 | 3109 | ||
| 368 | 19년 전 | 2910 | ||
| 367 | 19년 전 | 3729 | ||
| 366 | 19년 전 | 2661 | ||
| 365 | 19년 전 | 3748 | ||
| 364 | 19년 전 | 4022 | ||
| 363 | 19년 전 | 3449 | ||
| 362 | 19년 전 | 3488 | ||
| 361 | 19년 전 | 4110 | ||
| 360 |
hwatta
|
19년 전 | 2367 | |
| 359 | 19년 전 | 5111 | ||
| 358 | 19년 전 | 3660 | ||
| 357 | 19년 전 | 2606 | ||
| 356 |
sdesign1s
|
19년 전 | 2284 | |
| 355 | 20년 전 | 2758 | ||
| 354 | 20년 전 | 3031 | ||
| 353 | 20년 전 | 2798 | ||
| 352 |
|
20년 전 | 5777 | |
| 351 |
|
20년 전 | 2709 | |
| 350 |
|
20년 전 | 4299 | |
| 349 |
hwatta
|
20년 전 | 2187 | |
| 348 | 20년 전 | 7311 | ||
| 347 | 20년 전 | 2418 | ||
| 346 | 20년 전 | 3507 | ||
| 345 | 20년 전 | 4314 | ||
| 344 | 20년 전 | 2658 | ||
| 343 | 20년 전 | 3923 | ||
| 342 | 20년 전 | 3072 | ||
| 341 | 20년 전 | 4097 | ||
| 340 |
|
20년 전 | 5148 | |
| 339 |
|
20년 전 | 4240 | |
| 338 | 20년 전 | 5880 | ||
| 337 | 20년 전 | 2046 | ||
| 336 |
|
20년 전 | 3326 | |
| 335 |
|
20년 전 | 3543 | |
| 334 |
|
20년 전 | 2937 | |
| 333 |
hwatta
|
20년 전 | 2444 | |
| 332 | 20년 전 | 4658 | ||
| 331 | 20년 전 | 2280 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기