import { Product } from './product'
export class Playstation extends Product {
generation:number = 0;
constructor(names:string,generations:number,prices:number){
super(names,prices)
this.generation = generations;
}
getProfile(){
return `${this.name} (Gen ${this.generation})`
}
getDiscountPrice(){
return this.price - (this.price * (Playstation.DISCOUNT_PERCENT/100))
}
}
export class Product {
readonly name:string = "";
protected price:number = 0;
static readonly DISCOUNT_PERCENT:number =10;
constructor(names:string,prices:number=0){
this.name = names;
this.price = prices;
}
getPrice(){
return this.price;
}
setPrice(price: number){
this.price = price;
}
}
import { Product } from './product'
import { Playstation } from './playstation'
const product = new Product('Generic Product')
console.log(product.name)
// Generic Product
console.log(product.getPrice())
// 0
product.setPrice(1000)
console.log(product.getPrice())
// 1000
const ps = new Playstation('PS5', 5, 17990)
console.log(ps.getProfile())
// PS5 (Gen 5)
console.log(ps.getPrice())
// 17990
ps.setPrice(18990)
console.log(ps.getPrice())
// 18990
console.log(ps.getDiscountPrice())
// 17091 (ถ้า Product.DISCOUNT_PERCENT = 10, คำนวณจาก 18990 - (18990 * 10 / 100))
# TypeScript Quiz — ข้อ 3 (1 คะแนน)
## 🎯 คำสั่ง
ให้นักเรียนสร้าง 2 class ได้แก่
- `Product` ในไฟล์ `product.ts` (parent class)
- `Playstation` ในไฟล์ `playstation.ts` (child class ที่ `extends Product`)
โดยต้องทำให้ไฟล์ `index.ts` ที่กำหนดให้ สามารถทำงานได้ถูกต้อง และได้ผลลัพธ์ตามที่คาดหวัง
ห้ามแก้ไขไฟล์ `index.ts`
## 📌 หมายเหตุสำคัญ
**ในไฟล์ `index.ts` มี comment (`//`) ที่แสดงตัวอย่างผลลัพธ์ที่คาดหวัง**
ให้นักเรียนอ่านผลลัพธ์จาก comment เหล่านั้น แล้วเขียน method logic ใน class ต่างๆ ให้สอดคล้องกับผลลัพธ์ที่แสดงไว้
**ตัวอย่าง:**
```typescript
console.log(ps.getProfile())
// PS5 (Gen 5)
จาก comment ด้านบน แสดงว่าผลลัพธ์ที่คาดหวังคือ "PS5 (Gen 5)" ดังนั้น method getProfile() ใน class Playstation ต้อง return ค่าในรูปแบบนี้
📁 โครงสร้างไฟล์ที่ต้องมี
p03/
├── product.ts
├── playstation.ts
└── index.ts
export class Product และ export class Playstation extends ProductProduct ต้องมี attribute name และ price โดย
name ต้องอ่านได้จากภายนอก object แต่แก้ไขไม่ได้ (readonly)price ต้องไม่ให้เข้าถึงตรง ๆ จากภายนอก แต่ class ลูกต้องนำไปใช้งานต่อได้Product ต้องกำหนดค่าเริ่มต้นของ price เป็น 0Product ต้องมี constructor ที่รับ name และ price แบบ optionalProduct ต้องมี method:
getPrice()setPrice(price: number)Product ต้องมี static readonly สำหรับเปอร์เซ็นต์ส่วนลด (เช่น DISCOUNT_PERCENT) เพื่อใช้ในการคำนวณPlaystation ต้องมี constructor รับค่า name, generation, pricePlaystation ต้องเขียน getProfile() และมี method getDiscountPrice() สำหรับคำนวณราคาหลังหักส่วนลด โดยใช้ค่า static readonly จาก Productanyconsole.log ภายใน class