title = $title; $this->author = $author; $this->price = $price; } public function getTitle(): string { return $this->title; } public function getAuthor(): string { return $this->author; } public function getPrice(): float { return $this->price; } public function getInfo(): string { return sprintf( "%s — Tác giả: %s — Giá: %s VNĐ", htmlspecialchars($this->title), htmlspecialchars($this->author), number_format($this->price, 0, ',', '.') ); } } class Ebook extends Book implements Downloadable { private float $fileSize; // Dung lượng tính bằng MB public function __construct(string $title, string $author, float $price, float $fileSize) { parent::__construct($title, $author, $price); $this->fileSize = $fileSize; } public function getFileSize(): float { return $this->fileSize; } public function getInfo(): string { return parent::getInfo() . sprintf(" — Dung lượng: %.1f MB", $this->fileSize); } public function download(): string { return "Đang tải xuống Ebook '{$this->title}' ({$this->fileSize} MB)... Hoàn tất 100%!"; } } // Danh sách sách mẫu trong thư viện $library = [ new Book("Clean Code (Mã Sạch)", "Robert C. Martin", 185000), new Book("Design Patterns (Gang of Four)", "Erich Gamma et al.", 240000), new Ebook("PHP 8 Objects, Patterns, and Practice", "Matt Zandstra", 120000, 14.5), new Ebook("Laravel Up & Running", "Matt Stauffer", 95000, 8.2), ]; $downloadNotice = null; if (isset($_GET['download_idx'])) { $idx = (int)$_GET['download_idx']; if (isset($library[$idx]) && $library[$idx] instanceof Downloadable) { $downloadNotice = $library[$idx]->download(); } } ?> Bài 07 - Hệ thống Quản lý Thư viện (Book & Ebook) | Lab 02 OOP

Bài 07: Hệ thống Quản lý Thư viện (Book, Ebook & Downloadable)

Kế thừa lớp Book và triển khai Interface Downloadable cho sách điện tử Ebook

Downloadable::download():
Danh mục Sách giấy & Ebook trong Thư viện
$item): ?>
Loại sách Thông tin chi tiết (getInfo()) Hành động (Interface Downloadable)
Ebook Sách giấy (Book) getInfo() ?> Gọi download() Không hỗ trợ tải file