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
*/
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 130 | 20년 전 | 3993 | ||
| 129 | 20년 전 | 2932 | ||
| 128 | 20년 전 | 3668 | ||
| 127 | 20년 전 | 3498 | ||
| 126 | 20년 전 | 3756 | ||
| 125 | 20년 전 | 8584 | ||
| 124 | 20년 전 | 2601 | ||
| 123 | 20년 전 | 3743 | ||
| 122 | 20년 전 | 3204 | ||
| 121 | 20년 전 | 2609 | ||
| 120 | 20년 전 | 2666 | ||
| 119 | 20년 전 | 2579 | ||
| 118 | 20년 전 | 2859 | ||
| 117 |
|
20년 전 | 3054 | |
| 116 | 20년 전 | 5306 | ||
| 115 | 20년 전 | 3918 | ||
| 114 | 20년 전 | 4966 | ||
| 113 | 20년 전 | 6207 | ||
| 112 | 20년 전 | 7322 | ||
| 111 | 20년 전 | 18429 | ||
| 110 | 20년 전 | 6872 | ||
| 109 | 20년 전 | 2875 | ||
| 108 | 20년 전 | 4136 | ||
| 107 |
prosper
|
20년 전 | 2492 | |
| 106 |
prosper
|
20년 전 | 4314 | |
| 105 |
아우겐나이스
|
20년 전 | 2910 | |
| 104 | 20년 전 | 2255 | ||
| 103 | 20년 전 | 2474 | ||
| 102 | 20년 전 | 2253 | ||
| 101 | 20년 전 | 2571 | ||
| 100 | 20년 전 | 1743 | ||
| 99 | 20년 전 | 1563 | ||
| 98 | 20년 전 | 1615 | ||
| 97 | 20년 전 | 2126 | ||
| 96 | 20년 전 | 1871 | ||
| 95 | 20년 전 | 2370 | ||
| 94 | 20년 전 | 3564 | ||
| 93 | 20년 전 | 1558 | ||
| 92 | 20년 전 | 1755 | ||
| 91 | 20년 전 | 3177 | ||
| 90 | 20년 전 | 2339 | ||
| 89 | 20년 전 | 3168 | ||
| 88 | 20년 전 | 2868 | ||
| 87 | 20년 전 | 3293 | ||
| 86 | 20년 전 | 5130 | ||
| 85 | 20년 전 | 2516 | ||
| 84 | 20년 전 | 4823 | ||
| 83 | 20년 전 | 2501 | ||
| 82 | 20년 전 | 3119 | ||
| 81 | 20년 전 | 7623 | ||
| 80 | 20년 전 | 3820 | ||
| 79 | 20년 전 | 3206 | ||
| 78 | 20년 전 | 4693 | ||
| 77 | 20년 전 | 2894 | ||
| 76 | 20년 전 | 6215 | ||
| 75 | 20년 전 | 4470 | ||
| 74 | 20년 전 | 5780 | ||
| 73 | 20년 전 | 3624 | ||
| 72 | 20년 전 | 5967 | ||
| 71 | 20년 전 | 3113 | ||
| 70 | 20년 전 | 2836 | ||
| 69 | 20년 전 | 2638 | ||
| 68 | 20년 전 | 2444 | ||
| 67 | 20년 전 | 2653 | ||
| 66 | 20년 전 | 2669 | ||
| 65 | 20년 전 | 3784 | ||
| 64 | 20년 전 | 2823 | ||
| 63 | 20년 전 | 2454 | ||
| 62 | 20년 전 | 2264 | ||
| 61 | 20년 전 | 3069 | ||
| 60 | 20년 전 | 3133 | ||
| 59 | 20년 전 | 2515 | ||
| 58 | 20년 전 | 2587 | ||
| 57 | 20년 전 | 2965 | ||
| 56 | 20년 전 | 2316 | ||
| 55 | 20년 전 | 2759 | ||
| 54 | 20년 전 | 2126 | ||
| 53 | 20년 전 | 2348 | ||
| 52 | 20년 전 | 2698 | ||
| 51 |
prosper
|
21년 전 | 2349 | |
| 50 |
prosper
|
21년 전 | 2170 | |
| 49 | 21년 전 | 2175 | ||
| 48 | 21년 전 | 2336 | ||
| 47 | 21년 전 | 1930 | ||
| 46 | 21년 전 | 1927 | ||
| 45 | 21년 전 | 2131 | ||
| 44 | 21년 전 | 2358 | ||
| 43 | 21년 전 | 4571 | ||
| 42 |
prosper
|
21년 전 | 2713 | |
| 41 |
prosper
|
21년 전 | 2112 | |
| 40 | 21년 전 | 2178 | ||
| 39 | 21년 전 | 2143 | ||
| 38 | 21년 전 | 2417 | ||
| 37 | 21년 전 | 2567 | ||
| 36 | 21년 전 | 1770 | ||
| 35 | 21년 전 | 4069 | ||
| 34 | 21년 전 | 3850 | ||
| 33 | 21년 전 | 2989 | ||
| 32 |
prosper
|
21년 전 | 2901 | |
| 31 | 21년 전 | 5275 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기