Menu
Coddy logo textTech

E-commerce 商品システム

CoddyのJavaScriptジャーニー「オブジェクト指向プログラミング」セクションの一部 — レッスン 54/56。

challenge icon

チャレンジ

これはオンラインストアの商品システムです。商品には価格があり、ノートパソコンのような物理的な商品は重量に基づいて送料を計算します。電子書籍のようなデジタル商品は送料が無料になります。

あなたのタスクは、商品システムの不足しているメソッドを完成させることです:

1: product.js 内の Product クラスを完成させてください:

  • this.#price を返す getPrice() メソッドを追加する
  • `${this.name} - ${this.#price}` を返す getDescription() メソッドを追加する

2: physicalProduct.js 内の PhysicalProduct クラスを完成させてください:

  • 以下の処理を行う calculateShipping() メソッドを追加する:
    • this.weight * 0.5 (1ポンドあたり50セント) を計算する
    • 送料を返す

自分で試してみよう

import { Product } from './product.js';
import { PhysicalProduct } from './physicalProduct.js';

const book = new Product('Book', 20);
const laptop = new PhysicalProduct('Laptop', 1000, 5);

console.log(book.getDescription()); // 表示内容: "Book - $20"
console.log(laptop.getDescription()); // 表示内容: "Laptop - $1000  (5 lbs)"

console.log(`Shipping cost for laptop: $${laptop.calculateShipping()}`); // 表示内容: "Shipping cost for laptop: $2.5"

console.log(`Book price: $${book.getPrice()}`); // 表示内容: "20"
console.log(`Laptop price: $${laptop.getPrice()}`); // 表示内容: "1000"

オブジェクト指向プログラミングのすべてのレッスン