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
*/
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 7630 | 10년 전 | 671 | ||
| 7629 |
|
10년 전 | 2424 | |
| 7628 | 10년 전 | 809 | ||
| 7627 |
|
10년 전 | 1046 | |
| 7626 |
|
10년 전 | 1802 | |
| 7625 | 10년 전 | 733 | ||
| 7624 | 10년 전 | 748 | ||
| 7623 |
|
10년 전 | 3125 | |
| 7622 | 10년 전 | 747 | ||
| 7621 |
leeleeleelee
|
10년 전 | 603 | |
| 7620 | 10년 전 | 552 | ||
| 7619 | 10년 전 | 515 | ||
| 7618 | 10년 전 | 1050 | ||
| 7617 | 10년 전 | 737 | ||
| 7616 | 10년 전 | 672 | ||
| 7615 | 10년 전 | 740 | ||
| 7614 | 10년 전 | 1281 | ||
| 7613 |
|
10년 전 | 2096 | |
| 7612 | 10년 전 | 1178 | ||
| 7611 | 10년 전 | 1440 | ||
| 7610 |
|
10년 전 | 1916 | |
| 7609 |
|
10년 전 | 1374 | |
| 7608 |
mwdkim
|
10년 전 | 1148 | |
| 7607 |
|
10년 전 | 1079 | |
| 7606 |
mwdkim
|
10년 전 | 3954 | |
| 7605 | 10년 전 | 704 | ||
| 7604 | 10년 전 | 1042 | ||
| 7603 | 10년 전 | 1664 | ||
| 7602 |
|
10년 전 | 1095 | |
| 7601 |
AniNest
|
10년 전 | 2810 | |
| 7600 |
port443
|
10년 전 | 1052 | |
| 7599 | 10년 전 | 958 | ||
| 7598 | 10년 전 | 1039 | ||
| 7597 | 10년 전 | 4589 | ||
| 7596 |
SeungYeon
|
10년 전 | 908 | |
| 7595 |
untitled
|
10년 전 | 2453 | |
| 7594 |
프로그래머7
|
10년 전 | 1747 | |
| 7593 |
untitled
|
10년 전 | 2388 | |
| 7592 |
untitled
|
10년 전 | 1957 | |
| 7591 |
untitled
|
10년 전 | 2693 | |
| 7590 |
아리마2001
|
10년 전 | 870 | |
| 7589 | 10년 전 | 1122 | ||
| 7588 |
|
10년 전 | 2938 | |
| 7587 | 10년 전 | 1319 | ||
| 7586 | 10년 전 | 686 | ||
| 7585 | 10년 전 | 1720 | ||
| 7584 | 10년 전 | 1424 | ||
| 7583 |
leeleeleelee
|
10년 전 | 1189 | |
| 7582 |
|
10년 전 | 1148 | |
| 7581 | 10년 전 | 1376 | ||
| 7580 | 10년 전 | 1030 | ||
| 7579 |
|
10년 전 | 619 | |
| 7578 | 10년 전 | 1440 | ||
| 7577 |
|
10년 전 | 1888 | |
| 7576 | 10년 전 | 1401 | ||
| 7575 |
멋진남자임
|
10년 전 | 1481 | |
| 7574 | 10년 전 | 2140 | ||
| 7573 | 10년 전 | 3276 | ||
| 7572 | 10년 전 | 776 | ||
| 7571 |
|
10년 전 | 796 | |
| 7570 |
|
10년 전 | 1337 | |
| 7569 | 10년 전 | 1565 | ||
| 7568 |
this1mg
|
10년 전 | 1064 | |
| 7567 |
|
10년 전 | 781 | |
| 7566 | 10년 전 | 929 | ||
| 7565 |
Angel하늘
|
10년 전 | 1031 | |
| 7564 |
seoldi
|
10년 전 | 1277 | |
| 7563 |
|
10년 전 | 1415 | |
| 7562 |
멋진남자임
|
10년 전 | 2113 | |
| 7561 | 10년 전 | 732 | ||
| 7560 |
leeleeleelee
|
10년 전 | 930 | |
| 7559 | 10년 전 | 5070 | ||
| 7558 |
RinaP
|
10년 전 | 810 | |
| 7557 |
|
10년 전 | 1271 | |
| 7556 | 10년 전 | 1211 | ||
| 7555 |
hyohyojj1234
|
10년 전 | 1680 | |
| 7554 | 10년 전 | 1108 | ||
| 7553 |
senseme
|
10년 전 | 1357 | |
| 7552 |
ehdltdoit
|
10년 전 | 1455 | |
| 7551 |
|
10년 전 | 1846 | |
| 7550 |
leeleeleelee
|
10년 전 | 1614 | |
| 7549 | 10년 전 | 2447 | ||
| 7548 | 10년 전 | 1856 | ||
| 7547 |
멋진남자임
|
10년 전 | 1975 | |
| 7546 | 10년 전 | 1020 | ||
| 7545 |
ILMare1003
|
10년 전 | 1311 | |
| 7544 |
|
10년 전 | 1267 | |
| 7543 | 10년 전 | 901 | ||
| 7542 | 10년 전 | 681 | ||
| 7541 |
울라라라우
|
10년 전 | 879 | |
| 7540 | 10년 전 | 1608 | ||
| 7539 | 10년 전 | 954 | ||
| 7538 |
|
10년 전 | 1843 | |
| 7537 | 10년 전 | 3629 | ||
| 7536 |
Gaumi
|
10년 전 | 1438 | |
| 7535 |
프로그램은어려워
|
10년 전 | 1292 | |
| 7534 |
senseme
|
10년 전 | 1218 | |
| 7533 | 10년 전 | 1222 | ||
| 7532 | 10년 전 | 886 | ||
| 7531 | 10년 전 | 2075 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기