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
*/
게시판 목록
팁게시판
질문은 상단의 QA에서 해주시기 바랍니다.
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 5652 | 10년 전 | 1303 | ||
| 5651 |
멋진남자임
|
10년 전 | 1413 | |
| 5650 | 10년 전 | 3123 | ||
| 5649 | 10년 전 | 1509 | ||
| 5648 |
멋진남자임
|
10년 전 | 2022 | |
| 5647 | 10년 전 | 5015 | ||
| 5646 | 10년 전 | 1743 | ||
| 5645 |
멋진남자임
|
10년 전 | 1893 | |
| 5644 | 10년 전 | 1564 | ||
| 5643 |
|
10년 전 | 1684 | |
| 5642 | 10년 전 | 2377 | ||
| 5641 | 10년 전 | 1490 | ||
| 5640 | 10년 전 | 1471 | ||
| 5639 | 10년 전 | 2406 | ||
| 5638 |
|
10년 전 | 3236 | |
| 5637 | 10년 전 | 3830 | ||
| 5636 | 10년 전 | 2109 | ||
| 5635 | 10년 전 | 1463 | ||
| 5634 | 10년 전 | 3369 | ||
| 5633 |
CHAVO
|
10년 전 | 1047 | |
| 5632 | 10년 전 | 2458 | ||
| 5631 | 10년 전 | 3214 | ||
| 5630 | 10년 전 | 1247 | ||
| 5629 | 10년 전 | 1345 | ||
| 5628 | 10년 전 | 2569 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기